HZAU 1199 Little Red Riding Hood【dp】

本文介绍了一个基于动态规划算法解决的小红帽摘花问题。故事背景为小红帽在前往奶奶家的路上摘花,但摘下花朵后周围花朵会枯萎。目标是通过动态规划算法找出最大价值的摘花方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值