2096: [Poi2010]Pilots

2096: [Poi2010]Pilots

Time Limit: 30 Sec   Memory Limit: 162 MB
Submit: 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

Sample Output

4
(有两个子串的长度为4: 5, 8, 6, 6 和8, 6, 6, 9.最长子串的长度就是4)

HINT

Source

[ Submit][ Status][ Discuss]



显然,,具有决策单调性(水经验。。。)

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值