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

2017-05-26 21:02:33 Tabris_ 阅读数:596


博客爬取于2020-06-14 22:40:38
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/72773556


题目链接:http://acm.hzau.edu.cn/problem.php?id=1199

——————————————————————————————————————————
1199: Little Red Riding Hood
Time Limit: 1 Sec Memory Limit: 1280 MB
Submit: 937 Solved: 166
[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 (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

———————————————————————————————————————————
题目大意:
就是一个序列,拿取的最大价值,拿取一个后,半径为k的位置价值都变为0了。

解题思路:

很明显更的dp

设dp[i]为到拿取第i个位置的最大价值,

转移就是

dp[i]=a[i]+max{dp[j]  j<ik}dp[i]=a[i]+max\Big\{dp[j]\ \Big|\ j<i-k\Big\}

由于最大值是前缀最大值,所以用一个树状数组维护就好了

附本题代码
———————————————————————————————————————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N = 2e5+7;
//const int INF = (~(1<<31));

int read(){
int x=0,f=1;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch = getchar();}
return x;
}
/*******************************************/

int a[N];
int n,k;

LL mx[N];
# define lowbit(x) (x&-x)
void update(int i,LL v){
for(;i<=n;i+=lowbit(i)) mx[i]=max(mx[i],v);
}

LL getMax(int i){
if(i<=0) return 0;
LL ans = 0;
for(;i;i-=lowbit(i)) ans = max(ans,mx[i]);
return ans;
}

int main(){//printf("%d\n",INF);
int _;
scanf("%d",&_);
while(_--){memset(mx,0,sizeof(mx));
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) a[i]=read();

LL ans = 0,tmp;
for(int i=1;i<=n;i++){
tmp=a[i]+getMax(i-k-1);
update(i,tmp);
ans = max(ans,tmp);
}
printf("%d\n",ans);
}
return 0;
}