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 (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 (

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+;
const double eps = 1e-;
int T,n,w[N],sum[N<<],p[N<<],cnt,m,ret[N];
int k,a[N],pos[N],vis[N],dp[N][];
int main() {
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
memset(dp,,sizeof dp);
int ans=,max1=,max2=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
dp[i][]=max2;
dp[i][]=max1+a[i];
if(i-k>=)max1=max(max1,max(dp[i-k][],dp[i-k][]));
max2=max(dp[i][],dp[i][]);
//printf("!!!%d %d\n",max1,max2);
}
printf("%d\n",max(dp[n][],dp[n][]));
}
return ;
}
												

HZAU 1199 Little Red Riding Hood(DP)的更多相关文章

  1. HZAU 1199: Little Red Riding Hood 01背包

    题目链接:1199: Little Red Riding Hood 思路:dp(i)表示前i朵花能取得的最大价值,每一朵花有两种选择,摘与不摘,摘了第i朵花后第i-k到i+k的花全部枯萎,那么摘的话d ...

  2. hzau 1199 Little Red Riding Hood

    1199: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 918  Solved: 158[Submit ...

  3. Little Red Riding Hood

    问题 : Little Red Riding Hood 时间限制: 1 Sec  内存限制: 1280 MB 题目描述 Once upon a time, there was a little gir ...

  4. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  5. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  6. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  7. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

随机推荐

  1. MyBatis框架的使用及源码分析(十三) ResultSetHandler

    在PreparedStatementHandler中的query()方法中,用ResultSetHandler来完成结果集的映射. public <E> List<E> que ...

  2. Maven-Standard Directory Layout

    顶层工程描述文件: pom.xml. 此外, 还有一些供用户阅读以快速理解工程的文本性文档, 如: readme.txt, license.txt,等. 该结构下只有2个子目录, src和target ...

  3. html 制作静态页面新知识

    1.在区块线边框添加一条水平线 例如:<div  style:"height :300px;width:800px;border-bottom: solid 1px orange ;& ...

  4. 【BZOJ4868】期末考试 [三分][贪心]

    期末考试 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Samp ...

  5. 【BZOJ】4558: [JLoi2016]方

    [题意]给定有(n+1)*(m+1)个点的网格图,其中指定k个点不合法,求合法的正方形个数(四顶点合法). [算法]计数 [题解]斜着的正方形很麻烦,所以考虑每个斜正方形其外一定有正的外接正方形. 也 ...

  6. Bazinga(HDU5510+KMP)

    t题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题目: 题意:找到一个编号最大的字符串满足:存在一个编号比它小的字符串不是它的字串. 思路:K ...

  7. bzoj 1179 tarjan+spfa

    首先我们可以将这个图缩成DAG,那么问题中的路线就可以简化为DAG中的一条链,那么我们直接做一遍spfa就好了. 反思:开始写的bfs,结果bfs的时候没有更新最大值,而是直接赋的值,后来发现不能写b ...

  8. OnLoad & DOMReady

    window.onload 事件会在页面或图像加载完成后立即触发(即所有元素的资源都下载完毕).如果页面上有许多图片.音乐或falsh,onload事件会迟迟无法触发.所以出现了DOM Ready事件 ...

  9. 【转】ps命令详解

    原文地址:http://apps.hi.baidu.com/share/detail/32573968 有 时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程 ...

  10. Perl6 Bailador框架(8):自定义400/500

    第一种方法, 直接写在源码中: use Bailador; get '/' => sub { '<h1>hello, Bailador</h1>'; } get '/te ...