Description
Input
Output
Sample Input
6 5 1 4 1 6 1 7 2 7 2 8 3 0
Sample Output
4
动态规划,dp[x][T]表示在T时刻,x位置他最多容纳的馅饼数量
对T进行排序
当T_cur(现在的时刻)==T_pre(上一个时刻)
dp[t_cur.x][t_cur.T]+=1;对T时刻,x位置的馅饼数+1
当T_cur(现在的时刻)>T_pre(上一个时刻)
r=-T_cur-T_pre
假设j为T_pre他所在的位置
那么在r这个时间段他的活动范围是 k属于[ j-r,j+r ];
dp[j][t_cur.T]=find_max(dp[k][t_pre.T],dp[j][t_cur.T]);
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <vector> #include <algorithm> #define MAX_T 100001 #define find_max(a,b) a>b?a:b using namespace std; int dp[11][MAX_T]; struct node{ int x,T; bool operator < (const node &a) const { return T<a.T; } }; int main() { int n; while(~scanf("%d",&n)) { if(n==0) break; vector<node> vtable; int x,T; for(int i=0;i<n;++i) { scanf("%d%d",&x,&T); vtable.push_back((node){x,T}); } vtable.push_back((node){5,0}); sort(vtable.begin(),vtable.end()); memset(dp,-1,sizeof(dp)); dp[5][0]=0; int max=0; for(int i=1;i<vtable.size();++i) { node &t_cur=vtable[i]; node &t_pre=vtable[i-1]; int r=t_cur.T-t_pre.T; if(r) { for(int j=0;j<11;++j) { for(int k=j-r;k<=j+r;++k) { if(k>=0&&k<11) dp[j][t_cur.T]=find_max(dp[k][t_pre.T],dp[j][t_cur.T]); } max=find_max(dp[j][t_cur.T],max); } } dp[t_cur.x][t_cur.T]+=1; max=find_max(dp[t_cur.x][t_cur.T],max); } printf("%d\n",max); } return 0; }