杭电2182-Frog

Frog

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 595    Accepted Submission(s): 286


Problem Description
A little frog named Fog is on his way home. The path's length is N (1 <= N <= 100), and there are many insects along the way. Suppose the
original coordinate of Fog is 0. Fog can stay still or jump forward T units, A <= T <= B. Fog will eat up all the insects wherever he stays, but he will
get tired after K jumps and can not jump any more. The number of insects (always less than 10000) in each position of the path is given.
How many insects can Fog eat at most?
Note that Fog can only jump within the range [0, N), and whenever he jumps, his coordinate increases.
 

Input
The input consists of several test cases.
The first line contains an integer T indicating the number of test cases.
For each test case:
The first line contains four integers N, A, B(1 <= A <= B <= N), K (K >= 1).
The next line contains N integers, describing the number of insects in each position of the path.
 

Output
each test case:
Output one line containing an integer - the maximal number of insects that Fog can eat.
 

Sample Input

1 4 1 2 2 1 2 3 4
 

Sample Output

8
该题又是一道动态规划题,所以关键是写出动态转移方程,该题的动态转移方程为:maxn=DP[num-1][pos-i];和if(maxn!=-1) DP[num][pos]=Now[pos]+maxn; else DP[num][pos]=0;
这里解释下:因为青蛙的跳跃范围是【a,b】所以假设青蛙在起始点0所以一次跳跃后,我们要计算青蛙从【0+a,0+b】中最大蚊子位置和数量后然加上起始位置的蚊子数量,即DP[跳跃次数][当前位置]=num[当前位置]+Max(DP[跳跃次数-1][当前位置-a],DP[跳跃次数-1][当前位置-(a+1)],.....,DP[跳跃次数-1][当前位置-b])
AC代码:
#include const int MAX=101; int DP[MAX][MAX]; int Now[MAX]; using namespace std; int main() { int t; int n,a,b,k,i,num,pos,maxn; while(cin>>t) { while(t--) { cin>>n>>a>>b>>k; memset(DP,0,sizeof(DP)); memset(Now,0,sizeof(Now)); for(i=0;i { scanf("%d",&Now[i]); } for(i=0;i<=k;i++) { DP[i][0]=Now[0]; } for(num=1;num<=k;num++) { for(pos=1;pos { maxn=-1; for(i=a;i<=b;i++) { if(pos-i>=0&&DP[num-1][pos-i]) { if(maxn { maxn=DP[num-1][pos-i]; } } } if(maxn!=-1) DP[num][pos]=Now[pos]+maxn; else DP[num][pos]=0; } } maxn=DP[k][0]; for(i=1;i { if(DP[k][i]>maxn) maxn=DP[k][i]; } cout< } } return 0; } 

你可能感兴趣的:(杭电ACM_算法题_动态规划)