Little Red Riding Hood

题目链接:http://acm.hzau.edu.cn/problem.php?cid=1029&pid=0

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

分析:这是一道动态规划,只不过我学得不太好,当时没找到转移状态方程,后来是网上找的题解,    后面和01背包很像
#include<stdio.h>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,k;
        long long a[100010];
        long long dp[100010];
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        dp[0]=0;
        int i;
        for(i=1;i<=k;i++)
            dp[i]=max(dp[i-1],a[i]);
        for(;i<=n;i++)
            dp[i]=max(dp[i-1],a[i]+dp[i-k-1]);
        printf("%I64d\n",dp[n]);
    }
    return 0;
}
 
时间: 2024-08-10 19:09:35

Little Red Riding Hood的相关文章

华中农业大学第五届程序设计大赛 (7/12)

今天实在累了,还有的题晚点补.... 题目链接:http://acm.hzau.edu.cn/problemset.php?page=3 题目:acm.hzau.edu.cn/5th.pdf A:Little Red Riding Hood 题意:给你n朵花,每朵花有个权值,然后每次取花最少要间隔k朵,求权值最大: 思路:简单dp: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream&

Linux常用 bash

学会Linux常用 bash命令 目录 基本操作1.1. 文件操作1.2. 文本操作1.3. 目录操作1.4. SSH, 系统信息 & 网络操作 基本 Shell 编程2.1. 变量2.2. 字符串替换2.3. 函数2.4. 条件2.5. 循环 技巧 调试 1. Basic Operations a. export 显示所有的环境变量,如果你想获取某个变量的详细信息,使用 echo $VARIABLE_NAME. export Example: $ export SHELL=/bin/zsh A

华中农业大学第五届程序设计大赛网络同步赛解题报告(转)

A.Little Red Riding Hood B.Choosy in Food •F[i]:从第I个点到终点的期望步数 •F[i] = (F[i + k] + k ) * P[k] •F[ed] = 0 •高斯消元求解 •注意存在从起点不能到达终点的情况 C.Friends •F[rt][len] :节点rt的子孙里,距离rt的为len的节点个数 •ans[rt][len] : 整棵树上,距离rt为len的节点个数 •F值一次简单的深搜可以得到 •而ans[rt][len] = F[rt][

华中农业大学第五届程序设计大赛网络同步赛题解

A.Little Red Riding Hood B.Choosy in Food •F[i]:从第I个点到终点的期望步数 •F[i] = (F[i + k] + k ) * P[k] •F[ed] = 0 •高斯消元求解 •注意存在从起点不能到达终点的情况 C.Friends •F[rt][len] :节点rt的子孙里,距离rt的为len的节点个数 •ans[rt][len] : 整棵树上,距离rt为len的节点个数 •F值一次简单的深搜可以得到 •而ans[rt][len] = F[rt][

Python修炼之路第二步:Python的基石

Python作为最棒的计算机程序设计语言,构成Python的庞大体系的零部件就起到了基石的作用.今天就介绍其中的几种: 1.变量 2.列表 3.元组 4.字典 5数据运算 1. 变量: 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问. 那么我们的第一步就是声明一个变量: first_script = 'Hello World' print (first_script) >>>Hello World 第一个脚本 如代码块中所示,我们需要的是将一个值赋

iOS开发学习之#提醒处理#(2)响应动作表单

在动作表单中我们用很多按钮实现,在这里我们用到了UIActionsheetDelegate协议中的actionSheet:clickedButtonAtIndex:方法实现,其语法形式如下: - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex; 其中,(UIActionSheet*)actionSheet 用来指定动作表单中包含的按钮,(NSInteger)butt

Django Model 定义语法

Django Model 定义语法 版本:1.7主要来源:https://docs.djangoproject.com/en/1.7/topics/db/models/ 简单用法 from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) 会自动生成SQL: CREA

bash guide

Table of Contents Basic Operations 1.1. File Operations 1.2. Text Operations 1.3. Directory Operations 1.4. SSH, System Info & Network Operations 1.5. Process Monitoring Operations Basic Shell Programming 2.1. Variables 2.2. Array 2.3. String Substit

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版