Frog
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 919 Accepted Submission(s): 440
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
Source
题目大意:初始的时候青蛙在位置1,能够跳k步,每步的间隔最小为A,最大为B。每到一个点,都能吃掉这个点上的蚊子,问最大吃蚊子的数量。
思路:
简单dp
1、设dp【i】【j】表示跳到i上,跳跃了j次的最大吃蚊子的数量。
2、状态转移方程:
dp【i】【j】=max(dp【i】【j】,dp【Q】【j-1】+a【i】){i-a=>Q >=i-b&&Q>=0&&Q<n}
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int aa[1000];
int dp[1000][1000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
for(int i=0;i<n;i++)
{
scanf("%d",&aa[i]);
}
if(k>100)k=101;
memset(dp,0,sizeof(dp));
dp[0][0]=aa[0];
for(int i=1;i<n;i++)
{
for(int j=i-b;j<=i-a;j++)
{
if(j<0)continue;
for(int l=1;l<=k;l++)
dp[i][l]=max(dp[j][l-1]+aa[i],dp[i][l]);
}
}
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<=k;j++)
{
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
}