HZAU 1199 Little Red Riding Hood (dp)
来源:互联网 发布:iphone7p淘宝店铺 编辑:程序博客网 时间:2024/06/17 08:32
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 ≤ 105,1 ≤ k < n), the second line of them input n positive integers a (1 ≤ a ≤ 105).
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
题意
小红帽去采花,每朵花都有它的 degree
,每采一朵其周边范围 degree
和。
思路
则有状态转移方程:
AC 代码
#include <iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 1000007typedef unsigned long long LL;LL dp[110000][2];int a[110000];int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--) { int n,k; cin>>n>>k; for(int i=0; i<n; i++) cin>>a[i]; memset(dp,0,sizeof(dp)); dp[0][1]=a[0]; dp[0][0]=0; for(int i=1; i<n; i++) { dp[i][0]=max(dp[i-1][1],dp[i-1][0]); int r=i-k-1; if(r<0)dp[i][1]=a[i]; else dp[i][1]=max(dp[r][0],dp[r][1])+a[i]; } cout<<max(dp[n-1][0],dp[n-1][1])<<endl; } return 0;}
- HZAU 1199 Little Red Riding Hood (dp)
- HZAU 1199 Little Red Riding Hood(水DP)
- HZAU 1199 Little Red Riding Hood
- HZAU 1199: Little Red Riding Hood 01背包
- HZAU 网络赛 Little Red Riding Hood
- hazu A. Little Red Riding Hood (DP)@
- Little Red Riding Hood
- Problem A: Little Red Riding Hood----普通dp
- HZAU_1199_Little Red Riding Hood(DP 背包)
- 湖北民族学院oj 1877 之 Little Red Riding Hood(dp)
- HAU校赛Little Red Riding Hood
- 华中农业大学第五届程序设计大赛 A Little Red Riding Hood [BIT优化dp]【动态规划】
- HZAU校赛F题 LCS (dp)
- HZAU--21--Arithmetic Sequence(二维dp)
- HZAU 1201 Friends(树形dp)
- HZAU 1201 Friends(树形DP 待整理)
- HZAU 1019 Arithmetic Sequence [DP]
- ZOJ 2604 Little Brackets(高精度+dp)
- opencv mat数据剪裁感兴趣的部分处理方法
- LED闪烁
- 创业,合伙人很重要
- 快速排序
- JS基础知识
- HZAU 1199 Little Red Riding Hood (dp)
- opencv从零开始——2. ROI与线性融合
- dubbo 配置文件详解
- 软件设计发展路线
- 初试C++单向链表
- static
- 二叉树的做题总结
- Xutils3.0网络带缓存的封装
- 【leetcode】链表的基本操作