[BZOJ4199][NOI2015]品酒大会

bzoj
luogu

sol

按照\(Height[i]\)从大到小的顺序合并\(SA[i]\)\(SA[i-1]\)
考虑合并两个集合对答案的贡献。
方案数会增加\(sz[x]*sz[y]\)
最大值可以被\(max(mx[x]*mx[y],mn[x]*mn[y])\)更新

code

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
const int N = 300005;
char S[N];
int n,a[N],t[N],x[N],y[N],SA[N],Rank[N],Height[N];
int id[N],fa[N],sz[N],mx[N],mn[N];
ll ans[2][N];
bool cmp(int i,int j,int k){return y[i]==y[j]&&y[i+k]==y[j+k];}
void getSA()
{
    int m=30;
    for (int i=1;i<=n;++i) ++t[x[i]=a[i]];
    for (int i=1;i<=m;++i) t[i]+=t[i-1];
    for (int i=n;i>=1;--i) SA[t[x[i]]--]=i;
    for (int k=1;k<=n;k<<=1)
    {
        int p=0;
        for (int i=0;i<=m;++i) y[i]=0;
        for (int i=n-k+1;i<=n;++i) y[++p]=i;
        for (int i=1;i<=n;++i) if (SA[i]>k) y[++p]=SA[i]-k;
        for (int i=0;i<=m;++i) t[i]=0;
        for (int i=1;i<=n;++i) ++t[x[y[i]]];
        for (int i=1;i<=m;++i) t[i]+=t[i-1];
        for (int i=n;i>=1;--i) SA[t[x[y[i]]]--]=y[i];
        swap(x,y);
        x[SA[1]]=p=1;
        for (int i=2;i<=n;++i) x[SA[i]]=cmp(SA[i],SA[i-1],k)?p:++p;
        if (p>=n) break;
        m=p;
    }
    for (int i=1;i<=n;++i) Rank[SA[i]]=i;
    for (int i=1,j=0;i<=n;++i)
    {
        if (j) --j;
        while (a[i+j]==a[SA[Rank[i]-1]+j]) ++j;
        Height[Rank[i]]=j;
    }
}
bool comp(int i,int j){return Height[i]>Height[j];}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void Merge(int x,int y,int z)
{
    x=find(x);y=find(y);
    ans[0][z]+=(ll)sz[x]*sz[y];
    sz[x]+=sz[y];
    ans[1][z]=max(ans[1][z],max((ll)mx[x]*mx[y],(ll)mn[x]*mn[y]));
    mx[x]=max(mx[x],mx[y]);
    mn[x]=min(mn[x],mn[y]);
    fa[y]=x;
}
int main()
{
    n=gi();scanf("%s",S+1);
    for (int i=1;i<=n;++i) a[i]=S[i]-'a'+1;
    getSA();
    for (int i=1;i<=n;++i) id[i]=fa[i]=i,sz[i]=1,mx[i]=mn[i]=gi();
    sort(id+2,id+n+1,comp);
    memset(ans[1],-127,sizeof(ans[1]));
    for (int i=2;i<=n;++i)
        Merge(SA[id[i]],SA[id[i]-1],Height[id[i]]);
    for (int i=n-2;i>=0;--i) ans[0][i]+=ans[0][i+1],ans[1][i]=max(ans[1][i],ans[1][i+1]);
    for (int i=0;i<n;++i)
        printf("%lld %lld\n",ans[0][i],ans[0][i]?ans[1][i]:0);
    return 0;
}
posted @ 2018-02-24 22:10  租酥雨  阅读(168)  评论(0编辑  收藏  举报