HDU-3530 subsequence

Source: Internet
Author: User

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 &&amp ; 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.