1. 程式人生 > >HDU 3530 單調佇列

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.

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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3995    Accepted Sub

HDU 3530 單調佇列

題意:給n個數和m,k,問你數列中最長的子序列,其中最大值減去最小值大於等於m小於等於k 思路:想著想著想到尺取去了,寫了一半實現不了((/ □ \)),一看單調佇列也沒怎麼練過,大致就只知道單調佇列肯定是維護一個什麼東西,只能看大神們的思路了,維護了兩個佇列,一個是以當

hdu~3530(單調佇列)

單調佇列就是佇列中的元素是單調遞增或遞減的。比如把 5 2 3 1 4 入隊: 減:、、、、、、、、、、、增: 5 、、、、、、、、、、、、5 5 2 、、、、、、、、、、、2  5 3 、、、、、

Problem A. Ascending Rating hdu 6319 單調佇列

Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submissi

hdu 3530單調佇列

傳送門 題解: 用一個單調不升的佇列維護最大值,一個單調不減的佇列維護最小值。如果不滿足條件,後移答案區間左端點,取兩個佇列頭指標的元素較小的一個(位置儘量靠前使區間儘量長)。 #include&

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

HDU 3401 Trade(DP + 單調佇列優化)

任重而道遠 Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study. He forecasts the next T days' stock market. On

HDU 6319 Ascending Rating 單調佇列,(最大值變化次數)

題意:長度為n的序列a.對每個m大小的區間[i,i+m-1],求出該區間的最大值,以及最大值變化的次數. 例如區間(4,2,7,5),最大值變化次數為2. n<=1e7. n<=1e7. 標準解應該為O(n). 容易想到用單調佇列維護每個區間的最大值. 發現正著做單調佇列的過程中,單

HDU 3401 Trade(dp+單調佇列優化)

Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5843  &

Second My Problem First HDU -單調佇列

單調佇列維護固定長度的區間的最小值即可。 #include<bits/stdc++.h> using namespace std; #define maxn 11111111 #define ll long long int L, n,a,b; ll

HDU 5956 ICPC-2016 瀋陽 I題 樹上斜率優化(”可持久化”單調佇列

題目 題解: 斜率優化在樹上,直接暴力刪線段再用棧維護複雜度顯然是錯的。 然後以為要用可持久化線段樹維護, 後來發現我sb了 只需要在查詢和修改的時候二分一下修改位置,直接在原陣列上修改和記錄就好了 因為我們每次只修改一個點,並且只會查詢上一個版本(父親)的單調

3530 Subsequence【單調佇列

Time limit 1000 ms Memory limit 32768 kB There is a sequence of integers. Your task is to find the longest subsequence that satisfi

HDU 3401 Trade(用單調佇列優化DP)

題意:給出T天內,每天的股票買賣價格和每天的股票買賣最大數量,而且最多隻能擁有maxP數量的股票,開始時有無限本金,任意兩次交易需要間隔W天及以上,也就是第i天交易,第i+w+1天及以後才能再交易。問最多能賺多少錢?(題意應該還有一個隱晦的要求是,當天只能買股票或者賣股票

單調佇列及其deque寫法 HDU 3415+Poj 4002 (日期處理) + 合併果子

嘗試用deque寫一下單調佇列,發現速度還是可以接受的,STL依賴症越來越嚴重了。。。。 HDU 3415 Max Sum of Max-K-sub-sequence 題意:給出一個有N個數字(-1000..1000,N<=10^5)的環狀序列,讓你求一個和最大的連續

HDU-3401-Trade-dp-單調佇列優化

【Description】 Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study. He forecasts

HDU-3401:Trade(dp+單調佇列優化)

題目大意: 就是有一個人知道每一天的股票的購入和賣出的價錢(注意只有一種股票),並且每天能夠購入和賣出的股票的數量也是有限制的。第 i 天買的股票也只能第 i+w+1 天賣出。但是無論何時,他手中持有的股票的數量不能超過m、問他本金無限的情況下最多盈利多少。 題意解

Alice's mooncake shop【HDU 4122】【單調佇列

題目連結   這道題還稍微有了點坑點,就是一開始過了的時候沒想到的,就是在一開始處理的時候,我們用到單調佇列,但是單調佇列一開始,我用陣列模擬,所以先放進去了區間長度-1個元素,但是,這個時候,就會有問題,就是,如果期限剛好在區間內的話,我們就會少去這個答案,所以不行!我們一邊

HDU 3415 Max Sum of Max-K-sub-sequence(長度不超過k的最大連續子序列和,單調佇列)

題目連結: HDU 3415 Max Sum of Max-K-sub-sequence 題意: 給n個數,首尾相連,求長度不超過k的最大連續子序列和。 資料範圍:1≤n≤100000,1≤k≤n 分析: 因為考慮首尾相連,所以我們把n個數看成2∗

hdu 2430 Beans 單調佇列

#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<string> #include<algorit

HDU 6319】 暑期多校day3 Ascending Rating (雙端單調佇列

題目大意 給定一個序列 a[1..n],對於每個長度為 m 的連續子區間, 求出區間 a 的最大值以及從左往右掃描該區間時 a 的最大值的變化次數。(1≤m≤n≤107)(1≤m≤n≤107) 解題思路 今天上午的時候剛幫高中教練驗了一道幾乎一樣