1. 程式人生 > >HDU-3530 Subsequence(單調隊列)

HDU-3530 Subsequence(單調隊列)

clu lan lar ssi isf multi while for inline

Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7390 Accepted Submission(s): 2498


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 5 4

Source 2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

Recommend zhengfeng | We have carefully selected several similar problems for you: 3535 3529 3528 3527 3415 有一點突破的跡象qwq 就是求某一個區間的最值並且這個區間的左右端點在變的情況一般都用單調隊列 or 單調棧 來做QwQ
dalao勿噴qwq
 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX=1e5+5;
 5 int n,m,k;
 6 int a[MAX];
 7 deque <int> q1,q2;
 8 inline int read(){
 9     int an=0,x=1;char c=getchar();
10     while (c<0 || c>9) {if (c==-) x=-1;c=getchar();}
11     while (c>=0 && c<=9) {an=an*10+c-0;c=getchar();}
12     return an*x;
13 }
14 int main(){
15     freopen ("subsequence.in","r",stdin);
16     freopen ("subsequence.out","w",stdout);
17     int i,j;
18     while (~scanf("%d%d%d",&n,&m,&k)){
19         int ans=0,last=0;
20         for (i=1;i<=n;i++) a[i]=read();
21         while (q1.size()) q1.pop_back(); while (q2.size()) q2.pop_back();
22         for (i=1;i<=n;i++){
23             while (q1.size() && a[i]>a[q1.back()]) q1.pop_back();
24             while (q2.size() && a[i]<a[q2.back()]) q2.pop_back();
25             q1.push_back(i),q2.push_back(i);
26             while (q1.size() && q2.size() && (a[q1.front()]-a[q2.front()])>k){
27                 if (q1.front()<q2.front()){
28                     last=q1.front(),q1.pop_front();
29                 }
30                 else if (q1.front()>q2.front()){
31                     last=q2.front(),q2.pop_front();
32                 }
33                 else{
34                     last=q1.front(),q1.pop_front(),q2.pop_front();
35                 }
36             }
37             if (q1.size() && q2.size() && (a[q1.front()]-a[q2.front()])>=m){
38                 ans=max(ans,i-last);
39             }
40         }
41         printf("%d\n",ans);
42     }
43     return 0;
44 }

HDU-3530 Subsequence(單調隊列)