hdu 2182 Frog

Frog

http://acm.hdu.edu.cn/showproblem.php?pid=2182

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

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
ac了代码
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
    int t,n,a,b,sum,max1,i,j,k;
    int p[110],q[110][110];
    cin>>t;
    while(t--)
    {
        cin>>n>>a>>b>>sum;
        for(i=0;i<n;i++)
            cin>>p[i];
        memset(q,0,sizeof(q));//q[i][j]储存第i步走到j吃虫子的数量,
                              //如果走不到j位置就为0
        max1=p[0];
        for(i=a;i<=b;i++)//走第一步
        {
            q[1][i]=p[i]+p[0];
             if(max1<q[1][i])
                    max1=q[1][i];
        }
        for(i=2;i<=sum;i++)//走第i步
        {
             for(j=0;j<n;j++)
             if(q[i-1][j])//第i-1步j位置是否为0,为0则说明i-1步走不到j
             {
                 for(k=a;k<=b&&k+j<n;k++)
                 {
                    int t=max(q[i-1][j+k],q[i-1][j]+p[j+k]);
                    t=max(t,q[i][j+k]);//t储存q[i-1][j+k],q[i][j+k],q[i-1][j]+p[j+k]最大值
                    q[i][j+k]=t;
                    if(max1<q[i][k+j])
                        max1=q[i][k+j];
               }
            }
        }
        cout<<max1<<endl;
    }
}
View Code

一直wa的代码

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
  int t,n,a,b,k,p[102],q[102][102],i,j,max;
  cin>>t;
  while(t--)
  {
      max=0;
      cin>>n>>a>>b>>k;
      for(i=0;i<n;i++)
        cin>>p[i];
       memset(q,0,sizeof(q));
       q[0][0]=p[0];
       max=p[0];
      for(i=1;i<=k;i++)
      {
          for(j=0;j<n;j++)
          {
              if(q[i-1][j]!=0)
              {
                  for(int s=a;s<=b&&s+j<n;s++)
                    {
                        q[i][s+j]=q[i-1][s+j]>q[i-1][j]+p[s+j]?q[i-1][s+j]:q[i-1][j]+p[s+j];
                        if(q[i][s+j]>max)
                           max=q[i][s+j];
                    }
              }
          }
    
      }
      cout<<max<<endl;
  }
}
View Code

 

posted on 2013-07-29 16:26  天凉了  阅读(255)  评论(0编辑  收藏  举报

导航