BZOJ4199 NOI2015 品酒大会 题解&代码

并查集维护…着急回宿舍(浪)明天再写详细题解

——————————分割线——————————————
题解来啦!
题意:给出一个长度为n的串s,如果s中的第p位开始的后缀和第q位开始的后缀的公共前缀长度为r,那么称p和q是r相似的。将p和q混合在一起时,p和q混合物的权值是a[p]*a[q]。输出x∈[0,n-1]的x相似方案数和x相似时的最大权值
题解:
看到后缀的公共前缀第一反应应该是后缀数组吧
这道题题意比较复杂…不过仔细看题还是看得懂的
我们很容易可以得到r-1相似中一定包含了所有r相似的情况,那么考虑由r相似向下计算【用数组(vector)进行一次height[]相关的重新排序即可【以代码中排序法为主
r相似中的各相邻rank进行一次并查集合并,合并的时候维护max,min,cnt即可(max是集合最大值,min是集合最小值,cnt是集合size),顺便累计全局变量…嘛,没了

TLE了一个小时23333333最后发现我的后缀数组没用倍增

/**************************************************************
    Problem: 4199
    User: Rainbow6174
    Language: C++
    Result: Accepted
    Time:5992 ms
    Memory:29628 kb
****************************************************************/

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 300005;
vector <int> edge[maxn];
int n, s[maxn], a[maxn];
int fa[maxn], mi[maxn], ma[maxn], cn[maxn];
long long tot, del, ansc[maxn], ansd[maxn];
char str[maxn];
int wa[maxn], wb[maxn], wv[maxn], cnt[maxn];
int sa[maxn], height[maxn], Rank[maxn];
inline void DA(int *r,int n,int m)
{
    int *x = wa, *y = wb, p;
    for(int i = 0; i < m; i++) cnt[i] = 0;
    for(int i = 0; i < n; i++) cnt[ x[i] = r[i] ]++;
    for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
    for(int i = n-1; i >= 0; i--) sa[--cnt[x[i]]] = i;
    for(int j = 1; j < n ; j=j<<1)
    {
        p = 0;
        for(int i = n-j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
        for(int i = 0; i < n; i++) wv[i] = x[y[i]];
        for(int i = 0; i < m; i++) cnt[i] = 0;
        for(int i = 0; i < n; i++) cnt[wv[i]]++;
        for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
        for(int i = n-1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i];
        swap(x,y);
        p=1;
        x[sa[0]] = 0;
        for(int i = 1; i < n; i++)
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]]) && (y[sa[i]+j] == y[sa[i-1]+j]) ? p-1 : p++;
        if(p >= n) break;
        m = p;
    }
}
void calheight(int *r,int n)
{
    for(int i = 1; i <= n; i++)Rank[sa[i]] = i;
    int j = 0, k = 0;
    for(int i = 0; i < n; height[Rank[i]] = k, i++)
        for(k?k--:k=0, j = sa[Rank[i]-1]; r[i+k] == r[j+k]; k++);
}
void init(void)
{
    for(int i = 1; i <= n; i++)
    {
        fa[i] = i; cn[i] = 1;
        mi[i] = ma[i] = a[sa[i]];
    }
}
int Find(int x)
{
    if(x == fa[x])return x;
    return fa[x] = Find(fa[x]);
}
void unite(int x,int y)
{
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    if(x > y) swap(x,y);
    long long cal = max((long long)ma[x] * (long long)ma[y], (long long)mi[x] * (long long)mi[y]);
    if(!tot || cal > del) del = cal;
    tot += (long long)cn[x] * (long long)cn[y];
    fa[y] = x;
    ma[x] = max(ma[y], ma[x]);
    mi[x] = min(mi[y], mi[x]);
    cn[x] += cn[y];
}
int main(void)
{
    scanf("%d%s", &n, str);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        s[i] = str[i]-'a'+1;
    }
    DA(s,n+1,27);
    calheight(s,n);
    for(int i = 2; i <= n; i++)
        edge[height[i]].push_back(i);
    init();
    for(int i = n-1; i >= 0; i--)
    {
        for(int j = 0; j < edge[i].size(); j++)
            unite(edge[i][j],edge[i][j]-1);
        ansc[i] = tot;
        ansd[i] = del;
    }
    for(int i = 0; i < n; i++)
        printf("%lld %lld\n", ansc[i], ansd[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值