2096: [Poi2010]Pilots
Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 746 Solved: 382
[ Submit][ Status][ Discuss]
Description
Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值。耍畸形一个人是不行的,于是他找到了你。
Input
输入:第一行两个有空格隔开的整数k(0<=k<=2000,000,000),n(1<=n<=3000,000),k代表Tz设定的最大值,n代表难度序列的长度。第二行为n个由空格隔开的整数ai(1<=ai<=2000,000,000),表示难度序列。
Output
输出:最大的字串长度。
Sample Input
3 9
5 1 3 5 8 6 6 9 10
5 1 3 5 8 6 6 9 10
Sample Output
4
(有两个子串的长度为4: 5, 8, 6, 6 和8, 6, 6, 9.最长子串的长度就是4)
(有两个子串的长度为4: 5, 8, 6, 6 和8, 6, 6, 9.最长子串的长度就是4)
HINT
Source
显然,,具有决策单调性(水经验。。。)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 3E6 + 30;
int n,k,h1,t1,h2,t2,A[maxn],q1[maxn],q2[maxn];
bool Judge(int R)
{
if (h1 > t1 || h2 > t2) return 1;
int Max = h1 <= t1?max(A[q1[h1]],A[R]):A[R];
int Min = h2 <= t2?min(A[q2[h2]],A[R]):A[R];
return Max - Min <= k;
}
void Insert(int R)
{
while (h1 <= t1 && A[q1[t1]] <= A[R]) --t1;
while (h2 <= t2 && A[q2[t2]] >= A[R]) --t2;
q1[++t1] = q2[++t2] = R;
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
k = getint(); n = getint();
for (int i = 1; i <= n; i++) A[i] = getint();
int Ans,R; Ans = R = 0; h1 = h2 = 1;
for (int i = 1; i <= n; i++)
{
while (h1 <= t1 && q1[h1] < i) ++h1;
while (h2 <= t2 && q2[h2] < i) ++h2;
while (R < n && Judge(R + 1)) Insert(++R);
Ans = max(Ans,R - i + 1);
}
cout << Ans;
return 0;
}