SSLOJ2884 假期

Description

经过几个月辛勤的工作,FJ决定让奶牛放假。假期可以在1…N天内任意选择一段(需要连续),每一天都有一个享受指数W。但是奶牛的要求非常苛刻,假期不能短于P天,否则奶牛不能得到足够的休息;假期也不能超过Q天,否则奶牛会玩的腻烦。FJ想知道奶牛们能获得的最大享受指数。

Input

第一行:N,P,Q.

第二行:N个数字,中间用一个空格隔开,每个数都在longint范围内。

Output

一个整数,奶牛们能获得的最大享受指数。

思路

可以通过前缀和的方式把暴力变成单调队列。
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;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/113953406
今日推荐