zoukankan      html  css  js  c++  java
  • POJ 2823 Sliding Window (单调队列)

    题意:给定一个序列,从左到右每次的滑动一个窗口,最大值和最小值是多少。

    析:普通的方法可能会超时,维护两个单调队列,一个单调递增的,一个单调递减,每次把最值保存下来。

    也可以用线段树,RMQ等数据结构,每次查询区间的最小值和最大值。POJ 交G++ 死活超时,交C++才过。

    代码如下:

    #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-8;
    const int maxn = 1e6 + 10;
    const int mod = 1e9 + 7;
    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 a[maxn];
    int q1[maxn], q2[maxn];
    int ans1[maxn], ans2[maxn];
    
    int main(){
      while(scanf("%d %d", &n, &m) == 2){
        int fro1 = 1, rear1 = 0;
        int fro2 = 1, rear2 = 0;
        for(int i = 0; i < m; ++i){
          scanf("%d", a+i);
          while(fro1 <= rear1 && a[i] <= a[q1[rear1]]) --rear1;
          while(fro2 <= rear2 && a[i] >= a[q2[rear2]]) --rear2;
          q1[++rear1] = i;
          q2[++rear2] = i;
        }
        ans1[0] = a[q1[fro1]];
        ans2[0] = a[q2[fro2]];
        for(int i = m; i < n; ++i){
          scanf("%d", a+i);
          while(fro1 <= rear1 && a[i] <= a[q1[rear1]]) --rear1;
          while(fro2 <= rear2 && a[i] >= a[q2[rear2]]) --rear2;
          q1[++rear1] = q2[++rear2] = i;
          while(q1[fro1] <= i-m) ++fro1;
          while(q2[fro2] <= i-m) ++fro2;
          ans1[i-m+1] = a[q1[fro1]];
          ans2[i-m+1] = a[q2[fro2]];
        }
        for(int i = 0; i <= n-m; ++i){
          if(i)  putchar(' ');
          printf("%d", ans1[i]);
        }
        printf("
    ");
        for(int i = 0; i <= n-m; ++i){
          if(i)  putchar(' ');
          printf("%d", ans2[i]);
        }
        printf("
    ");
      }
      return 0;
    }
    /*
    10 3
    154 5 54545154 51 545 1545464 54 2 54 54
    */
    
  • 相关阅读:
    __doPostback在客户端控件中的作用
    BlogEngine学习二:基于ICallbackEventHandler的轻量级Ajax方式
    JS操作XML数据备忘
    JS解析DataSet.GetXML()方法产生的xml
    JS中的prototype的使用方式
    实体类的二进制序列化
    PostgreSQL的.NET驱动程序Npgsql中参数对象的一个Bug
    PDF.NET的SQL日志
    PostgreSQL的PDF.NET驱动程序构建过程
    使用XSD编写具有智能提示的XML文件(以SQLMAP脚本为实例)
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6548913.html
Copyright © 2011-2022 走看看