[Luogu P3512] [BZOJ 2096] [POI2010]PIL-Pilots

探讨了在给定序列中寻找最长子序列的问题,其中子序列的最大值和最小值之差不超过预设值。使用两个单调队列分别维护最大值和最小值,实现了高效的解题策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷传送门
BZOJ传送门

题目大意

给定 n , k n,k n,k和一个长度为 n n n的序列,求最长的最大值最小值相差不超过 k k k的序列

输入输出格式

输入格式:

第一行两个有空格隔开的整数 k k k 0 ≤ k ≤ 2000 , 000 , 000 0\le k\le 2000,000,000 0k2000,000,000), n ( 1 ≤ n ≤ 3000 , 000 ) n(1\le n\le 3000,000) n(1n3000,000) k k k代表设定的最大值, n n n代表序列的长度。第二行为 n n n个由空格隔开的整数 a i a_i ai 1 ≤ a i ≤ 2000 , 000 , 000 1\le a_i\le 2000,000,000 1ai2000,000,000),表示序列。

输出格式:

Your program should print a single integer to the standard output:

the maximum length of a fragment of the flight that is within the given tolerance level.

一个整数代表最大的符合条件的序列

输入输出样例

输入样例#1:
3 9
5 1 3 5 8 6 6 9 10
输出样例#1:
4

说明

样例解释: 5 , 8 , 6 , 6 5, 8, 6, 6 5,8,6,6 8 , 6 , 6 , 9 8, 6, 6, 9 8,6,6,9都是满足条件长度为 4 4 4的序列

解题分析

两个单调队列, 一个维护最小值, 一个维护最大值, 即可 A C \mathcal{AC} AC本题。

代码如下:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <cmath>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 3000500
template <class T>
IN void in(T &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc);
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
}
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
int dat[MX], mn[MX], mx[MX];
int l, r, lmin, rmin, lmax, rmax, len, lim, ans;
int main(void)
{
	in(lim), in(len);
	for (R int i = 1; i <= len; ++i) in(dat[i]);
	l = lmin = lmax = 1;
	for (R int i = 1; i <= len; ++i)
	{
		++r;
		W (lmin <= rmin && dat[mn[rmin]] > dat[r]) --rmin;
		W (lmax <= rmax && dat[mx[rmax]] < dat[r]) --rmax;
		mn[++rmin] = mx[++rmax] = r;
		W (dat[mx[lmax]] - dat[mn[lmin]] > lim)
		{
			++l;
			if (mn[lmin] < l) ++lmin;
			if (mx[lmax] < l) ++lmax;
		}
		ans = max(ans, r - l + 1);
	}
	printf("%d", ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值