Description
There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition:the difference between the maximum El Ement and the minimum element of the subsequence is no smaller than M and no larger than K.
Input
There is multiple test cases.
For each test case, the first line had three integers, N, m and K. N is the length of the sequence and was in the range [1, 100000]. m and K is in the range [0, 1000000]. The second line has n integers, which is all in the range [0, 1000000].
Proceed to the end of file.
Output
For each test case, print the length of the subsequence in a single line.
Sample Input
Sample Output
Source
ACM-ICPC multi-university Training Contest (10) ――host by HEU
Test instructions: A sequence of the longest maximum and minimum difference in the range of [M, K]
Idea: Maintain a maximum sequence and a minimum sequence. And then infer if it fits the range.
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < queue>using namespace Std;const int maxn = 100010;int num[maxn];int q1[maxn], Q2[maxn];int N, M, K;int main () {while (s CANF ("%d%d%d", &n, &m, &k)! = EOF) {for (int i = 1; I <= n; i++) scanf ("%d", &num[i]); int ans = 0;int re AR1 = 0, front1 = 0, rear2 = 0, Front2 = 0;int cnt = 0;for (int i = 1; I <= n; i++) {while (Front1 < rear1 && ; Num[q1[rear1-1]] < Num[i]) rear1--;q1[rear1++] = I;while (Front2 < rear2 && num[q2[rear2-1] [> Num[i]) rea r2--;q2[rear2++] = I;while (Front1 < rear1 && Front2 < rear2 && Num[q1[front1]]-num[q2[front2]] > k) {if (Q1[front1] < Q2[front2]) {cnt = q1[front1];front1++;} else {cnt = q2[front2];front2++;}} if (Front1 < rear1 && Front2 < rear2 && Num[q1[front1]]-num[q2[front2]] >= m) ans = max (ans, i-cnt );} printf ("%d\n", ans);} return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
HDU-3530 subsequence