TJOI2015题解
(转载前请标明出处,谢谢)
打算来做一波TJOI2015,来写题解啦!
Day1:
T1:[bzoj3996]
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3996
首先我们对题目中的式子化简一下,得到
;
于是这就成了一个最小割模型:
- S和(i,j)连边,权值为b[i,j]
- (i,j)和i,j分别连边,权值为inf
- i和T连边,权值为c[i]
于是跑一下最小割就可以了;
(然而不知道发生了什么。。。最小割跑不过去。。。似乎bzoj把p党卡常数了QAQ)
(cyand神犇说他在省选的时候贪心了一发,就过了,至今找不出反例,于是我水过去了)
最小割代码如下:
uses math;
var s,t,n,i,j,tot,w,cnt:longint;
last,pre,other,flow:array[..] of longint;
l,q:array[..] of longint;
ans:int64;
procedure insert(a,b,c,d:longint);
begin
pre[tot]:=last[a];
last[a]:=tot;
other[tot]:=b;
flow[tot]:=c;
inc(tot);
pre[tot]:=last[b];
last[b]:=tot;
other[tot]:=a;
flow[tot]:=d;
inc(tot);
end;
function bfs:boolean;
var s1,t1,u,v,q1:longint;
begin
fillchar(q,sizeof(q),);
for i:= to n do
l[i]:=-;
s1:=;
t1:=;
l[s]:=;
q[t1]:=s;
while (s1<t1) do
begin
inc(s1);
u:=q[s1];
q1:=last[u];
while (q1>=) do
begin
v:=other[q1];
if (flow[q1]>) and (l[v]=-) then
begin
inc(t1);
q[t1]:=v;
l[v]:=l[u]+;
end;
q1:=pre[q1];
end;
end;
if (l[t]=-) then exit(false) else exit(true);
end;
function find(u,int:longint):longint;
var w,v,q1,t1:longint;
begin
if (u=t) then exit(int);
w:=;
q1:=last[u];
while (q1>=) and (w<int) do
begin
v:=other[q1];
if (l[v]=l[u]+) then
begin
t1:=find(v,min(flow[q1],int-w));
flow[q1]:=flow[q1]-t1;
flow[q1 xor ]:=flow[q1 xor ]+t1;
w:=w+t1;
end;
q1:=pre[q1];
end;
if (w>=int) then l[u]:=-;
exit(w);
end;
function dinic:int64;
var e:longint;
ans:int64;
begin
ans:=;
while bfs do
begin
e:=find(s,maxlongint);
ans:=ans+e;
end;
exit(ans);
end;
begin
readln(n);
s:=n*n+n+;
t:=n*n+n+;
tot:=;
for i:= to t do
last[i]:=-;
cnt:=n;
ans:=;
for i:= to n do
begin
for j:= to n do
begin
read(w);
inc(cnt);
insert(s,cnt,w,);
insert(cnt,i,maxlongint,);
if (i<>j) then insert(cnt,j,maxlongint,);
ans:=ans+w;
end;
readln;
end;
for i:= to n do
begin
read(w);
insert(i,t,w,);
end;
readln;
n:=t;
writeln(ans-dinic);
end.
贪心代码如下:
var n,i,j,s,ans,max,max1:longint;
b:array[..,..] of longint;
c,inc:array[..] of longint;
begin
readln(n);
for i:= to n do
begin
for j:= to n do
read(b[i,j]);
readln;
end;
for i:= to n do
read(c[i]);
readln;
fillchar(inc,sizeof(inc),);
s:=;
ans:=;
for i:= to n do
begin
s:=;
for j:= to n do
s:=s+b[i,j]+b[j,i];
inc[i]:=b[i,i]-s+c[i];
end;
for i:= to n do
for j:= to n do
ans:=ans+b[i,j];
for i:= to n do
ans:=ans-c[i];
while true do
begin
max:=-;
max1:=-;
for i:= to n do
if (max<inc[i]) then
begin
max:=inc[i];
max1:=i;
end;
if (max1=-) then break;
ans:=ans+max;
for i:= to n do
inc[i]:=inc[i]+b[i,max1]+b[max1,i];
inc[max1]:=-;
end;
writeln(ans);
end.
T2:[bzoj3997]
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3997
本题要用一个Dilworth定理:DAG最小链覆盖=最大独立子集,
于是发现最大独立子集显然符合题目,于是直接跑DP就可以了,方程如下:
f[i,j]=max{f[i-1,j+1]+a[i,j],f[i-1,j],f[i,j+1]}
代码如下:
var t,l,n,m,i,j:longint;
a,f:array[..,..] of int64;
begin
readln(t);
for l:= to t do
begin
readln(n,m);
fillchar(a,sizeof(a),);
fillchar(f,sizeof(f),);
for i:= to n do
begin
for j:= to m do
read(a[i,j]);
readln;
end;
for i:= to n do
for j:=m downto do
begin
f[i,j]:=f[i-,j+]+a[i,j];
if (f[i-,j]>f[i,j]) then f[i,j]:=f[i-,j];
if (f[i,j+]>f[i,j]) then f[i,j]:=f[i,j+];
end;
writeln(f[n,]);
end;
end.
T3:[bzoj3998]
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3998
这题把我快写哭了QAQQQ。。。
这题是一个裸的后缀自动机,(然而我并不会,于是去看hzwer的博客)
于是写啊写啊写。。。然后狂WA不止。。。
问题在于,pascal党这里自动机建完以后千万不能写递归DFS。。。(C++党随意)
pascal党可以改成非递归形式或者拓扑排序,就可以了QAQQQ。。。
终于AC了,撒花撒花!!!
代码如下:
var ch:array[..] of char;
x:char;
n,i,j,tt,k,last,cnt,tot:longint;
a:array[..,..] of longint;
fa,mx,val,v,q,sum:array[..] of int64;
procedure extend(c:longint);
var p,np,q,nq,i,j:longint;
begin
p:=last;
inc(cnt);
last:=cnt;
np:=last;
mx[np]:=mx[p]+;
val[np]:=;
while (a[p,c]=) and (p<>) do
begin
a[p,c]:=np;
p:=fa[p];
end;
if (p=) then fa[np]:=
else
begin
q:=a[p,c];
if (mx[p]+=mx[q]) then fa[np]:=q
else
begin
inc(cnt);
nq:=cnt;
mx[nq]:=mx[p]+;
a[nq]:=a[q];
fa[nq]:=fa[q];
fa[q]:=nq;
fa[np]:=fa[q];
while (a[p,c]=q) do
begin
a[p,c]:=nq;
p:=fa[p];
end;
end;
end;
end;
procedure pre;
var i,j:longint;
t:int64;
begin
for i:= to cnt do
inc(v[mx[i]]);
for i:= to n do
v[i]:=v[i]+v[i-];
for i:=cnt downto do
begin
q[v[mx[i]]]:=i;
dec(v[mx[i]]);
end;
for i:=cnt downto do
begin
t:=q[i];
if (tt=) then val[fa[t]]:=val[fa[t]]+val[t] else val[t]:=;
end;
val[]:=;
for i:=cnt downto do
begin
t:=q[i];
sum[t]:=val[t];
for j:= to do
sum[t]:=sum[t]+sum[a[t,j]];
end;
end;
procedure dfs(x:longint);
var i:longint;
flag:boolean;
begin
while (k>val[x]) do
begin
k:=k-val[x];
flag:=false;
for i:= to do
begin
if (a[x,i]>) and not(flag) then
begin
if (k<=sum[a[x,i]]) then
begin
write(chr(i+));
x:=a[x,i];
flag:=true;
end
else
k:=k-sum[a[x,i]];
end;
end;
end;
end;
begin
n:=;
read(x);
while not((ord(x)>=) and (ord(x)<=)) do
begin
if (ord(x)>=) and (ord(x)<=+) then
begin
inc(n);
ch[n]:=x;
end;
read(x);
end;
while not((ord(x)>=) and (ord(x)<=)) do read(x);
tt:=ord(x)-;
read(x);
readln(k);
last:=;
cnt:=;
fillchar(fa,sizeof(fa),);
fillchar(mx,sizeof(mx),);
fillchar(val,sizeof(val),);
fillchar(sum,sizeof(sum),);
fillchar(v,sizeof(v),);
fillchar(q,sizeof(q),);
for i:= to n do
extend(ord(ch[i])-);
pre;
tot:=;
if (k>sum[]) then write('-1') else dfs();
writeln;
end.
Day2:
(T1留个坑,之后补)
T2:[bzoj4000]
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4000
这一题真心有毒QAQQQ
首先题目意思理解继续要读10遍题目QAQQQ注意行数是从0开始的QAQQQ
然后就是一个状态压缩DP。。。
显然过不了TATTT。。。
那就来一发矩阵乘法!
这就是正解了。。。然后作为pascal党的我木有过。。。这题bzoj上面pascal要用double(卡精度)。。。于是TLE。。。
不过介于cojs上面过了,所以一样贴过来了。
代码如下:
type arr=array[..,..] of int64;
var n,m,p,k,i,j,x,max:longint;
a:arr;
f:array[..] of longint;
mp:array[..,..] of longint;
ans,modp:int64;
function time(a:arr;b:longint):arr;
var ans,now,anss:arr;
i,j,k,r:longint;
begin
fillchar(ans,sizeof(ans),);
for i:= to max do
ans[i,i]:=;
now:=a;
r:=b;
while (r>) do
begin
if (r mod =) then
begin
fillchar(anss,sizeof(anss),);
for i:= to max do
for j:= to max do
for k:= to max do
anss[i,j]:=(anss[i,j]+(int64(ans[i,k]*now[k,j])));
ans:=anss;
end;
r:=r div ;
fillchar(anss,sizeof(anss),);
for i:= to max do
for j:= to max do
for k:= to max do
anss[i,j]:=anss[i,j]+(int64(now[i,k]*now[k,j]));
now:=anss;
end;
exit(ans);
end;
function tryit(a,b:longint):boolean;
var i,j,t,k:longint;
begin
for i:= to m- do
begin
if ((a shr i) and <>) then
begin
for j:= to mp[,] do
begin
k:=i+mp[,j];
if (k>=) and (k<=m-) and ((a shl k) and <>) then exit(false);
end;
for j:= to mp[,] do
begin
k:=i+mp[,j];
if (k>=) and (k<=m-) and ((b shr k) and <>) then exit(false);
end;
end;
end;
for i:= to m- do
begin
if ((b shr i) and <>) then
begin
for j:= to mp[,] do
begin
k:=i+mp[,j];
if (k>=) and (k<=m-) and ((b shr k) and <>) then exit(false);
end;
for j:= to mp[,] do
begin
k:=i+mp[,j];
if (k>=) and (k<=m-) and ((a shr k) and <>) then exit(false);
end;
end;
end;
exit(true);
end;
begin
modp:=;
for i:= to do
modp:=int64(modp*);
readln(n,m);
readln(p,k);
max:= shl m;
dec(max);
fillchar(mp,sizeof(mp),);
for i:= to do
begin
for j:= to p- do
begin
read(x);
if (x=) and not((i=) and (j=k)) then
begin
inc(mp[i,]);
mp[i,mp[i,]]:=j-k;
end;
end;
end;
fillchar(a,sizeof(a),);
fillchar(f,sizeof(f),);
for i:= to max do
for j:= to max do
if tryit(i,j) then a[i,j]:= else a[i,j]:=;
for i:= to max do
f[i]:=a[,i];
a:=time(a,n);
ans:=;
for i:= to max do
begin
if (f[i]<>) then ans:=(ans+a[i,]) mod modp;
while (ans>=modp) do ans:=ans-modp;
while (ans<) do ans:=ans+modp;
end;
writeln(ans);
end.
T3:[bzoj4001]
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4001
这一题一定是这次省选最友善的一个题。。。
首先我们可以打暴力(或者手算),于是找到了规律,
可以用组合数学证明一发(然而我不会。。。)QAQQQ
总之就是个结论题QAQQQ
代码如下:
var n:longint;
x:real;
begin
readln(n);
x:=n/(*n-);
x:=x*(n+)/;
writeln(x::);
end.
TJOI2015题解的更多相关文章
- 题解-TJOI2015 弦论
TJOI2015 弦论 字符串 \(s\) 和 \(t\) 和 \(k\).如果 \(t=0\),不同位置的相同子串算 \(1\) 个:如果 \(t=1\),不同位置的相同子串算多个.求 \(k\) ...
- 洛谷 P3975 / loj 2102 [TJOI2015] 弦论 题解【后缀自动机】【拓扑排序】
后缀自动机入门. 题目描述 为了提高智商,ZJY 开始学习弦论. 这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为 \(n\) 的字符串,求出它的第 \ ...
- BZOJ3998:[TJOI2015]弦论——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3998 https://www.luogu.org/problemnew/show/P3975 对于 ...
- 题解 P3978 【[TJOI2015]概率论】
这道题...好像是第一道我自己切出来的黑题... 先说一句,牛顿二项式蒟蒻并不会,可以说是直接套结论. 求诸位老爷轻喷. 这道题用卡特兰数搞. 卡特兰数这玩意从普及组初赛一路考到省选,十分有用. 如果 ...
- 【BZOJ】【TJOI2015】线性代数
网络流/最小割/最大权闭合图 2333好开心,除了一开始把$500^2$算成25000……导致数组没开够RE了一发,可以算是一次AC~ 咳咳还是回归正题来说题解吧: 一拿到这道题,我就想:这是什么鬼玩 ...
- TJOI2015 day2解题报告
TJOI2015终于写完啦~~~ T1:[TJOI2015]旅游 描述:(BZ没题面只能口述了..)一个人在一棵树上走,每次从a->b会进行一次贸易(也就是在这条路径上买入物品然后在后面卖出)然 ...
- 4001: [TJOI2015]概率论
4001: [TJOI2015]概率论 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 262 Solved: 108[Submit][Status] ...
- 3997: [TJOI2015]组合数学
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 247 Solved: 174[Submit][Status ...
- 【BZOJ4000】[TJOI2015]棋盘(矩阵快速幂,动态规划)
[BZOJ4000][TJOI2015]棋盘(矩阵快速幂,动态规划) 题面 BZOJ 洛谷 题解 发现所有的东西都是从\(0\)开始编号的,所以状压只需要压一行就行了. 然后就可以随意矩乘了. #in ...
随机推荐
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...
- mysql 5.7 编译安装脚本。
此脚本尽量运行在centos 服务器上面,用于编译安装mysql 5.7 将此脚本和相应的软件 都放到/usr/local/src 目录下面 由于不能上传附件 所以需要把cmake-3.9.6.ta ...
- SpringBoot之HelloWorld仔细分析
程序中的pom.xml文件: 一.父级标签 <parent> <groupId>org.springframework.boot</groupId> <art ...
- shell脚本:变量,文件判断,逻辑运算等纪要
shell脚本中的变量定义,引用各有不同的方式,除此之外,很常用的有文件属性判断,逻辑运算,数值运算等,下面记录一下它们的属性作用 变量 shell变量的定义分为两种:一种是直接赋值定义,另一种是嵌套 ...
- vs对某些网络错误的拦截
在编写代码的过程中发现如果在写好网页中的文本框内写入js代码(以<script>1</script>输入为例) vs会自动拦截并报错,如图(密码中我也输入了<script ...
- js跨域及解决办法
1.什么是跨域 我们经常会在页面上使用ajax请求访问其他服务器的数据,此时,客户端会出现跨域问题. 跨域问题是由于javascript语言安全限制中的同源策略造成的. 简单来说,同源策略是指一段脚本 ...
- Broken robot CodeForces - 24D (概率DP)
You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you unders ...
- POJ3320 尺取法的正确使用法
一.前言及题意: 最近一直在找题训练,想要更加系统的补补思维,补补漏洞什么的,以避免被个类似于脑筋急转弯的题目干倒,于是在四处找书,找了红书.蓝书,似乎都有些不尽如人意.这两天看到了日本人的白书,重新 ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- 【bzoj3339】Rmq Problem
[bzoj3339]Rmq Problem Description Input Output Sample Input 7 50 2 1 0 1 3 21 32 31 43 62 7 Sample ...