HDU1176——免费馅饼

来源:互联网 发布:淘宝申诉场景怎么写 编辑:程序博客网 时间:2024/06/05 23:04

时间紧迫啊,懒得写题目了,直接上链接好了~

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176

题目分析:这里采用的是逆推的思想,好好体会体会,可能还掌握得不是很好。

先把代码附上啦。边境处理的时候还是蛮有技巧的。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define M 100002#define N 12int dp[N][M];inline int max(const int a, const int b){return a > b ? a : b;}void main(){int t,x;int i,j;int n,T;int temp;while(scanf("%d", &n)){if(!n)break;T = 0;memset(dp,0,sizeof(dp));while(n--){scanf("%d %d",&x,&t);++dp[x][t];T = T > t ? T : t;}for(j = T - 1; j >= 0; --j){for(i = 0; i <= 10; ++i){temp = max(dp[i + 1][j + 1],dp[i][j + 1]);if(i > 0){temp = max(dp[i - 1][j + 1],temp);}dp[i][j] += temp;}}printf("%d\n",dp[5][0]);}}

最开始为了节省内存,想把dp数组搞成一维的,结果老是出错。先把代码放在这里,以后再说啦~可能有一天就自己想清楚了。

没AC过的代码也放上,以后再思考:不过这种方法确实没有上面一种方法简洁啦~~

#include <stdio.h>#include <stdlib.h>#include <string.h>#define M 1000002#define N 13int dp[2][N];struct tu{int x;int t;}s[M];inline int max(const int a, const int b){return a > b ? a : b;}int compare(const void *a, const void *b){struct tu *t1 = (struct tu *)a;struct tu *t2 = (struct tu *)b;return t1->t - t2->t;}void main(){int n;int i,j,k;int nMax;int now;while(scanf("%d", &n)){if(!n)break;for(i = 0; i < n; ++i){scanf("%d %d", &s[i].x, &s[i].t);}qsort(s,n,sizeof(s[0]),compare);memset(dp,0,sizeof(dp));k = 0;nMax = 0;now = 0;for(j = 1; k < n; ++j){now ^= 1;//dp[i]更新成上一个时刻的最大值for(i = 1; i <= 11; ++i){dp[now][i] = max(dp[now^1][i], dp[now^1][i - 1]);dp[now][i] = max(dp[now][i], dp[now^1][i + 1]);nMax = max(nMax,dp[now][i]);}//加上这一时刻该点掉下来的馅饼for(; s[k].t == j; ++k){if(j == 1 && (s[k].x < 4 || s[k].x > 6)){continue;}dp[now][s[k].x+1] += 1;nMax = max(nMax,dp[now][s[k].x+1]);}}printf("%d\n", nMax);}}


原创粉丝点击