• POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)


    题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度。

    析:由于n太大,无法暴力,也承受不了O(n*n)的复杂度,只能是O(nlogn),首先是用f[i] 表示每个数 i 为左端点,向右可以最多到达的点为f[i],

    那么这个dp很好转移,f[i] = max(f[i+1], last[a[i]]),其中last数组是用来记录上次序列数中a[i]的出现的位置。

    那么对于给定的区间[l, r] g[i] = min(r, f[i]) - i + 1,而答案为 ans = max{ g[i] l <= i<= r}。这样复杂度也太大,我们从f上下手、

    f 数组 一定是非降序的,所以一定存在一个位置pos,

    当 i < pos 时,g[i] = f[i] - i + 1,这个我们可以用rmq 来维护最大值。

    当 i >= pos 时,g[i] = r - i + 1,这个我们可以直接求出。

    对于pos我们可以用二分快速求出,利用 f 的单调性。总时间复杂度就是 O(nlogn)。

    代码如下:

    #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>
    #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 = 2e5 + 10;
    const int mod = 1e6 + 10;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -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 f[maxn], last[mod*3];
    
    struct Rmq{
      int dp[maxn][30];
      void init(int *a){
        for(int i = 0; i < n; ++i)  dp[i][0] = a[i] - i + 1;
        for(int j = 1; (1<<j) <= n; ++j)
          for(int i = 0; i + (1<<j) <= n; ++i)
            dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
      }
    
      int query(int l, int r){
        int k = log(r-l+1.0) / log(2.0);
        return max(dp[l][k], dp[r-(1<<k)+1][k]);
      }
    };
    
    Rmq rmq;
    
    int main(){
    
      while(scanf("%d %d", &n, &m) == 2){
        for(int i = 0; i < n; ++i){
          scanf("%d", a+i);
          a[i] += mod;
          last[a[i]] = n;
        }
        f[n-1] = n-1;  last[a[n-1]] = n-1;
        for(int i = n-2; i >= 0; --i){
          f[i] = min(f[i+1], last[a[i]]-1);
          last[a[i]] = i;
        }
        rmq.init(f);
        while(m--){
          int l, r;
          scanf("%d %d", &l, &r);
          int pos = lower_bound(f+l, f+n, r) - f;
          int ans = r - pos + 1;
          --pos;
          if(pos > l)  ans = max(ans, rmq.query(l, pos));
          printf("%d
    ", ans);
        }
      }
      return 0;
    }
    
  • 相关阅读:
    IfcFurnishingElementType
    IfcRelAssociatesClassification
    IfcRelAssociatesDocument
    IfcContext
    IfcRelAssociatesMaterial
    我是高敏感的人,你呢?
    介绍一本红色的书
    矫枉必须过正
    大家都在说的民法典,与我有何关系?
    线上Kafka突发rebalance异常,如何快速解决?
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6359084.html
Copyright © 2020-2023  润新知