Little Red Riding Hood(动态规划)

题目:

 Little Red Riding Hood


题目描述

    Once upon a time, there was a little girl. Her name was Little Red Riding Hood. One day, her grandma was ill. Little Red Riding Hood went to visit her. On the way, she met a big wolf. “That's a good idea.”,the big wolf thought. And he said to the Little Red Riding Hood, “Little Red Riding Hood, the flowers are so beautiful. Why not pick some to your grandma?” “Why didn't I think of that? Thank you.” Little Red Riding Hood said.

    Then Little Red Riding Hood went to the grove to pick flowers. There were n flowers, each flower had a beauty degree a[i]. These flowers arrayed one by one in a row. The magic was that after Little Red Riding Hood pick a flower, the flowers which were exactly or less than d distances to it are quickly wither and fall, in other words, the beauty degrees of those flowers changed to zero. Little Red Riding Hood was very smart, and soon she took the most beautiful flowers to her grandma’s house, although she didn’t know the big wolf was waiting for her. Do you know the sum of beauty degrees of those flowers which Little Red Riding Hood pick? 

输入

    The first line input a positive integer T (1≤T≤100), indicates the number of test cases. Next, each test case occupies two lines. The first line of them input two positive integer n and

k (2 <= n <= 10^5 ) ,1 <=  k <= n ), the second line of them input n positive integers a (1<=a <=10^5)

输出

    Each group of outputs occupies one line and there are one number indicates the sum of the largest beauty degrees of flowers Little Red Riding Hood can pick. 

样例输入

1 
3 1 
2 1 3

样例输出

5

题目描述:

动态规划题,设dp[i]为到拿取第i个位置的最大价值,推出转移方程即可。

代码:

#include<stdio.h>
#include<string.h>
int a,b,aa[100010],c[100010],d[100010],f[100010];
int main()
{
    int i,j,k;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&a,&b);
        memset(aa,0,sizeof(aa));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(f,0,sizeof(f));
        int max = 0,max1 = 0;
        for(i = 1; i <= a;i ++){
            scanf("%d",&aa[i]);
            if(i -b-1 >= 1){
                if(max < aa[i -b-1]){
                    max = aa[i -b-1];
                }
            }
            c[i] = max;
            if(i - b >= 1){
                if(max1 < aa[i - 1]){
                    max1 = aa[i - 1];
                }
            }
            d[i] = max1;
//          printf("%d %d\n",c[i],d[i]);
        }
        f[1] = aa[1];
        int ma=0,ma1=0;
        for(i = 1;i <=a;i++){
            if(i-b-1>=1)
                if(ma<f[i-b-1])
                    ma = f[i-b-1];
//          ma1 = 0;
//          if(i-b>=1){
//              for(j=i;j<i+b;j++)
//                  if(ma1<f[j])
//                      ma1 = f[j];
//          }
            f[i] = ma+aa[i]>f[i-1]?ma+aa[i]:f[i-1];
//          printf("!%d\n",f[i]);
        }
        printf("%d\n",f[a]);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/hunt_er/article/details/80244705
RED
今日推荐