• HZAU 1199 Little Red Riding Hood(DP)


    Little Red Riding Hood

    Time Limit: 1 Sec  Memory Limit: 1280 MB
    Submit: 853  Solved: 129
    [Submit][Status][Web Board]

    Description

    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? 

    Input

    The first line input a positive integer T (1T100), 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 (

    Output

     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. 

    Sample Input

    1 
    3 1 
    2 1 3
    

    Sample Output

    5
    【分析】给你一个数组,然后让你从中选出一些数,使得和最大,但是当你选了一个数,距离这个数长度为 K 的数都会变为0,问
    你最终选的数的最大和。
    dp[i][0,1]表示不选当前数或选当前数的最大值。然后维护两个最大值max1:1~i-k 的最大值;max2:1~i的最大值,那么
    dp[i][1]=max1+a[i];
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <cstdlib>
    #include <iostream>
    #include <map>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int>pii;
    const int N = 1e5+5;
    const double eps = 1e-8;
    int T,n,w[N],sum[N<<2],p[N<<2],cnt,m,ret[N];
    int k,a[N],pos[N],vis[N],dp[N][2];
    int main() {
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&k);
            memset(dp,0,sizeof dp);
            int ans=0,max1=0,max2=0;
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            for(int i=1;i<=n;i++){
                dp[i][0]=max2;
                dp[i][1]=max1+a[i];
                if(i-k>=1)max1=max(max1,max(dp[i-k][1],dp[i-k][0]));
                max2=max(dp[i][0],dp[i][1]);
                //printf("!!!%d %d
    ",max1,max2);
            }
            printf("%d
    ",max(dp[n][0],dp[n][1]));
        }
       return 0;
    }


  • 相关阅读:
    【Todo】Java线程面试题 Top 50 (转载)
    【Todo】秒杀系统 & 乐观锁 & Nginx反向代理
    【Todo】C++和Java里面的浮点数及各种数字表示
    asp.net操作word的表格
    Android消息推送(二)--基于MQTT协议实现的推送功能
    单点更新线段树 RMQ
    英语月结
    AppWidget应用(二)---PendingIntent 之 getActivity
    2 WAN 和1 Evo/3g Routeros PCC 方法负载平衡
    Android 网络权限配置
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6754522.html
Copyright © 2020-2023  润新知