Frog
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
解题思路:
二维DP,先枚举步数,从1-k,然后枚举位置从1-n,最后枚举上一个的位置。
那么每一个位置的状态,都是从上一步的位置跳过来的,枚举这些位置,择优录取,保存最大值。需要注意的是站在起点处就已经把起点的昆虫吃了,而且不计步数。
dp[i][j] 表示第i步,第j个位置时吃的昆虫总数量。
转移方程:dp[i][j] = max(dp[i][j],dp[i-1][k]+a[j])
AC代码:
#include <iostream>
#include <math.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define int long long
using namespace std;
int a[150];
int dp[120][120];
signed main()
{
int n,m,a1,b,k;
cin>>n;
while(n--)
{
cin>>m>>a1>>b>>k;
for(int i = 0 ; i < m ; i ++) cin>>a[i];
memset(dp,-1,sizeof(dp));
dp[0][0] = a[0];
for(int i = 1 ; i <= k ; i ++) // 枚举步数
for(int j = 1 ; j < m ; j ++) // 枚举位置
for(int l = max(j-b,(int)0) ; l <= j-a1 ; l ++) // 枚举上一步的位置
{
if(dp[i-1][l] != -1)
dp[i][j] = max(dp[i][j],dp[i-1][l]+a[j]);
}
int ma = dp[0][0];
for(int i = 1 ; i <= k ; i ++)
for(int j = 0 ; j < m ; j ++)
ma = max(ma,dp[i][j]);
cout<<ma<<endl;
}
}