• 洛谷P3512 [POI2010]PIL-Pilots


    传送门

    题目描述

    In the Byteotian Training Centre, the pilots prepare for missions requiring extraordinary precision and control.

    One measure of a pilot’s capability is the duration he is able to fly along a desired route without deviating too much - simply put, whether he can fly steadily. This is not an easy task, as the simulator is so sensitive that it registers even a slightest move of the yoke1.

    At each moment the simulator stores a single parameter describing the yoke’s position.

    Before each training session a certain tolerance level is set.

    The pilots’ task then is to fly as long as they can in such a way that all the yoke’s position measured during the flight differ by at most . In other words, a fragment of the flight starting at time and ending at time is within tolerance level if the position measurements, starting with -th and ending with -th, form such a sequence that for all elements of this sequence, the inequality holds.

    Your task is to write a program that, given a number and the sequence of yoke’s position measurements, determines the length of the longest fragment of the flight that is within the tolerance level .

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

    输入格式

    In the first line of the standard input two integers are given, and (, ), separated by a single space, denoting the tolerance level and the number of yoke’s position measurements taken.

    The second line gives those measurements, separated by single spaces. Each measurement is an integer from the interval from to .

    第一行两个有空格隔开的整数k(0<=k<=2000,000,000),n(1<=n<=3000,000),k代表设定的最大值,n代表序列的长度。第二行为n个由空格隔开的整数ai(1<=ai<=2000,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 和8, 6, 6, 9都是满足条件长度为4的序列

    上代码:

    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    inline int read() {
    	int f = 0, s = 0;
    	char ch = getchar();
    	while (!isdigit(ch)) f |= ch == '-', ch = getchar();
    	while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
    	return f ? -s : s;
    }
    void print(int x) {
    	if (x < 0) putchar('-'), x = -x;
    	if (x > 9) print(x / 10);
    	putchar(x % 10 + '0');
    }
    const int maxn = 3e6 + 10;
    int k, n, qm[maxn], qn[maxn], frontm = 1, frontn = 1, backm, backn, a[maxn], qmid[maxn], qnid[maxn];  // qm, qn 为维护区间最值的单调队列, qmid, qnid 维护的为区间最值的坐标。
    signed main() {
    	k = read(), n = read();
    	int ans = 0, id = 1;
    	for (int i = 1; i <= n; i++) a[i] = read();
    	for (int i = 1; i <= n; i++) {
    		while (frontn && frontn <= backn && a[i] < qn[backn]) --backn;
    		while (frontm && frontm <= backm && a[i] > qm[backm]) --backm;
    		qn[++backn] = a[i], qm[++backm] = a[i], qnid[backn] = i, qmid[backm] = i;
    		while (qm[frontm] - qn[frontn] > k) {
    			if (qnid[frontn] < qmid[frontm]) id = qnid[frontn] + 1, ++frontn;
    			else id = qmid[frontm] + 1, ++frontm;
    		}
    		ans = max(ans, i - id + 1);
    	}
    	print(ans);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    噢不,一条update.where无索引导致的MySQL死锁
    做过哪些外设驱动?
    大规模分布式存储系统核心原理解析,读书有感
    冯登国院士团队重磅论文!《具体高效的安全多方计算协议综述》解读
    php之 角色的权限管理(RBAC)详解
    产品思维训练 | 从海底捞涨价看产品价格调整的一些考虑
    20220701 Barbalat引理证明
    JAVA毕业设计课堂互动应答系统mp4计算机源码+lw文档+系统+调试部署+数据库
    运维小妙招:如何让系统信息随登录自动展现?
    快速统计文本数字之和
  • 原文地址:https://blog.csdn.net/lzx_xzl_______/article/details/126821706