• HZAU 1199 Little Red Riding Hood(水DP)


    1199: Little Red Riding Hood

    Time Limit: 1 Sec  Memory Limit: 1280 MB
    Submit: 876  Solved: 139
    [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 (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)

    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
    

    HINT

    Source



    题意:给你n朵花,每朵花有个权值,然后每次取花最少要间隔k朵,求权值最大;

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 1e5 + 10;
    long long dp[MAXN];
    long long a[MAXN];
    
    int main()
    {
        int T;
        int n, k;
        int i;
        long long ans;
    
        scanf("%d", &T);
    
        while (T--) {
            scanf("%d%d", &n, &k);
            for (i = 1; i <= n; ++i) {
                scanf("%lld", &a[i]);
            }
            memset(dp, 0, sizeof(dp));
            ans = 0;
            for (i = 1; i <= k; ++i) {
                dp[i] = max(dp[i - 1], a[i]);//dp[i - 1] > a[i] ? dp[i - 1] : a[i];
                if (dp[i] > ans) {
                    ans = dp[i];
                }
            }
            for (i = k + 1; i <= n; ++i) {
                dp[i] = max(dp[i - 1], dp[i - k - 1] + a[i]);
                if (dp[i] > ans) {
                    ans = dp[i];
                }
            }
            printf("%lld
    ", ans);
        }
    
        return 0;
    }
    
    


  • 相关阅读:
    CentOS配置启动ssh与开机自启
    CentOS中怎样安装、配置、启动Nginx
    CentOS中配置Mysql表名忽略大小写以及提示:Caused by: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock的解决
    CentOS中部署jar包时提示:org.quartz.SchedulerException: Couldn't get host name
    CentOS6中怎样将jdk1.7升级到1.8
    CentOS6在使用yum install 时提示镜像源路径不存在:PYCURL ERROR 22
    信息系统项目管理师-项目立项管理考点笔记
    chrome89不再支持/deep/的解决方案
    手写async await
    proxy和reflect
  • 原文地址:https://www.cnblogs.com/zswbky/p/6792890.html
Copyright © 2020-2023  润新知