• [NOI 2015]品酒大会


    Description

    题库链接

    (n) 杯鸡尾酒排成一行,其中第 (i) 杯酒 ((1 leq i leq n)) 被贴上了一个标签 (s_i),每个标签都是 (26) 个小写英文字母之一。设 (mathrm{Str}(l, r)) 表示第 (l) 杯酒到第 (r) 杯酒的 (r - l + 1) 个标签顺次连接构成的字符串。若 (mathrm{Str}(p, p_o) = mathrm{Str}(q, q_o)),其中 (1 leq p leq p_o leq n)(1 leq q leq q_o leq n)(p eq q)(p_o - p + 1 = q_o - q + 1 = r),则称第 (p) 杯酒与第 (q) 杯酒是“(r)相似” 的。当然两杯“(r)相似” ((r > 1))的酒同时也是“(1) 相似”、“(2) 相似”、(dots)、“((r - 1)) 相似”的。特别地,对于任意的 (1 leq p, q leq n)(p eq q),第 (p) 杯酒和第 (q) 杯酒都是“(0)相似”的。

    如果把第 (p) 杯酒与第 (q) 杯酒调兑在一起,将得到一杯美味度为 (a_p a_q) 的酒。现在对于 (r = 0,1,2, dots, n - 1),统计出有多少种方法可以选出 (2) 杯“(r)相似”的酒,并回答选择 (2) 杯“(r)相似”的酒调兑可以得到的美味度的最大值。

    (1leq nleq 300000)

    Solution

    可以参考[AHOI 2013]差异这题来统计个数。

    找最大值的话,可以用 ( ext{rmq}) 查区间最值相乘。

    最后做一遍前缀和以及前缀最大值即可。

    Code

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int N = 300000+5;
    const ll inf = 1ll*1000000000*1000000000+5;
    
    char ch[N];
    int n, m, x[N<<1], y[N<<1], c[N], sa[N], rk[N], height[N], L[N], R[N];
    int a[N], val[N], minn[20][N], maxn[20][N], lim, bin[25];
    int s[N], top, l[N], r[N], logn[N];
    ll cnt[N], ans[N];
    
    void get() {
        for (int i = 1; i <= n; i++) c[x[i] = ch[i]]++;
        for (int i = 2; i <= m; i++) c[i] += c[i-1];
        for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
        for (int k = 1; k <= n; k <<= 1) {
            int num = 0;
            for (int i = n-k+1; i <= n; i++) y[++num] = i;
            for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i]-k;
            for (int i = 1; i <= m; i++) c[i] = 0;
            for (int i = 1; i <= n; i++) c[x[i]]++;
            for (int i = 2; i <= m; i++) c[i] += c[i-1];
            for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
            swap(x, y); x[sa[1]] = num = 1;
            for (int i = 2; i <= n; i++)
                x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) ? num : ++num;
            if ((m = num) == n) break;
        }
        for (int i = 1; i <= n; i++) rk[sa[i]] = i;
        for (int i = 1, k = 0; i <= n; i++) {
            if (rk[i] == 1) continue;
            if (k) --k; int j = sa[rk[i]-1];
            while (i+k <= n && j+k <= n && ch[i+k] == ch[j+k]) ++k;
            height[rk[i]] = k;
        }
    }
    void rmq() {
        for (int i = 1; i <= n; i++) minn[0][i] = maxn[0][i] = val[i];
        for (int t = 1; t <= lim; t++)
            for (int i = 1; i+bin[t]-1 <= n; i++)
                minn[t][i] = min(minn[t-1][i], minn[t-1][i+bin[t-1]]),
                maxn[t][i] = max(maxn[t-1][i], maxn[t-1][i+bin[t-1]]);
    }
    int qmin(int l, int r) {
        int t = logn[r-l+1];
        return min(minn[t][l], minn[t][r-bin[t]+1]);
    }
    int qmax(int l, int r) {
        int t = logn[r-l+1];
        return max(maxn[t][l], maxn[t][r-bin[t]+1]);
    }
    void work() {
        scanf("%d%s", &n, ch+1); m = 255; logn[0] = -1; bin[0] = 1;
        for (int i = 1; i <= 20; i++) bin[i] = bin[i-1]<<1;
        for (int i = 1; i <= n; i++) logn[i] = logn[i>>1]+1;
        lim = logn[n];
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        get();
        for (int i = 1; i <= n; i++) val[rk[i]] = a[i];
        rmq();
        s[top = 1] = 1;
        for (int i = 2; i <= n; i++) {
            while (top != 1 && height[i] < height[s[top]]) --top;
            l[i] = s[top]; s[++top] = i;
        }
        s[top = 1] = n+1;
        for (int i = n; i >= 2; i--) {
            while (top != 1 && height[i] <= height[s[top]]) --top;
            r[i] = s[top]; s[++top] = i;
        }
        for (int i = 0; i <= n; i++) ans[i] = -inf;
        for (int i = 2; i <= n; i++) {
            cnt[height[i]] += 1ll*(i-l[i])*(r[i]-i);
            ans[height[i]] = max(ans[height[i]], 1ll*qmax(l[i], i-1)*qmax(i, r[i]-1));
            ans[height[i]] = max(ans[height[i]], 1ll*qmin(l[i], i-1)*qmin(i, r[i]-1));
        }
        for (int i = n-1; i >= 0; i--) 
            cnt[i] += cnt[i+1], ans[i] = max(ans[i], ans[i+1]);
        for (int i = n-1; i >= 0; i--)
            if (cnt[i] == 0) ans[i] = 0;
        for (int i = 0; i < n; i++) printf("%lld %lld
    ", cnt[i], ans[i]);
    }
    int main() {work(); return 0; }
  • 相关阅读:
    java:LeakFilling (Linux)
    java:redis(redis安装配置,redis的伪集群配置)
    java:Linux(简单命令,远程ssh使用hostname访问,.免密钥登录配置)
    java:easyui(重点示例)
    任意文件下载漏洞学习
    Centos7 nginx报错403 forbidden
    Centos7 nginx提示错误 Access denied.
    Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
    PHP载入GIF图像造成服务器宕机(CVE-2018-5711)的漏洞复现
    Python问题:'Nonetype' object is not iterable
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/9295061.html
Copyright © 2020-2023  润新知