bzoj 3218: a + b Problem (可持久化线段树+最小割)
来源:互联网 发布:汽车单片机控制灯光 编辑:程序博客网 时间:2024/06/06 03:37
题目描述
传送门
题目大意:从前有个 n 个方格排成一行,从左至右依此编号为 1,2,⋯,n
有一天思考熊想给这 n 个方格染上黑白两色。
第 i个方格上有 6个属性:
如果方格 i 染成黑色就会获得
如果方格 i染成白色就会获得
但是太多了黑色就不好看了。如果方格 i是黑色,并且存在一个 j使得
如果方格 i 是奇怪的方格,就会使总好看度减少
也就是说对于一个染色方案,好看度为:
现在给你 n,a,b,w,l,r,p问所有染色方案中最大的好看度是多少。
题解
先考虑最原始的建图。
两种建图在写的过程中都尝试了,但是B是错的。为什么呢?区别在于B图拆点了,有些限制实现不了。就是如果一个点x不能直接限制另一个点y,但是可以通过限制另一个点z,再由z限制y。对于这种传递关系B图是实现不了的所以是错的,想让B也是对的其实只需要一个小修改,把B图中橙色的边改成双向边即可。
这也提示我们在建立最小割模型的时候要小心。
关键是怎么优化建图,发现A图中多数的边来之蓝色的边,极限情况下蓝色边的个数可以达到
发现如果我们按照点权,先不考虑
蓝色的边表示叶子节点连接每个点的代表节点,对于橙色的边,因为3,4属于权值线段树的同一个位置,所以对于所有流经4的也一定会流经3.
代码
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#define N 500003#define inf 1000000000 using namespace std;int tot,point[N],v[N],nxt[N],remain[N],last[N],deep[N],cur[N],num[N];int n,m,L[N],R[N],w[N],a[N],b[N],p[N];int ls[N],rs[N],root[N],sz,c[N],cnt;int add(int x,int y,int z){ tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; //cout<<x<<" "<<y<<" "<<z<<endl;}int addflow(int s,int t){ int now=t; int ans=inf; while (now!=s) { ans=min(ans,remain[last[now]]); now=v[last[now]^1]; } now=t; while (now!=s) { remain[last[now]]-=ans; remain[last[now]^1]+=ans; now=v[last[now]^1]; } return ans;}void bfs(int s,int t){ for (int i=1;i<=sz;i++) deep[i]=sz; deep[t]=0; queue<int> p; p.push(t); while (!p.empty()){ int now=p.front(); p.pop(); for (int i=point[now];i!=-1;i=nxt[i]) if (deep[v[i]]==sz&&remain[i^1]) deep[v[i]]=deep[now]+1,p.push(v[i]); }}int isap(int s,int t){ int now=s; bfs(s,t); int ans=0; for (int i=1;i<=sz;i++) cur[i]=point[i]; for (int i=1;i<=sz;i++) num[deep[i]]++; while (deep[s]<sz) { if (now==t) { ans+=addflow(s,t); now=s; } bool pd=false; for (int i=point[now];i!=-1;i=nxt[i]) if (deep[v[i]]+1==deep[now]&&remain[i]){ cur[now]=i; pd=true; last[v[i]]=i; now=v[i]; break; } if (!pd) { int minn=sz+1; for (int i=point[now];i!=-1;i=nxt[i]) if(remain[i]) minn=min(minn,deep[v[i]]); if (!--num[deep[now]]) break; num[deep[now]=minn+1]++; cur[now]=point[now]; if (now!=s) now=v[last[now]^1]; } } return ans;}void insert(int &i,int l,int r,int x,int id){ ls[++sz]=ls[i]; rs[sz]=rs[i]; if (l==r) { if (i) add(sz,i,inf); add(sz,id,inf); i=sz; return; } int mid=(l+r)/2; i=sz; if (x<=mid) insert(ls[i],l,mid,x,id); else insert(rs[i],mid+1,r,x,id);}void query(int i,int l,int r,int ll,int rr,int id){ if (ll<=l&&r<=rr) { if (i) add(id,i,inf); return; } int mid=(l+r)/2; if (ll<=mid) query(ls[i],l,mid,ll,rr,id); if (rr>mid) query(rs[i],mid+1,r,ll,rr,id);}int main(){ freopen("a.in","r",stdin);// freopen("my.out","w",stdout); scanf("%d",&n); int sum=0; tot=-1; memset(point,-1,sizeof(point)); for (int i=1;i<=n;i++){ scanf("%d%d%d%d%d%d",&a[i],&b[i],&w[i],&L[i],&R[i],&p[i]); sum+=b[i]+w[i]; c[++cnt]=a[i]; c[++cnt]=L[i]; c[++cnt]=R[i]; } sort(c+1,c+cnt+1); cnt=unique(c+1,c+cnt+1)-c-1; int S=1; int T=2+2*n; for (int i=1;i<=n;i++){ add(S,i+1,b[i]); add(i+1,i+n+1,p[i]); add(i+1,T,w[i]); } sz=T; for (int i=1;i<=n;i++) { a[i]=lower_bound(c+1,c+cnt+1,a[i])-c; L[i]=lower_bound(c+1,c+cnt+1,L[i])-c; R[i]=lower_bound(c+1,c+cnt+1,R[i])-c; query(root[i-1],1,cnt,L[i],R[i],i+n+1); root[i]=root[i-1]; insert(root[i],1,cnt,a[i],i+1); } for (int i=T+1;i<=sz;i++) { if (ls[i]) add(i,ls[i],inf); if (rs[i]) add(i,rs[i],inf); } printf("%d\n",sum-isap(S,T));}
- bzoj 3218: a + b Problem (可持久化线段树+最小割)
- BZOJ 3218 a + b Problem 可持久化线段树+最小割
- bzoj 3218: a + b Problem 最小割+可持久化线段树
- [BZOJ3218][UOJ#77]A+B Problem(可持久化线段树+最小割)
- BZOJ 3218 a + b Problem 最大流 + 可持久化线段树
- BZOJ 3218|UOJ 77|A + B Problem|最大流|可持久化线段树
- BZOJ 3218 a + b Problem 网络流 可持久化线段树优化建图
- bzoj 3218: a + b Problem|最小割|主席树
- bzoj 3489: A simple rmq problem 可持久化线段树套可持久化线段树
- [主席树优化建图 最小割] BZOJ 3218 a + b Problem
- 【网络流+可持久化线段树】[UOJ#77/BZOJ3218]A+B Problem
- UOJ#77 bzoj3218 a + b Problem 可持久化线段树优化建图+网络流
- BZOJ 2653 可持久化线段树
- 【bzoj3218】 a + b Problem 最小割+主席树
- BZOJ3218 UOJ#77 A+B Problem(最小割+主席树)
- BZOJ 3673 && BZOJ 3674 可持久化线段树
- 3218: a + b Problem 最小割+主席树优化建图
- BZOJ 2588 Count on a tree (COT) 可持久化线段树
- 0611 js写水仙花数
- 【算法】我拿走了哪个数?
- 二叉搜索树的插入
- aliyun 配置https
- 【linux】ubuntu php7.0 discuz mysqli_connect()不支持的解决方案
- bzoj 3218: a + b Problem (可持久化线段树+最小割)
- 用js在页面上显示一个Hello Word
- 工业大数据漫谈15:工业大数据与工业4.0的关系
- 二叉搜索树的查找
- 网页解析利器——HtmlUnit
- 微信移动端数据库组件WCDB系列(一)-iOS基础篇
- 免费馅饼
- php与js,jsp等语言特性对比
- 计蒜之道复赛D题—— 百度地图导航