[原博客] POI系列(1)
正规、严谨、精妙。 -POI
发现POI(波兰信息学奥赛)的题都很有意思。于是开刷bzoj上的poi题目(按ac人数降序。。)。顺手写一写题解,加深印象。
为了防止一篇文章过于长,打算每五道题另起一篇文章。
BZOJ 1103 : [POI2007]大都市meg
给一棵树,每次可以把树上的一些边标记了,问一个点与根之间需要走多少没有标记的边。
这个可以把这颗树遍历得到一个dfs序每第一次经过一个点的时候记录为+1,第二次经过一个点的时候记录为-1,然后记录每个点第一次经过时在序列里的位置f[i]和第二次经过在序列里的位置l[i],对于查询(x)就是序列中一直到f[x]-1的前缀和,因为真正没走的那些边都+1-1消掉了,只剩下真正要统计的边了。对于每次标记(a,b) 设a<b 就是序列里 f[b]的位置-1,l[b]的位置+1,就相当于消掉了这条边。另外这道题得手写栈dfs,否则会爆栈。
/**************************************************************
Problem: 1103
User: zrts
Language: C++
Result: Accepted
Time:4484 ms
Memory:9596 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lowbit(x) (x&-x)
//by zrt
//problem:
using namespace std;
typedef long long ll;
);
int n,m;
];
],P[],X[],tot;
],l[];
];
int tim;
],top;
void add(int pos,int x){
for(;pos<=n;pos+=lowbit(pos)){
c[pos]+=x;
}
}
int ask(int pos){
;
;pos-=lowbit(pos)){
ret+=c[pos];
}
return ret;
}
inline void Add(int x,int y){
P[++tot]=y;X[tot]=H[x];H[x]=tot;
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
,x,y;i<n;i++){
scanf("%d%d",&x,&y);
if(x<y) Add(x,y);
else Add(y,x);
}
stk[top++]=;
int k;
){
k=stk[--top];
if(f[k]){
l[k]=++tim;
c[tim]=-;
continue;
}else{
f[k]=++tim;
c[tim]=;
stk[top++]=k;
for(int i=H[k];i;i=X[i]){
stk[top++]=P[i];
}
continue;
}
}
n*=;
;i<n;i++){
if(i+lowbit(i)<=n){
c[i+lowbit(i)]+=c[i];
}
}
scanf("%d",&m);
m=n/+m-;
,x,y;i<m;i++){
scanf("%s",s);
]=='W'){
scanf("%d",&x);
printf();
}else{
scanf("%d%d",&x,&y);
if(x>y){
add(f[x],-);
add(l[x],);
}else{
add(f[y],-);
add(l[y],);
}
}
}
;
}
BZOJ 1121 : [POI2008]激光发射器SZK
刚看完觉得不可做。
因为光线都是沿45°,所以从一个点发射光线必有一个点接收,又因为光路是可逆的,不可能有不同的点出发的光路重合的现象,所以输出n/2(原题让输出方案。。);
/**************************************************************
Problem: 1121
User: zrts
Language: C++
Result: Accepted
Time:0 ms
Memory:804 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
typedef long long ll;
);
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n;
scanf("%d",&n);
printf();
;
}
BZOJ 1113 : [Poi2008]海报PLA
显然单调栈啊,相等的时候处理一下。没了。
/**************************************************************
Problem: 1113
User: zrts
Language: C++
Result: Accepted
Time:1496 ms
Memory:1780 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
typedef long long ll;
);
],top;
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n;
scanf("%d",&n);
;
,x;i<n;i++){
scanf("%*d%d",&x);
&&x<=stk[top-]) {
top--,ans++;
if(stk[top]==x) ans--;
}
stk[top++]=x;
}
printf("%d\n",ans+top);
;
}
BZOJ 1529 : [POI2005]ska Piggy banks
一堆存钱罐,每个存钱罐的钥匙在另一个存钱罐里,问最少砸烂多少个存钱罐。
如果A罐的钥匙在B罐里,我们连边B->A,表示只要开了B就能开A。然后得到一张无向图,显然在一个强连通分量中的罐子最多只用砸烂一个,而每个强连通分量的罐子有可能被别的强连通分量的罐子打开,所以将这张图缩点,得到一个DAG,显然我们只用砸那些入度为0的强连通分量并且每个分量砸一个就ok了,也就是DAG的叶子节点。
(原来tarjan中,dfn没有记录的必要啊,详见程序。)
/**************************************************************
Problem: 1529
User: zrts
Language: C++
Result: Accepted
Time:2316 ms
Memory:63620 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
typedef long long ll;
);
int n;
],X[],P[],tot;
inline void add(int x,int y){
P[++tot]=y;X[tot]=H[x];H[x]=tot;
}
],stk[],top;
];
int cnt;
];
void tarjan(int x){
int dfn=low[x]=++tim;
instack[x]=;stk[top++]=x;
for(int i=H[x];i;i=X[i]){
if(!low[P[i]]) tarjan(P[i]),low[x]=min(low[x],low[P[i]]);
else if(instack[P[i]]) low[x]=min(low[x],low[P[i]]);
}
if(low[x]==dfn){
cnt++;
int k;
do{
k=stk[--top];
instack[k]=;
belong[k]=cnt;
}while(k!=x);
}
}
];
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
,x;i<=n;i++){
scanf("%d",&x);
add(x,i);
}
;i<=n;i++){
if(!low[i]) tarjan(i);
}
;i<=n;i++){
for(int j=H[i];j;j=X[j]){
;
}
}
;
;i<=cnt;i++){
if(!ind[i]) ot++;
}
printf("%d\n",ot);
;
}
BZOJ 1532 : [POI2005]Kos-Dicing
网络流里这个模型太熟了。
首先求最大值最小先二分,然后判定是否可以相互制约使得都在ans场内决出冠军。具体连边就是S向每场比赛连边容量为1,每场比赛向两个人分别连边,容量1。然后每个人向T连边,容量为ans。看看最大流S是否满载。搞定。
/**************************************************************
Problem: 1532
User: zrts
Language: C++
Result: Accepted
Time:576 ms
Memory:2764 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
typedef long long ll;
);
int n,m;
],X[],P[],flow[],d[],tot;
],h,t;
],b[];
int S,T;
inline void add(int x,int y,int z){
P[++tot]=y;X[tot]=H[x];H[x]=tot;flow[tot]=z;
}
bool bfs(){
memset(d,,;
d[S]=;q[t++]=S;
int k;
while(h!=t){
k=q[h++];
for(int i=H[k];i;i=X[i]){
&&(!d[P[i]])){
q[t++]=P[i];
d[P[i]]=d[k]+;
}
}
}
return d[T];
}
int dfs(int x,int a){
||x==T) return a;
int f=a,tmp;
for(int i=H[x];i&&a;i=X[i]){
&&flow[i]>){
tmp=dfs(P[i],min(a,flow[i]));
a-=tmp;
flow[i]-=tmp;
flow[i^]+=tmp;
}
}
;
return f-a;
}
int dinic(){
);
while(bfs()){
f+=dfs(S,<<);
}
return f;
}
bool judge(int x){
memset(H,,;
;i<m;i++){
add(S,i,);add(i,S,);
add(i,a[i]+m,);add(a[i]+m,i,);
add(i,b[i]+m,);add(b[i]+m,i,);
}
;i<=n;i++) add(i+m,T,x),add(T,i+m,);
if(dinic()>=m) return true;
else return false;
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d%d",&n,&m);
;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
}
),R(m),M;
S=,T=;
){
M=(L+R)>>;
if(judge(M)){
R=M;
}else L=M;
}
printf("%d\n",R);
;
}
以上。
[原博客] POI系列(1)的更多相关文章
- [原博客] POI系列(3)
正规.严谨.精妙. -POI BZOJ 1131 : [POI2008]Sta 树形dp吧,让求找一个点使以这个点深度和最小.首先可以随便整出来一棵树,对于每个节点记录down[i]以i为根下面的点的 ...
- [原博客] POI系列(5)
正规.严谨.精妙. -POI BZOJ 2213 : [Poi2011]Difference 如果我们每次枚举两个字母最大最小情况时,很容易想到写出代码里注释的样子.这样是26*26*n的,我们发现枚 ...
- [原博客] POI系列(4)
正规.严谨.精妙. -POI BZOJ 1531 : [POI2005]Bank notes 裸的背包,可以二进制拆分一下.一个物品比如说有n个,可以拆成 1,2,4,8,16...个. OJ上没有样 ...
- [原博客] POI系列(2)
正规.严谨.精妙. -POI bzoj 1098 : [POI2007]办公楼biu 如果把互相有手机号的建边得到一个无向图,那么这个图的补图的连通分量个数就是答案了.因为互相没手机号的必然在同一个连 ...
- 原博客地址http://blog.chinaunix.net/uid/20656672.html弃用
原博客地址http://blog.chinaunix.net/uid/20656672.html弃用
- [原博客] BZOJ 2242 [SDOI2011] 计算器
题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...
- 【全网最全的博客美化系列教程】08.自定义地址栏Logo
全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...
- 原博客地址http://blog.chinaunix.net/uid/20656672.html不再维护(10年前数百篇oracle/teradata性能优化、故障处理案例)
原博客地址http://blog.chinaunix.net/uid/20656672.html不再维护(数百篇oracle/teradata性能优化.故障处理原创文章) 858871 top 500 ...
- 【全网最全的博客美化系列教程】01.添加Github项目链接
全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...
随机推荐
- iOS中利用 runtime 一键改变字体
1.准备 我们新建一个项目名叫ChangeFont,然后我就随便找了个名叫loveway.ttf的字体库拖进去,里面的工程目录大概就是这样的 目录 现在我们就简单的直接在storyboard上拖了一个 ...
- Android_EditText
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- Eclipse错误
1.java compiler level does not match the version of the installed java project facet 解决:http://blog. ...
- 深入理解计算机系统第二版习题解答CSAPP 2.4
不进行数字转换为二进制和十六进制,计算结果. A. 0x503C + 0x8 = 0x5044 B. 0x503C - 0x40 = 0x4FFC C. 0x503C + 64 = 0x503C + ...
- Python 基础【第三篇】输入和输出
这里我们创建一个python(pytest)脚本用于学习测试(以后都为这个文件,不多做解释喽),这个文件必须要有执行权限的哈 1.创建pytest并赋予执行权限 [root@fengyuba_serv ...
- net.sf.json的jar包:JSONArray
今天在用maven添加net.sf.json的jar包的时候,代码如下: ? 1 2 3 4 5 <dependency> <groupId>net.sf.json-l ...
- Asp.Net 如何获取所有控件&如何获取指定类型的所有控件
一. Asp.Net Page页面中访问所有控件的属性为: Page.Controls 控件的结构是树结构. 二.获取指定类型所有控件实例: 1.递归方法定义: private void GetCon ...
- 20160322 javaweb 学习笔记--response 重定向
//一般方法 response.setStatus(302); response.setHeader("Location", "/20160314/index.jsp&q ...
- 响应者链 hittest:withEvent: 方法的使用
关于响应者链部分的基础内容 参考http://www.cnblogs.com/wendingding/p/3795171.html 这里我要说明的是 关于- (UIView *)hitTest:(CG ...
- HDU 4708 Rotation Lock Puzzle(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...