SSL_2884【Holidays】

Holiday

topic

After months of hard work, FJ decided to let the cows go on holiday. Holidays can be selected for a period of 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 be bored with 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

data range

p<=q<=n<=106


Parsing

The naked monotonic queue optimizes DP, first prefix the sum, kick off the end of the team >=a[ip] in each round, add ip, kick out the head of the <iq team, and find the maximum value.

code:

#include<cstdio>
#include<deque>
#define int long long
using namespace std;
inline int max(int x,int y){
    
    return (x>y)?x:y;}
inline bool idigit(char x){
    
    return (x<'0'|x>'9')?0:1;}
inline int read()
{
    
    
	int num=0,f=1;
	char c=0;
	while(!idigit(c=getchar())){
    
    if(c=='-')f=-1;}
	while(idigit(c))num=(num<<1)+(num<<3)+(c&15),c=getchar();
	return num*f;
}
inline void write(int x)
{
    
    
	int F[20];
	int tmp=x>0?x:-x;
	if(x<0)putchar('-');
	int cnt=0;
	while(tmp>0){
    
    F[cnt++]=tmp%10+'0';tmp/=10;}
	while(cnt>0)putchar(F[--cnt]);
	if(x==0)putchar('0');
}
int n,p,q,a[100010],maxn=-1e18;
deque <int> b;
signed main()
{
    
    
	n=read(),p=read(),q=read();
	for(int i=1;i<=n;i++)a[i]=read()+a[i-1];
	for(int i=p;i<=n;i++)
	{
    
    
		while(!b.empty()&&a[b.back()]>=a[i-p])b.pop_back();
		b.push_back(i-p);
		while(!b.empty()&&b.front()<i-q)b.pop_front();
		maxn=max(maxn,a[i]-a[b.front()]);
	}
	write(maxn);
	return 0;
}

Guess you like

Origin blog.csdn.net/zhanglili1597895/article/details/114407004