zoukankan      html  css  js  c++  java
  • HDU 3706 Second My Problem First (单调队列)

    题意:求给定的一个序列中最长子序列,该子序列的最大值和最小值介于m和k之间。

    析:用两个单调队列来维护一个最小值,一个最大值,然后每次更新即可。

    代码如下;

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-5;
    const int maxn = 100000 + 10;
    const int mod = 1e6;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    int q1[maxn], q2[maxn];
    int a[maxn], k;
    
    int main(){
      while(scanf("%d %d %d", &n, &m, &k) == 3){
        int ans = 0;
        int fro1 = 0;  int rear1 = 0;
        int fro2 = 0;  int rear2 = 0;
        int tmp = 1;
        for(int i = 1; i <= n; ++i){
          scanf("%d", a+i);
          while(fro1 < rear1 && a[q1[rear1-1]] > a[i])  --rear1;  //increase
          while(fro2 < rear2 && a[q2[rear2-1]] < a[i])  --rear2;  //decrease
          q1[rear1++] = q2[rear2++] = i;
          while(fro1 < rear1 && fro2 < rear2 && a[q2[fro2]]-a[q1[fro1]] > k){
            if(q1[fro1] < q2[fro2])  tmp = q1[fro1++] + 1;
            else tmp = q2[fro2++] + 1;
          }
          if(fro1 < rear1 && fro2 < rear2 && a[q2[fro2]]-a[q1[fro1]] >= m)  ans = max(ans, i - tmp + 1);
        }
        printf("%d
    ", ans);
      }
      return 0;
    }
    
  • 相关阅读:
    问题堆栈区39+40
    ListView优化分页优化
    AsyncTask理解- Day36or37
    Activity是如何挂载Pargment的Day35
    BroadcastReceiver和Intetnt的理解 Day34
    深入理解自定义ListView
    手势识别=读取手机联系人=ContentResolver-Day3
    Android本地JUnit Text
    Java——(一)一切都是对象
    SciTE: 中文字符支持问题
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6480789.html
Copyright © 2011-2022 走看看