Luogu3594 [POI2015]WIL-Wilcze doły (双端队列)

单调性显然,双端队列队列维护严格单调递减手写双端队列真的可恶

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;
//#include <queue>

const int N = 2000007;

int a[N];
long long sum[N], x[N];
//deque<int> q;
int q[N], t, h = 1;
int main(){
//FileOpen();
	int n, d;
	long long Val;
	io >> n >> Val >> d; 
    R(i,1,n){
    	io >> a[i];
    	sum[i] = sum[i - 1] + a[i];
    }
   	R(i,d,n){
   		x[i] = sum[i] - sum[i - d];
   	} 
	int ans = d;
	q[++t] = d;
	int l = 1;
	R(r,d,n){
		while(h <= t && x[r] >= x[q[t]]) --t;
		q[++t] = r;
		while(h <= t && q[h] - d + 1< l) ++h;
		while(h <= t && sum[r] - sum[l - 1] - x[q[h]] > Val){
			++l;
			while(h <= t && q[h] - d + 1 < l) ++h;
		}
		ans = Max(ans, r - l + 1);
	}
    printf("%d", ans);
    return 0;
}

posted @ 2019-07-27 10:08  邱涵的秘密基地  阅读(119)  评论(0编辑  收藏  举报