
6 5 1 4 1 6 1 7 2 7 2 8 3 0
4
题意很明了,无需多说,本题不难,但是第一步把题意转换成一个以t,x为横竖坐标的思路还是花了点时间的,一看题目就很容易想到要从最后的时间依次向前递推,而递推的方向只能是从相邻的两侧(距离1m)或者原地不动,取一个最大值,依次累加到t为0为止,这时候输出5那个位置上的数即可,这里为了处理方便,x坐标都向右偏移一位,这样不用单独处理最左边时候的情况,但最后输出的时候也应该输出6那个位置(相当于5)的数。1A
思考:这个题目还可以问从哪一点开始接馅饼,能接到的馅饼最多,这个时候就可以最后扫一遍t = 0的那一行(也就是最下面的那一行)选出那个最大的就是解。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXX = 100005;
int tower[MAXX][15];
int max(int a, int b, int c){
int ret = b;
if (a > b){
ret = a;
}
if (ret < c){
ret = c;
}
return ret;
}
void solve(int t){
for (int i = t - 1; i >= 0; --i){
for (int j = 1; j <= 11; ++j){
tower[i][j] += max(tower[i + 1][j], tower[i + 1][j - 1], tower[i + 1][j + 1]);
}
}
}
int main(){
//freopen("in.txt", "r", stdin);
int c,x,t,mt;
while (scanf("%d", &c) != EOF){
if (c == 0)break;
memset(tower, 0, sizeof(tower));
mt = -1;
while (c--){
scanf("%d %d", &x, &t);
++tower[t][x+1];
if (t > mt)mt = t;
}
solve(mt);
printf("%d\n",tower[0][6]);
}
}HDU 1176 免费馅饼 (DP),布布扣,bubuko.com
原文地址:http://blog.csdn.net/iaccepted/article/details/29843227