P2884 Holiday

P2884 Holiday

Description

After months of hard work, FJ decided to let the cows go on holiday. Holidays can be selected for a period of time within 1...N days (need to be continuous), and each day has an enjoyment index W. However, the requirements of dairy cows are very demanding. The holidays cannot be shorter than P days, otherwise the cows cannot get enough rest; the holidays cannot exceed Q days, otherwise the cows will get bored of playing. FJ wants to know the maximum enjoyment index that cows can get.

Input

The first line: N, P, Q. The
second line: N numbers, separated by a space, each number is in the range of longint.

Output

An integer, the maximum enjoyment index that cows can get.

Sample Input

5 2 4                            
-9 -4 -3 8 -6

Sample Output

5

Hint
50% 1≤N≤10000
100% 1≤N≤100000
1<=p<=q<=n
Hint
chooses day 3-4, enjoyment index is -3+8=5.

Analysis:
Seeing the question of the interval, we must first think of the sum of the prefixes. We record the sum of [1,k] as sum[k], we can get sum[i] = sum[i-1] + a[i], The sum of [l,r] is sum[r]-sum[l-1] (here sum[0] = 0). We assume that the selected interval is [l,r] and r is fixed, we know that r−q+1≤l≤r−p+1, if we want to maximize the value of the interval [l,r], then sum[l-1] The minimum is required to make sum[r]-sum[l-1] the smallest. When i is shifted one bit to the right to i+1, because p and q are given constant values, the minimum sum[l-1] ] The interval is also shifted to the right.
As shown in the figure below, when p=3, q=5:
Insert picture description here

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6;
long long n,p,q,a[N+10],qzh[N+10],head=1,tail=0,maxn=-2147483647,que[N+10];
int main()
{
    
    
	scanf("%lld%lld%lld",&n,&p,&q);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]),
		qzh[i]=qzh[i-1]+a[i];
	for(int i=p;i<=n;i++)
	{
    
    
		while(head<=tail&&qzh[que[tail]]>=qzh[i-p]) tail--;
		que[++tail]=i-p;
		while(head<=tail&&i-q>que[head]) head++;
		maxn=max(maxn,qzh[i]-qzh[que[head]]);
	}
	printf("%lld",maxn);
	return 0;
}


Guess you like

Origin blog.csdn.net/bigwinner888/article/details/107949948