HDU--2182--动态规划

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

思路:dp[i][j]为青蛙跳j次到达i位置可以吃到的最多的昆虫数;dp[i][j]=max(dp[i][j],dp[t][j-1]+p[i]);

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int p[105];
int dp[105][105];
int n,a,b,k;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(dp,0,sizeof(dp));
        scanf("%d%d%d%d",&n,&a,&b,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&p[i]);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=k;j++){
                for(int r=i+a;r<=i+b&&r<=n;r++)
                    dp[r][j]=max(dp[r][j],dp[i][j-1]+p[r]);
 
            }
        }
        printf("%d\n",dp[n][k]+p[1]);
    }
}
发布了150 篇原创文章 · 获赞 73 · 访问量 6565

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/104353729