zoukankan      html  css  js  c++  java
  • hdu2182Frog(动态规划)

    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

    无脑的dpAC代码:

    include

    include

    using namespace std;
    int main()
    {
    int t;
    cin>>t;
    while(t--)
    {
    int N,A,B,K;
    cin>>N>>A>>B>>K;
    int a[120]={0};
    for(int i=0;i<N;i++)
    cin>>a[i];
    int dp[120][120]; //dp[i][k]表示在第i+1个位置的第k次跳后的最大值
    memset(dp,-1,sizeof(dp));
    dp[0][0]=a[0];
    for(int i=0;i<N;i++)
    {
    for(int j=A;j<=B;j++)
    {
    for(int k=0;k<=max(K,101);k++)
    {
    if(dp[i][k]!=-1&&i+j<N) dp[i+j][k+1]=max(dp[i][k]+a[i+j],dp[i+j][k+1]);
    else continue;
    }
    }
    }
    int max1=0;
    for(int i=0;i<N;i++)
    {
    for(int j=1;j<=K;j++)
    {
    if(dp[i][j]>max1) max1=dp[i][j];
    }
    }
    cout<<max1<<endl;
    }
    return 0;
    }

    但凡使用dp的,稍微有点难度的基本都会用到max()


    作者:孙建钊
    出处:http://www.cnblogs.com/sunjianzhao/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    matlab 自定义函数及调用
    matlab 逻辑数组及其应用
    matlab 嵌套循环的学习
    matlab和C语言的break及continue测试
    matlab 向量法建数组(推荐)
    matlab for循环应用(阶乘及the day of year)
    matlab for循环的三种类型
    ORACLE创建用户,建表空间,授予权限
    Linux计划任务
    android开发环境搭建
  • 原文地址:https://www.cnblogs.com/sunjianzhao/p/11383585.html
Copyright © 2011-2022 走看看