E. Exposition
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/problemset/problem/6/E
Description
The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.
Input
The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.
Output
In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.
In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.
Sample Input
3 3
14 12 10
Sample Output
2 2
1 2
2 3
HINT
题意
给你一堆数,让你找到最长的区间,使得这个区间里面的最大值减去最小值不超过K,然后让你输出每一个区间的起始位置和结束为止
题解:
类似于双端队列的写法,我们用multiset来处理这个问题,如果当前区间不合法,那么我们不断删去起始端的数就好了
然后不断跑,O(n)
代码
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1000010 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; //const int inf=0x7fffffff; const int inf=0x3f3f3f3f; /* inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } */ inline ll read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** int a[maxn]; vector< pair<int,int> > q; multiset<int> s; int main() { int n=read(),k=read(); for(int i=0;i<n;i++) a[i]=read(); int j=0; int aa=-1; for(int i=0;i<n;i++) { s.insert(a[i]); while(*s.rbegin()-*s.begin()>k) s.erase(s.find(a[j++])); if(i-j+1>aa) { aa=i-j+1; q.clear(); } if(i-j+1==aa) q.push_back(make_pair(j+1,i+1)); } printf("%d %d ",aa,q.size()); for(int i=0;i<q.size();i++) printf("%d %d ",q[i].first,q[i].second); }