题目:
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5146 Accepted Submission(s): 1712
Problem 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 element and the minimum element of the subsequence is no smaller than m and no larger than k.
Input
There are multiple test cases.
For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range [1, 100000]. m and k are in the range [0, 1000000]. The second line has n integers, which are all in the range [0, 1000000].
Proceed to the end of file.
Output
For each test case, print the length of the subsequence on a single line.
Sample Input
5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
Sample Output
Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU
Recommend
zhengfeng
题意:给一个长度为n的序列,求出其中最长的子序列,满足最大值与最小值之差大于等于m小于等于k。
思路:可以用一个单调队列来维护序列的最大值和最小值,如果最大值与最小值之差大于k,那么就让最大值队列的队首或最小值队列的队首中位置较小的那个出队,直到满足最大值与最小值之差小于等于k,选位置较小的出队是因为要使序列的长度最长,出队后的新区间的起点为出队的位置+1.然后再判断最大值与最小值之差是否满足大于等于m,
如果小于m不必出队,因为出队不可能使它们的差变大,如果大于等于m再考虑更新ans。
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include