• [NOI2015] 品酒大会 题解


    [NOI2015] 品酒大会 题解

    link

    题目大意

    给定一个长度为 n" role="presentation" style="position: relative;">n 的字符串 s" role="presentation" style="position: relative;">s ,和第 i" role="presentation" style="position: relative;">i 个位置的权值 ai" role="presentation" style="position: relative;">ai

    对于每一个 r[0,n)" role="presentation" style="position: relative;">r[0,n) ,求满足 lcp(i,j)r" role="presentation" style="position: relative;">lcp(i,j)r(i,j)" role="presentation" style="position: relative;">(i,j) 的对数

    以及所有的 (i,j)" role="presentation" style="position: relative;">(i,j) 中, ai×aj" role="presentation" style="position: relative;">ai×aj 的最大值

    n3×105" role="presentation" style="position: relative;">n3×105

    题解

    1

    可以转化为倒序枚举 r" role="presentation" style="position: relative;">r 后求 lcp(i,j)=r" role="presentation" style="position: relative;">lcp(i,j)=r(i,j)" role="presentation" style="position: relative;">(i,j) 数量以及最大值。

    求后缀和、 后缀最大值

    2

    由于 lcp" role="presentation" style="position: relative;">lcprk" role="presentation" style="position: relative;">rk 上连续的一段 height" role="presentation" style="position: relative;">height 的最小值

    实际上就是要求区间最小值为 r" role="presentation" style="position: relative;">r 的区间个数

    3

    height" role="presentation" style="position: relative;">height 降序排序,按照顺序加入。

    每次在 p" role="presentation" style="position: relative;">p 插入一个数,就是合并两堆数,即 p+1" role="presentation" style="position: relative;">p+1p1" role="presentation" style="position: relative;">p1 所在堆与 p" role="presentation" style="position: relative;">p 合并。

    并查集维护大小、最值即可。

    code

    1. #include
    2. using namespace std;
    3. typedef long long LL;
    4. const int N = 3e5 + 5;
    5. int n, sa[N], rk[N], old[N], t[N], id[N], m, h[N];
    6. int fa[N], sz[N];
    7. LL a[N], mx[N], mn[N], s1[N], s2[N], cnt, res = -1e18;
    8. char str[N];
    9. inline void rs() {
    10. for (int i = 1; i <= m; i++) t[i] = 0;
    11. for (int i = 1; i <= n; i++) ++t[rk[i]];
    12. for (int i = 1; i <= m; i++) t[i] += t[i - 1];
    13. for (int i = n; i >= 1; i--) sa[t[rk[id[i]]]--] = id[i], id[i] = 0;
    14. }
    15. inline int EQ(int x, int y, int k)
    16. { return old[x] == old[y] && old[x + k] == old[y + k]; }
    17. inline void bui() {
    18. m = 200;
    19. for (int i = 1; i <= n; i++) rk[i] = str[i], id[i] = i;
    20. rs();
    21. for (int k = 1, p; k <= n; k <<= 1) {
    22. p = 0;
    23. for (int i = n - k + 1; i <= n; i++) id[++p] = i;
    24. for (int i = 1; i <= n; i++) if (sa[i] > k) id[++p] = sa[i] - k;
    25. rs(), memcpy(old, rk, sizeof(rk)), p = 0;
    26. for (int i = 1; i <= n; i++) rk[sa[i]] = EQ(sa[i], sa[i - 1], k) ? p : ++p;
    27. if (p == n) break;
    28. m = p;
    29. }
    30. for (int i = 1, j, k = 0; i <= n; h[rk[i++]] = k)
    31. for (k ? --k : 0, j = sa[rk[i] - 1]; str[i + k] == str[j + k]; k++);
    32. }
    33. int fd(int x) {
    34. while (fa[x] ^ x) x = fa[x] = fa[fa[x]];
    35. return x;
    36. }
    37. vector<int> b[N];
    38. inline void mer(int x, int y) {
    39. x = fd(x), y = fd(y);
    40. cnt += 1ll * sz[x] * sz[y], res = max(res, max(mx[x] * mx[y], mn[x] * mn[y]));
    41. fa[y] = x, sz[x] += sz[y], mx[x] = max(mx[x], mx[y]), mn[x] = min(mn[x], mn[y]);
    42. }
    43. int main() {
    44. scanf("%d%s", &n, str + 1);
    45. for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    46. bui();
    47. for (int i = 1; i <= n; i++) {
    48. fa[i] = i, sz[i] = 1, mx[i] = mn[i] = a[sa[i]];
    49. b[h[i]].push_back(i);
    50. }
    51. for (int i = n - 1; ~i; i--) {
    52. // for (int x : b[i]) mer(x, x - 1);
    53. int le = b[i].size();
    54. for (int j = 0; j < le; j++) mer(b[i][j], b[i][j] - 1);
    55. if (cnt) s1[i] = cnt, s2[i] = res;
    56. }
    57. for (int i = 0; i < n; i++) printf("%lld %lld\n", s1[i], s2[i]);
    58. }
  • 相关阅读:
    安卓毕业设计app项目源码基于Uniapp实现的美食餐厅订餐点餐
    vivado 仿真读写bmp图片
    【计算机考研】【英语一】必备词组
    [开源项目推荐]privateGPT使用体验和修改
    DP专题4 不同路径|
    pytorch的使用:使用神经网络进行气温预测
    chrome V3插件入门到放弃,Plasmo不完全使用指南
    Django部署深度学习项目-1
    【cartographer ros】十: 延时和误差分析
    python的环境安装(版本3.10.6)
  • 原文地址:https://blog.csdn.net/KonjakuLAF/article/details/126854270