poj 3167 Cow Bowling【dp】

这是一道最最基础的dp题目,还记得当时看刘汝佳写的《入门经典》时,就是拿的这个做例子,不过牛人当然是一笔带过这么简单的例子。

状态转移方程为:dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]),如果要记录路径也简单,另外再用一个数组专门存放原始数组就好

原三角形是

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

dp三角形是

30

23 21

20 13 10

7 12 10 10

4  5    2   6    5

如果要记录路径,就是从dp[1][1](即30)开始,2选1较大的,并在input数组中找到对应的数即可。如:

30-->23-->20-->12-->5            对应有:

7  -->3
 -->8  -->7  -->5

思路很清晰

AC的代码:

#include<stdio.h>

int dp[352][352];

inline int max(int a,int b){return a>b?a:b;}

int main()
{
    int n,i,j;

    scanf("%d",&n);

	//输入
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)
            scanf("%d",&dp[i][j]);

	//dp
    for(i=n-1;i>=1;i--)
        for(j=1;j<=i;j++)
            dp[i][j]=max(dp[i][j]+dp[i+1][j],dp[i][j]+dp[i+1][j+1]);

    printf("%d\n",dp[1][1]);

    return 0;
}

这道题用不用两个数组都可以,不过记录路径就要用

用两个数组就是要把第n层的数据复制过去

#include <stdio.h>

int input[360][360];
int dp[360][360];

inline int max(int a,int b){return a>b?a:b;}

int main()
{
	int n;
	scanf("%d",&n);

	//输入
	int i,j;
	for(i=1;i<=n;i++)
		for(j=1;j<=i;j++)
			scanf("%d",&input[i][j]);

	for(i=1;i<=n;i++)
		dp[n][i]=input[n][i];
	//dp
	for(i=n-1;i>=1;i--)
		for(j=1;j<=i;j++)
			dp[i][j]=input[i][j]+max(dp[i+1][j],dp[i+1][j+1]);

	printf("%d\n",dp[1][1]);

	return 0;
}

复制过来两个别人写的很好的文章

1.poj 3176 的结题报告:POJ3176-Cow Bowling

2.

五大常用算法之二:动态规划算法

时间: 2024-08-27 17:37:31

poj 3167 Cow Bowling【dp】的相关文章

POJ 3176 Cow Bowling:简单DP

Cow Bowling http://poj.org/problem?id=3176 Time Limit: 1000MS Memory Limit: 65536K Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-l

poj 1503 Integer Inquiry【高精度】

这题总算是没有那么水的感觉了,不过还是水题,哈哈哈...题目主要是求高精度----大数的和,我专门写了一个add函数处理,sum和VeryLongIntegers是两个全局的变量,开始我还准备把sum也写成char型的字符串,但是考虑到结尾的'\0',那不是自找麻烦..果断换成int型数组,这样就容易处理多了. 在add函数中,我把VeryLongIntegers又反转了一次(即变成低位在前),也换成一个int型的数组,这样就变成两个int型数组的高精度加法了... 开始还觉得可能会WA一次,不

poj 3264 Balanced Lineup【RMQ】

这道题是poj上面RMQ最具代表性的一道题,没有之一 题目也基本上就是一个裸RMQ的算法,看到这道题也可以用线段树解决,决定还是要出一个线段树的版本的,一道题搞懂两个知识点,多好! RMQ(Range Minimum/Maximum Query)问题:      RMQ问题是求给定区间中的最值问题.当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够.可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值).不过,Sparse_Table算

poj 1326 Mileage Bank【四舍五入】

说实话,这道题虽然不难,但是考人的点还是有的,我WA了一次,后来检查发现了两个错误,看网上的结题报告,发现基本每个人都错了很多次...这说明不只是不仔细这么简单... 这道题有两个考人的点: 1.1-500 miles 就说明了 0miles 就不能算跑了路,如果出现 0miles 得算0处理 2.就是四舍五入的地方,只可能出现在 "B" 中,任何整数乘以1.5,只可能是 X.5 或者 X.0,当 X.5 的时候就要进位,但是 X.0 任为整数,不能进位的...[四舍五入的分析和模板我

算法:POJ 2184 Cow Exhibition (dp 转换01背包)

题目大意: 有N个物品,每个物品有属性Si和Fi,-1000 <= Si, Fi <= 1000, 每种物品最 多只能选一次,问怎样选使得物品的所有Si和Fi属性之和最大,并且要求Si之和与Fi之和都不能下于 0. 思路: 这题想了很久都没思路,于是跟前辈请教了下,恍然大悟.把属性Si当做是物品的 费用,Fi当做是价值,然后做01背包即可. 代码: #include<iostream> #include<queue> #include<stack> #inc

poj 3904 Sky Code【容斥原理】

点击打开链接 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1562   Accepted: 478 Description Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing

poj 2406 Power Strings【KMP】

点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b =

【原创】为什么 Redis 重启后没有正确恢复之前的内存数据

安装 Redis 后,默认配置下启动会得到如下日志:  ? 1 2 [3077] 05 Sep 10:01:54.181 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the

并查集专题【完结】

个人整理 并查集 [poj] 第一题 poj 1182 食物链 点击打开链接poj 1182 思路: 带权并查集 分析: 1 典型的带权并查集的题目 2 如果x和y是同类的关系认为是0,如果是x吃y那么关系认为是1,那么x和y的关系为2就说明y吃x 3 那么如果x和y同类则rank[x] == rank[y] , 如果x吃y那么rank[x] == (rank[y]+1)%3 , 注意mod3的使用 点击查看代码 第二题 poj 1308 Is It A Tree? 点击打开链接 poj 130