luogu1419 寻找段落 (二分,单调队列)

单调队列存坐标

#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("

----------

")
#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;

const int N = 100007;

int n, lenS, lenT;
int a[N];
double b[N], sum[N];
inline bool Check(double mid){ 
    R(i,1,n){
    	sum[i] = sum[i - 1] + (double)a[i] - mid;
    }
    int *q = new int[n + 3];
    int h = 1, t = 0;
    R(i,lenS,n){  
        while(h <= t && sum[i - lenS] < sum[q[t]]) --t;
        q[++t] = i - lenS;
        if(h <= t && q[h] < i - lenT) ++h;
        if(h <= t && sum[i] - sum[q[h]] >= 0){
        	delete []q;
        	return true;
        }
    }
    delete []q;
    return false;  
}  
int main(){
	io >> n >> lenS >> lenT;
	R(i,1,n){
		io >> a[i];
	}
	double l = -10000, r = 10000;
	while(r - l > 1e-6){
		double mid = (l + r) / 2.0;
		if(Check(mid))
			l = mid;
		else
			r = mid;
	}
	printf("%.3lf", l);

	return 0;
}
原文地址:https://www.cnblogs.com/bingoyes/p/11272272.html