Problem Descriptionthere 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.
Inputthere 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.
Outputfor each test case, print the length of the subsequence on a.
Sample Input
5 0 01 1 1 1 15 0 31 2 3 4 5
Sample Output
54
Source
Train of thought: two queues, one maintenance maximum, one maintenance minimum, when I, such as two queues, if the maximum value minus the minimum is greater than K, then two queues the smallest out of the queue, this time need to record his position, because his next position is to meet the conditions of the smallest POS,
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMS ET ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ( "%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define Bug PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 100005int n,m,k;int a[n],mique[n],maque[n];int Mita Il,mihead,matail,mahead;int now;void miinque (int i) {while (Mihead<mitail&&a[i]<a[mique[mitail-1]]) Mitail--;mique[mitail++]=i;} void Mainque (int i) {while (mahead<matail&&A[I]>A[MAQUE[MATAIL-1]]) matail--;maque[matail++]=i;} void Outque () {while (a[maque[mahead]]-a[mique[mihead]]>k) if (Maque[mahead]>mique[mihead]) {Now=mique[mihead] ; The smallest pos-1 mihead++ satisfying the condition; } else {Now=maque[mahead]; The smallest pos-1 mahead++ satisfying the condition; }}int Main () {int i,j,ans;while (~SFFF (n,m,k)) {fre (i,1,n+1) SF (A[i]); mitail=mihead=matail=mahead=0;ans=0; Now=0;fre (i,1,n+1) {miinque (i); Mainque (i); Outque (); if (a[maque[mahead]]-a[mique[mihead]]>=m) {Ans=max (ans, I-now); }} PF ("%d\n", ans);} return 0;}
HDU 3530 subsequence (monotone queue)