SSLOJ2884 Holiday

Description

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.

Ideas

The violence can be turned into a monotonous queue by means of prefix and sum.
code:

#include<iostream>
#include<cstdio>
#include<deque>
using namespace std;
long long n,p,q,s[100001],x,mx=-2147483647;
deque<int> o;
int main()
{
    
    
 cin>>n>>p>>q;
 for (int i=1;i<=n;i++)
 {
    
    
  cin>>x;
  s[i]=s[i-1]+x;
 }
 for (int i=p;i<=n;i++)
 {
    
    
  while (o.size()&&s[o.back()]>=s[i-p]) o.pop_back();
  o.push_back(i-p);
  while (o.size()&&o.front()<i-q)
  {
    
    
   o.pop_front();
  }
  mx=max(mx,s[i]-s[o.front()]);
 }
 cout<<mx;
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_49843717/article/details/113953406