【BZOJ4199&UOJ131】品酒大会(后缀数组,并查集)
题意:

两杯“r相似” (r>1)的酒同时也是“1 相似”、“2 相似”、……、“(r−1) 相似”的。
n<=300000 abs(a[i])<=10^9
思路:对于i,j两个后缀,它们的贡献只与它们的lcp有关
而lcp又是它们之间height的最小值
所以可以把height从大到小排序
然后用并查集合并最值,方案数之类的
每次合并的都是排名相邻的一对后缀,相当于一段不相交的线段
每个集合中的lcp即为height的最小值
将r相似的加到r-1相似中
被UOJ的extra卡了一发,原来是0相似的初始最大值忘记初始化了
const oo=<<;
var sum,f:array[..]of int64;
sa,rank,height,id,mx,mn,size,a,fa,x,y,wc,wd,b:array[..]of longint;
n,m,i,p,q,t:longint;
ch:ansistring;
procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function max(x,y:int64):int64;
begin
if x>y then exit(x);
exit(y);
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=height[id[(l+r)>>]];
repeat
while mid<height[id[i]] do inc(i);
while mid>height[id[j]] do dec(j);
if i<=j then
begin
swap(id[i],id[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; function find(k:longint):longint;
begin
if fa[k]<>k then fa[k]:=find(fa[k]);
find:=fa[k];
end; procedure merge(x,y:longint);
begin
size[y]:=size[y]+size[x];
mx[y]:=max(mx[x],mx[y]);
mn[y]:=min(mn[x],mn[y]);
fa[x]:=y;
end; function cmp(a,b,l:longint):boolean;
begin
exit((y[a]=y[b])and(y[a+l]=y[b+l]));
end; procedure getsa(n:longint);
var i,j,p:longint;
begin
for i:= to n- do
begin
x[i]:=a[i];
inc(wc[a[i]]);
end;
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[x[i]]);
sa[wc[x[i]]]:=i;
end;
j:=; p:=;
while p<n do
begin
p:=;
for i:=n-j to n- do
begin
y[p]:=i; inc(p);
end;
for i:= to n- do
if sa[i]>=j then begin y[p]:=sa[i]-j; inc(p); end;
for i:= to n- do wd[i]:=x[y[i]];
for i:= to m- do wc[i]:=;
for i:= to n- do inc(wc[wd[i]]);
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[wd[i]]);
sa[wc[wd[i]]]:=y[i];
end;
for i:= to n do swap(x[i],y[i]);
p:=; x[sa[]]:=;
for i:= to n- do
if cmp(sa[i-],sa[i],j) then x[sa[i]]:=p-
else
begin
x[sa[i]]:=p; inc(p);
end;
j:=j*;
m:=p;
end;
end; procedure getheight(n:longint);
var i,j,k:longint;
begin
for i:= to n do rank[sa[i]]:=i;
k:=;
for i:= to n- do
begin
if k> then dec(k);
j:=sa[rank[i]-];
while a[i+k]=a[j+k] do inc(k);
height[rank[i]]:=k;
end;
end; begin
assign(input,'bzoj4199.in'); reset(input);
assign(output,'bzoj4199.out'); rewrite(output);
readln(n);
readln(ch);
for i:= to n- do a[i]:=ord(ch[i+])-ord('a')+;
a[n]:=; m:=;
getsa(n+);
getheight(n);
for i:= to n- do read(b[i]);
for i:= to n do
begin
fa[i]:=i;
size[i]:=; mx[i]:=b[sa[i]]; mn[i]:=mx[i];
f[i]:=-oo;
end;
f[]:=-oo;
for i:= to n- do id[i]:=i+;
qsort(,n-);
for i:= to n- do
begin
p:=find(id[i]-); q:=find(id[i]);
t:=height[id[i]];
sum[t]:=sum[t]+int64(size[p])*size[q];
f[t]:=max(f[t],int64(mx[p])*mx[q]);
f[t]:=max(f[t],int64(mx[p])*mn[q]);
f[t]:=max(f[t],int64(mn[p])*mx[q]);
f[t]:=max(f[t],int64(mn[p])*mn[q]);
merge(p,q);
end;
for i:=n- downto do
begin
sum[i]:=sum[i]+sum[i+];
f[i]:=max(f[i],f[i+]);
end;
for i:= to n- do
if f[i]>-oo then writeln(sum[i],' ',f[i])
else writeln(,' ',); close(input);
close(output);
end.
【BZOJ4199&UOJ131】品酒大会(后缀数组,并查集)的更多相关文章
- [UOJ#131][BZOJ4199][NOI2015]品酒大会 后缀数组 + 并查集
[UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...
- NOI 2015 品酒大会 (后缀数组+并查集)
题目大意:略 40分暴力还是很好写的,差分再跑个后缀和 和 后缀最大值就行了 一种正解是后缀数组+并查集 但据说还有后缀数组+单调栈的高端操作蒟蒻的我当然不会 后缀数组求出height,然后从大到小排 ...
- 【BZOJ-4199】品酒大会 后缀数组 + 并查集合并集合
4199: [Noi2015]品酒大会 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 436 Solved: 243[Submit][Status] ...
- 【BZOJ4199】[Noi2015]品酒大会 后缀数组+并查集
[BZOJ4199][Noi2015]品酒大会 题面:http://www.lydsy.com/JudgeOnline/wttl/thread.php?tid=2144 题解:听说能用SAM?SA默默 ...
- [NOI2015] 品酒大会 - 后缀数组,并查集,STL,启发式合并
[NOI2015] 品酒大会 Description 对于每一个 \(i \in [0,n)\) 求有多少对后缀满足 LCP 长度 \(\le i\) ,并求满足条件的两个后缀权值乘积的最大值. So ...
- BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )
求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...
- 【学术篇】NOI2015 品酒大会 后缀数组+并查集
省选前大致是刷不了几道题了... 所以就找一些裸一点的题目练练板子算了= = 然而这题一点都不裸, 也并不怎么好写... 于是就浪费了将近一下午的时间... 然而还不是因为后缀数组板子不熟= = 首先 ...
- Uoj #131. 【NOI2015】品酒大会 后缀数组,并查集
#131. [NOI2015]品酒大会 统计 描述 提交 自定义测试 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项, ...
- BZOJ 4566 JZYZOJ 1547 [haoi2016T5]找相同子串 后缀数组 并查集
http://172.20.6.3/Problem_Show.asp?id=1547 http://www.lydsy.com/JudgeOnline/problem.php?id=4566 单纯后缀 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- 【第三届强网杯】write up
一,鲲or鳗orGame 从浏览器里拿到game.gb 用 VisualBoyAdvance ,搜索内存变量,然后改变量就完事了,但是改变量他们说直接利用添加代码功能有bug,所以要在工具里的内存查看 ...
- 推荐一波 瀑布流的RecylceView
推荐博客:http://www.bubuko.com/infodetail-999014.html
- 转 Shell调试篇
检查语法 -n选项只做语法检查,而不执行脚本. sh -n script_name.sh 启动调试 sh -x script_name.sh 进入调试模式后,Shell依次执行读入的语句,产生的输出中 ...
- WinForm ListBox 控件用法
下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...
- 【转】MySQL常见的运算符及使用
转自:http://www.linuxidc.com/Linux/2016-03/129672.htm MySQL中有4类运算符,它们是: 算术运算符 比较运算符 逻辑运算符 位操作运算符 算术操作符 ...
- JOptionPane.showMessageDialog出现在浏览器下面的解决方法
将JOptionPane.showMessageDialog(null, result, "发布公告:", JOptionPane.INFORMATION_MESSAGE);中的参 ...
- NodeJs学习记录(六)使用 res.locals 传递参数到页面
res.locals的生命周期是单次请求,有点类似于java servlet 里的 httpServletRequest.setAttribute("param1",1); 既然 ...
- C# 客户端读取共享目录文件
控制台应用程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 梦想CAD控件网页版标注样式
增加标注样式 _DMxDrawX::AddDimStyle 增加一个新的标注样式,如果当前已经有指定名的标注样式,就直接失败返回.详细说明如下: 参数 说明 BSTR pszName 新增加的标注样式 ...
- RabbitMQ系列(六)--面试官问为什么要使用MQ,应该怎么回答
如果简历中有写到使用过RabbitMQ或者其他的消息中间件,可能在MQ方面的第一个问题就是问:为什么要使用MQ 面试官期望的回答 1.项目中有什么业务场景需要用到MQ 2.但是用了MQ,会带来很多问题 ...