【NOI2015】品酒大会

一年一度的“幻影阁夏日品酒大会”隆重开幕了。大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加。

在大会的晚餐上,调酒师 Rainbow 调制了 n 杯鸡尾酒。这 n 杯鸡尾酒排成一行,其中第 i 杯酒 (1in) 被贴上了一个标签 si,每个标签都是 26 个小写英文字母之一。设 Str(l,r) 表示第 l 杯酒到第 r 杯酒的 rl+1 个标签顺次连接构成的字符串。若 Str(p,po)=Str(q,qo),其中 1ppon1qqonpqpop+1=qoq+1=r,则称第 p 杯酒与第 q 杯酒是“r相似” 的。当然两杯“r相似” (r>1)的酒同时也是“1 相似”、“2 相似”、、“(r1) 相似”的。特别地,对于任意的 1p,qnpq,第 p 杯酒和第 q 杯酒都是“0相似”的。

在品尝环节上,品酒师 Freda 轻松地评定了每一杯酒的美味度,凭借其专业的水准和经验成功夺取了“首席品酒家”的称号,其中第 i 杯酒 (1in) 的美味度为 ai。现在 Rainbow 公布了挑战环节的问题:本次大会调制的鸡尾酒有一个特点,如果把第 p 杯酒与第 q 杯酒调兑在一起,将得到一杯美味度为 apaq 的酒。现在请各位品酒师分别对于 r=0,1,2,,n1,统计出有多少种方法可以选出 2 杯“r相似”的酒,并回答选择 2 杯“r相似”的酒调兑可以得到的美味度的最大值。

输入格式

输入文件的第 1 行包含 1 个正整数 n,表示鸡尾酒的杯数。

第 2 行包含一个长度为 n 的字符串 S,其中第 i 个字符表示第 i 杯酒的标签。

第 3 行包含 n 个整数,相邻整数之间用单个空格隔开,其中第 i 个整数表示第 i 杯酒的美味度 ai

输出格式

输出文件包括 n 行。第 i 行输出 2 个整数,中间用单个空格隔开。第 1 个整数表示选出两杯“(i1)相似”的酒的方案数,第 2 个整数表示选出两杯“(i1)相似”的酒调兑可以得到的最大美味度。若不存在两杯“(i1)相似”的酒,这两个数均为 0

样例一

input

10
ponoiiipoi
2 1 4 7 4 8 3 6 4 7

output

45 56
10 56
3 32
0 0
0 0
0 0
0 0
0 0
0 0
0 0

explanation

用二元组 (p,q) 表示第 p 杯酒与第 q 杯酒。

0 相似:所有 45 对二元组都是 0 相似的,美味度最大的是 8×7=56

1 相似:(1,8) (2,4) (2,9) (4,9) (5,6) (5,7) (5,10) (6,7) (6,10) (7,10),最大的 8×7=56

2 相似:(1,8) (4,9) (5,6),最大的 4×8=32

没有 3,4,5,,9 相似的两杯酒,故均输出 0

样例二

input

12
abaabaabaaba
1 -2 3 -4 5 -6 7 -8 9 -10 11 -12

output

66 120
34 120
15 55
12 40
9 27
7 16
5 7
3 -4
2 -4
1 -4
0 0
0 0

样例三

见样例数据下载。

限制与约定

测试点编号n 的规模ai 的规模备注
1 n=100 ai10000  
2 n=200
3 n=500
4 n=750
5 n=1000 ai1000000000
6
7 n=2000
8
9 n=99991 ai1000000000 不存在“10相似”的酒
10
11 n=100000 ai1000000 所有 ai 的值都相等
12 n=200000
13 n=300000
14
15 n=100000 ai1000000000  
16
17 n=200000
18
19 n=300000
20

时间限制:1s

时间回到NOIday2:

