Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接:
http://codeforces.com/contest/6/problem/E
E. Exposition
memory limit per test 64 megabytes
样例
sample input
3 3
14 12 10sample output
2 2
1 2
2 3
题意
求所有的最长连续子串,满足最大值最小值之差小于等于k。
题解
用单调队列维护一下(写成优先队列了。。orz)
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
int n,k;
int arr[maxn];
int main() {
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
priority_queue<pair<int,int> > qmax,qmin;
int l=1,r=1,sum=-1;
VPII ans;
while(r<=n){
qmax.push(mkp(arr[r],r));
qmin.push(mkp(-arr[r],r));
while(abs(qmax.top().X+qmin.top().X)>k){
l++;
while(qmax.top().Y<l) qmax.pop();
while(qmin.top().Y<l) qmin.pop();
}
if(r-l+1>sum){
sum=r-l+1;
ans.clear();
ans.push_back(mkp(l,r));
}else if(r-l+1==sum){
ans.push_back(mkp(l,r));
}
r++;
}
prf("%d %d\n",sum,ans.sz());
rep(i,0,ans.sz()){
printf("%d %d\n",ans[i].X,ans[i].Y);
}
return 0;
}
//end-----------------------------------------------------------------------