HDU 3530 單調佇列
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3995 Accepted Submission(s): 1308
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.
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
題意:求一個最長子序列,使得最大值與最小值差值在mi,mx範圍內,
用一個遞增,一個遞減兩個單調佇列維護最大值,最小值,然後掃描一遍,佇列的起點從最近一個被單調佇列拋棄的下標+1算起。
程式碼:
/* ***********************************************
Author :_rabbit
Created Time :2014/5/13 10:30:48
File Name :C.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
int a[100100],que1[100100],que2[100100];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n,mx,mi;
while(~scanf("%d%d%d",&n,&mi,&mx)){
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int head1=0,tail1=0,head2=0,tail2=0;
int last1=0,last2=0;
int ans=0;
for(int i=1;i<=n;i++){
while(head1<tail1&&a[que1[tail1-1]]>a[i])tail1--;//遞增
que1[tail1++]=i;
while(head2<tail2&&a[que2[tail2-1]]<a[i])tail2--;//遞減
que2[tail2++]=i;
while(a[que2[head2]]-a[que1[head1]]>mx){
if(que1[head1]<que2[head2])head1++;
else head2++;
}
if(a[que2[head2]]-a[que1[head1]]>=mi)
ans=max(ans,i-max(que1[head1-1],que2[head2-1]));
}
cout<<ans<<endl;
}
return 0;
}
相關文章
- HDU 3530 Subsequence (dp+單調佇列)佇列
- HDU3530 單調佇列的應用佇列
- hdu 3415 單調佇列佇列
- hdu 3401 單調佇列+DP佇列
- hdu 4122 單調佇列佇列
- hdu4374單調佇列+dp佇列
- 單調佇列佇列
- 單調棧/單調佇列佇列
- 單調佇列雙端佇列佇列
- hdu3415 單調佇列求區間最大和佇列
- hdu 4122Alice's mooncake shop(單調佇列)佇列
- 單調棧和單調佇列佇列
- 單調棧 和 單調佇列佇列
- POJ 2823 單調佇列佇列
- HDU 4123 Bob's Race:樹的直徑 + 單調佇列 + st表佇列
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- 單調佇列最佳化 DP佇列
- POJ 3017 單調佇列dp佇列
- 佇列-單端佇列佇列
- hdu5040 優先佇列+bfs佇列
- noi.ac #289. 電梯(單調佇列)佇列
- poj3017 dp+單調佇列佇列
- HDU 5884-Sort(佇列+二分)佇列
- hdu5380 貪心+雙端佇列佇列
- 多重揹包問題的單調佇列優化佇列優化
- C語言 簡單的佇列(陣列佇列)C語言佇列陣列
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- HDU4546 比賽難度 (優先佇列)佇列
- hdu 4546 優先佇列 數列組合和第m小佇列
- POJ 2823Sliding Window(單調佇列水題)佇列
- HDU5630 Hiking(貪心+優先佇列)佇列
- RabbitMQ-簡單佇列MQ佇列
- 單向鏈式佇列佇列
- 棧,佇列,優先順序佇列簡單介面使用佇列
- POJ2823 Sliding Window (單調佇列的基本應用)佇列
- 多重揹包O(N*V)演算法詳解(使用單調佇列)演算法佇列
- laravel 佇列的簡單使用Laravel佇列