一眼后缀自动机么,只要做过BZOJ3238: [Ahoi2013]差异(http://www.cnblogs.com/wzj-is-a-juruo/p/4561157.html)就无难度了。

将原串反过来建立后缀自动机,那么两个前缀的LCP就是其代表节点的LCA的l值。对于每个节点记录子树大小,子树中美味值最大值,子树中美味值最小值,树形DP,最后将答案更新到其l值的数组中,并扫一遍更新即可。30min写完,1A样例1、2,及大样例。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define rep(i,s,t) for(int i=s;i<=t;i++)
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
const int maxn=600010;
char s[maxn];
ll A[maxn],ans1[maxn],ans2[maxn],mx[maxn],mn[maxn],tot[maxn];
int pos[maxn],fa[maxn],l[maxn],to[maxn][26],cnt=1,last=1;
void extend(int c,int n) {
    int np,nq,p,q;
    p=last;last=np=++cnt;l[np]=l[p]+1;
    mn[np]=mx[np]=A[n];tot[np]=1;
    for(;!to[p][c];p=fa[p]) to[p][c]=np;
    if(!p) fa[np]=1;
    else {
        q=to[p][c];
        if(l[p]+1==l[q]) fa[np]=q;
        else {
            l[nq=++cnt]=l[p]+1;mx[cnt]=-(1ll<<50);mn[cnt]=1ll<<50;
            memcpy(to[nq],to[q],sizeof(to[q]));
            fa[nq]=fa[q];
            fa[q]=fa[np]=nq;
            for(;to[p][c]==q;p=fa[p]) to[p][c]=nq;
        }
    }
}
int x[maxn],od[maxn];
ll ret1[maxn],ret2[maxn];
int main() {
    int n=read();
    scanf("%s",s+1);
    rep(i,1,n) A[i]=read();
    mx[1]=-(1ll<<50);mn[1]=(1ll<<50);
    for(int i=n;i;i--) extend(s[i]-'a',i);
    rep(i,1,cnt) x[l[i]]++;
    rep(i,0,cnt) ans2[i]=-(1ll<<50);
    rep(i,1,n) x[i]+=x[i-1];
    rep(i,1,cnt) od[x[l[i]]--]=i;
    for(int i=cnt;i;i--) {
        int p=od[i];
        ans1[fa[p]]+=tot[fa[p]]*tot[p];
        tot[fa[p]]+=tot[p];
        if(mx[fa[p]]!=-(1ll<<50)) ans2[fa[p]]=max(ans2[fa[p]],mx[fa[p]]*mx[p]);
        if(mn[fa[p]]!=(1ll<<50)) ans2[fa[p]]=max(ans2[fa[p]],mn[fa[p]]*mn[p]);
        mx[fa[p]]=max(mx[fa[p]],mx[p]);
        mn[fa[p]]=min(mn[fa[p]],mn[p]);
    }
    rep(i,0,n-1) ret2[i]=-(1ll<<50);
    rep(i,1,cnt) ret1[l[i]]+=ans1[i],ret2[l[i]]=max(ret2[l[i]],ans2[i]);
    for(int i=n-2;i>=0;i--) {
        ret1[i]+=ret1[i+1];
        if(ret1[i+1]) ret2[i]=max(ret2[i],ret2[i+1]);
    }
    rep(i,0,n-1) printf("%lld %lld
",ret1[i],ret1[i]?ret2[i]:0); 
    return 0;
}
View Code

然后。70分滚粗了,Au无望。

因为答案可能达到-10^18,超过我的阀值-2^50,WA了3个点(2333333)

改改INF就行了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define rep(i,s,t) for(int i=s;i<=t;i++)
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
const int maxn=600010;
const ll INF=1ll<<60;
char s[maxn];
ll A[maxn],ans1[maxn],ans2[maxn],mx[maxn],mn[maxn],tot[maxn];
int pos[maxn],fa[maxn],l[maxn],to[maxn][26],cnt=1,last=1;
void extend(int c,int n) {
    int np,nq,p,q;
    p=last;last=np=++cnt;l[np]=l[p]+1;
    mn[np]=mx[np]=A[n];tot[np]=1;
    for(;!to[p][c];p=fa[p]) to[p][c]=np;
    if(!p) fa[np]=1;
    else {
        q=to[p][c];
        if(l[p]+1==l[q]) fa[np]=q;
        else {
            l[nq=++cnt]=l[p]+1;mx[cnt]=-INF;mn[cnt]=INF;
            memcpy(to[nq],to[q],sizeof(to[q]));
            fa[nq]=fa[q];
            fa[q]=fa[np]=nq;
            for(;to[p][c]==q;p=fa[p]) to[p][c]=nq;
        }
    }
}
int x[maxn],od[maxn];
ll ret1[maxn],ret2[maxn];
int main() {
    int n=read();
    scanf("%s",s+1);
    rep(i,1,n) A[i]=read();
    mx[1]=-INF;mn[1]=INF;
    for(int i=n;i;i--) extend(s[i]-'a',i);
    rep(i,1,cnt) x[l[i]]++;
    rep(i,0,cnt) ans2[i]=-INF;
    rep(i,1,n) x[i]+=x[i-1];
    rep(i,1,cnt) od[x[l[i]]--]=i;
    for(int i=cnt;i;i--) {
        int p=od[i];
        ans1[fa[p]]+=tot[fa[p]]*tot[p];
        tot[fa[p]]+=tot[p];
        if(mx[fa[p]]!=-INF) ans2[fa[p]]=max(ans2[fa[p]],mx[fa[p]]*mx[p]);
        if(mn[fa[p]]!=INF) ans2[fa[p]]=max(ans2[fa[p]],mn[fa[p]]*mn[p]);
        mx[fa[p]]=max(mx[fa[p]],mx[p]);
        mn[fa[p]]=min(mn[fa[p]],mn[p]);
    }
    rep(i,0,n-1) ret2[i]=-INF;
    rep(i,1,cnt) ret1[l[i]]+=ans1[i],ret2[l[i]]=max(ret2[l[i]],ans2[i]);
    for(int i=n-2;i>=0;i--) {
        ret1[i]+=ret1[i+1];
        if(ret1[i+1]) ret2[i]=max(ret2[i],ret2[i+1]);
    }
    rep(i,0,n-1) printf("%lld %lld
",ret1[i],ret1[i]?ret2[i]:0); 
    return 0;
}
View Code

尽管ZSY这道题爆炸后缀数组写错只得了55分,这依然不影响他高我134分。

原文地址:https://www.cnblogs.com/wzj-is-a-juruo/p/4695160.html