对,没错就是单调队列。
我第一次用priority_queue实现单调队列(其实差不多)
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<cctype> #include<cassert> #include<climits> #include<queue> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,n) for(int i=n;i;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define RepD(i,n) for(int i=n;i>=0;i--) #define MEM(a) memset(a,0,sizeof(a)) #define MEMI(a) memset(a,127,sizeof(a)) #define MEMi(a) memset(a,128,sizeof(a)) #define INF (2139062143) #define F (1000000009) #define MAXN (3000000+10) #define MAXK (2000000000) typedef long long ll; int n; ll k,a[MAXN]; struct node { int v,i; node(){} node(int _i,int _v):v(_v),i(_i){} friend bool operator<(node a,node b){return a.v<b.v;}; }; priority_queue<node> h1,h2; int main() { // freopen("bzoj2096.in","r",stdin); cin>>k>>n; int l=1,ans=0; For(i,n) { scanf("%lld",&a[i]); h1.push(node(i,a[i])),h2.push(node(i,-a[i])); while (h1.top().v+h2.top().v>k) { l++; while (!h1.empty()&&h1.top().i<l) h1.pop(); while (!h2.empty()&&h2.top().i<l) h2.pop(); } ans=max(ans,i-l+1); // cout<<h1.top()<<' '<<h2.top()<<endl; } cout<<ans<<endl; // while(1); return 0; }