http://codeforces.com/contest/6/problem/E
直接做不论怎么优化都超时,重新捋了一下思路,发现只许维护当前上升序列和就行,用multiset自动排序之后很容易实现
set.rbegin()是set中的最后一个元素,一开始以为是s.end()debug了半天,难受.jpg
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long
int main()
{
int n, k;
int a[100005];
scanf("%d%d",&n,&k);
multiset<int> s;
int ans[100005];
bool vis[100005];
int pos = 0;
int maxy = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%d",&a[i]);
s.insert(a[i]);
vis[i] = 0;
while(*s.rbegin() - *s.begin() > k)
{
s.erase(s.find(a[++ pos]));
ans[pos] = i - pos;
vis[pos] = 1;
maxy = max(maxy, ans[pos]);
}
}
queue<int> q;
int cnt = 0;
for(int i = 1; i <= n; i ++)
{
if(! vis[i])
{
ans[i] = n - i + 1;
maxy = max(maxy, ans[i]);
}
}
for(int i = 1; i <= n; i ++)
{
if(ans[i] == maxy)
{
cnt ++;
q.push(i);
}
}
printf("%d %d\n",maxy, cnt);
while(! q.empty())
{
printf("%d %d\n",q.front(), q.front() + maxy - 1);
q.pop();
}
return 0;
}