monkey |
description |
Cc is a lovely monkey. It likes to play the game "catching plates". |
input |
For each test case,the first line contains three integers n, m and t(1 <= n <= 100,1 <= m <= 100,0 <= t <= 100), indicating the number of the pegs, the number of the plates and the maximum number Cc can jump.The next m lines gives ai and bi each, which means the clown will throw a plate with number bi on it to peg ai at the ith second. |
output |
For each test case, first print a line saying "Scenario #k", where k is the number of the test case.Then, print the maximum number Cc can get, one per line.Print a blank line after each test case, even after the last one. |
sample_input |
4 4 2 |
sample_output |
Scenario #1 |
hint |
source |
Time Limit 1000ms | Memory Limit 65536K |
思路:三维DP ,dp[a][b][c]代表,第a个时间在b个钉子上已经跳了c次得到的最多的香蕉。
dp[a][b][c]=max(dp[a-1][b-1][c-1],dp[a-1][b+1][c-1],dp[a-1][b][c])+该时刻该点的香蕉数。
这题坑人啊,时间卡的忒紧了,如果三维,初始化,比爆无疑。因此使用滚动数组,使其初始化时间减少。
另外,max()中能提高效率就尽量使用高效率的。否则还是过不了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mm=103;
const int oo=1e9;
int dp[3][mm][mm];
int bana[mm][mm];
int n,m,t,ans;
void Max(int&x,const int y)
{
if(x<y)x=y;
}
void data()
{
memset(bana,0,sizeof(bana));
for(int i=0;i<=1;++i)
for(int j=0;j<=n+1;++j)
for(int k=0;k<=t;++k)
dp[i][j][k]=-oo;
dp[0][1][0]=0;
}
void DP()
{ int z=1;
for(int i=1;i<=m;++i)///扔盘子的时间
{
for(int j=1;j<=n;++j)///所在的钉子
{
if(dp[z^1][j][0]>=0)
{
dp[z][j][0]=dp[z^1][j][0]+bana[i][j];
Max(ans,dp[z][j][0]);
}
for(int k=1;k<=t;++k)
{ dp[z][j][k]=max(dp[z^1][j-1][k-1],max(dp[z^1][j+1][k-1],dp[z^1][j][k]))+bana[i][j];
/*Max(dp[z][j][k],dp[z^1][j-1][k-1]+bana[i][j]);
Max(dp[z][j][k],dp[z^1][j+1][k-1]+bana[i][j]);
Max(dp[z][j][k],dp[z^1][j][k]+bana[i][j]);
Max(ans,dp[z][j][k]);
*/
///用上上面的注释也还是超时,实在无语
}
}
z^=1;
}
}
int main()
{ int cas=0;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{ int b,c;
data();
for(int i=1;i<=m;++i)
{
scanf("%d%d",&b,&c);
bana[i][b]=c;
}
ans=0;
DP();
printf("Scenario #%d\n%d\n\n",++cas,ans);
}
}