HZAU 1199 Little Red Riding Hood【dp】

原创 2018年04月17日 07:50:53

  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

测试数据:

1

6 3

2 1 3 7 6 9

输出

11

题意:有n朵花,每朵花都有对应的价值,当摘了第i朵花,则<=k距离的花价值都会变为0,问,能够摘得的最大价值是多少。

思路:显然dp,考虑摘第i朵花的策略(摘或者不摘),就可以转化为只涉及前i-1朵花的问题,如果摘了第i朵花,那么问题就是计算出前i-k-1朵花的最大价值,如果没有摘第i朵花,那么问题就是计算出第i-1朵花的最大价值.

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 100010;
int vis[maxn],dp[maxn];
int main()
{
	int n,k,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		memset(dp,0,sizeof(dp));
		for(int i = 1; i <= n; i ++)
			scanf("%d",&vis[i]);
		for(int i = 1; i <= n; i ++)
		{
			if(i <= k)
				dp[i] = max(dp[i-1],vis[i]);
			else
				dp[i] = max(dp[i-1],dp[i-k-1]+vis[i]);
		}
		printf("%d\n",dp[n]);
	}
	return 0;
}


版权声明:转载的时候给我说一声哦~~~ https://blog.csdn.net/hello_sheep/article/details/79969092

华中农业大学第五届程序设计大赛 A Little Red Riding Hood [BIT优化dp]【动态规划】

题目链接:http://acm.hzau.edu.cn/problem.php?id=1199—————————————————————————————————————————— 1199: Lit...
  • qq_33184171
  • qq_33184171
  • 2017-05-26 21:02:33
  • 419

HZAU 1199 Little Red Riding Hood

1199: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MB Submit: 892  Solved: 144 [S...
  • a15110103117
  • a15110103117
  • 2017-04-25 09:57:01
  • 359

HZAU 1199 Little Red Riding Hood (dp)

Description Once upon a time, there was a little girl. Her name was Little Red Riding Hood. One da...
  • qq_28954601
  • qq_28954601
  • 2017-04-23 18:02:15
  • 470

HZAU 1199: Little Red Riding Hood 01背包

题目链接:1199: Little Red Riding Hood         思路:dp(i)表示前i朵花能取得的最大价值,每一朵花有两种选择,摘与不摘,摘了第i朵花后第i-k到i+k的花...
  • flyawayl
  • flyawayl
  • 2017-04-23 22:36:04
  • 453

HZAU 1199 Little Red Riding Hood(水DP)

1199: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MB Submit: 876  Solved: 139 [S...
  • u014665013
  • u014665013
  • 2017-04-24 18:33:04
  • 473

HZAU Little Red Riding Hood【动态规划】

Little Red Riding Hood 题目描述 Once upon a time, there was a little girl. Her name was Little Re...
  • k_young1997
  • k_young1997
  • 2018-04-17 16:30:18
  • 4

HZAU 网络赛 Little Red Riding Hood

1199: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MB Submit: 898  Solved: 149 [...
  • my_sunshine26
  • my_sunshine26
  • 2017-04-26 14:17:57
  • 107

Little Red Riding Hood

题目描述 Once upon a time, there was a little girl. Her name was Little Red Riding Hood. One day, h...
  • sb_Ihateyou
  • sb_Ihateyou
  • 2017-05-06 17:02:12
  • 2213

Picture This: How Pictures Work

  • 2014年07月23日 14:04
  • 1.26MB
  • 下载

划分数(动态规划(DP))

作者:码农场站主 链接:http://www.hankcs.com/program/m-n-recursive-division.html 原题 有n个无区别的物品,将它们划分为不超...
  • Zhao_Xinhao
  • Zhao_Xinhao
  • 2017-08-29 17:05:14
  • 249
收藏助手
不良信息举报
您举报文章:HZAU 1199 Little Red Riding Hood【dp】
举报原因:
原因补充:

(最多只允许输入30个字)