HDU 3530 Subsequence 单调栈应用

来源:互联网 发布:财务报表数据分析方法 编辑:程序博客网 时间:2024/06/05 02:53

【题目链接】点击打开链接

【题意】 在一个队列中求一个最长子序列使得该子序列满足最大值与最小值的差大于k小于m求该子序列的最大长度

【解题方法】 维护一个单调递增的栈和一个单调递减的栈,在满足条件时,维护答案最大值即可。

【AC代码】

////Created by just_sort 2016/1/6//Copyright (c) 2016 just_sort.All Rights Reserved////#include <ext/pb_ds/assoc_container.hpp>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/hash_policy.hpp>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <time.h>#include <cstdlib>#include <cstring>#include <sstream> //isstringstream#include <iostream>#include <algorithm>using namespace std;//using namespace __gnu_pbds;typedef long long LL;typedef pair<int, LL> pp;#define REP1(i, a, b) for(int i = a; i < b; i++)#define REP2(i, a, b) for(int i = a; i <= b; i++)#define REP3(i, a, b) for(int i = a; i >= b; i--)#define CLR(a, b)     memset(a, b, sizeof(a))#define MP(x, y)      make_pair(x,y)const int maxn = 1000010;const int maxm = 2e5;const int maxs = 10;const int maxp = 1e3 + 10;//const int INF  = 1e9;const int UNF  = -1e9;const int mod  = 1e9 + 7;int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;//headstruct node{    int v, pos;    node(){}    node(int v, int pos) : v(v), pos(pos) {}}que1[maxn], que2[maxn];int n, m, k, a[100010];int head1, tail1;int head2, tail2;int main(){    while(scanf("%d%d%d", &n, &m, &k) != EOF)    {        CLR(que1, 0);        CLR(que2, 0);        head1 = tail1 = head2 = tail2 = 0;        int curans = 1, lastans = 0;        REP2(i, 1, n){            int x;            scanf("%d", &x);            while(head1 < tail1 && que1[tail1 - 1].v > x) tail1--;            que1[tail1] = node(x, i);            tail1++;            while(head2 < tail2 && que2[tail2 - 1].v < x) tail2--;            que2[tail2] = node(x, i);            tail2++;            while(head1 < tail1 && head2 < tail2 && (que2[head2].v - que1[head1].v) > k){                if(que2[head2].pos < que1[head1].pos) curans = que2[head2++].pos + 1;                else curans = que1[head1++].pos + 1;            }            if(head1 < tail1 && head2 < tail2 && (que2[head2].v - que1[head1].v) >= m){                lastans = max(lastans, i - curans + 1);            }        }        cout << lastans << endl;    }    return 0;}


0 0