BZOJ1889 : Maximal
二分答案,判断是否存在合法方案使得每个数都不超过$mid$。
考虑网络流建图:
$i$点的流量下限为$\max(a_i-mid,0)$,费用为$1$,故拆点进行限制。
$i$向$i+1$、$S$向$i$、$i$向$T$连边,费用为$0$。
那么一条增广路径对应选择一个区间进行减$1$。
求出流量不超过$K$时的最小费用可行流,若有解且费用不超过$M$,则可行。
#include<cstdio>
const int N=510,M=100010,inf=~0U>>2;
int n,K,m,i,a[N],L,R,mx,MID,ans,flow,cost,tmp;
int u[M],v[M],c[M],co[M],nxt[M],t,S,T,SS,TT,l,r,q[M],g[N],lim[N],f[N],d[N];bool in[N];
inline void add(int x,int y,int l,int r,int zo){
lim[x]-=l,lim[y]+=l;cost+=l*zo;
r-=l;
if(!r)return;
u[++t]=x;v[t]=y;c[t]=r;co[t]=zo;nxt[t]=g[x];g[x]=t;
u[++t]=y;v[t]=x;c[t]=0;co[t]=-zo;nxt[t]=g[y];g[y]=t;
}
bool spfa(){
int x,i;
for(i=1;i<=TT;i++)d[i]=inf,in[i]=0;
d[SS]=0;in[SS]=1;l=r=M>>1;q[l]=SS;
while(l<=r){
x=q[l++];
if(x==TT)continue;
for(i=g[x];i;i=nxt[i])if(c[i]&&co[i]+d[x]<d[v[i]]){
d[v[i]]=co[i]+d[x];f[v[i]]=i;
if(!in[v[i]]){
in[v[i]]=1;
if(d[v[i]]<d[q[l]])q[--l]=v[i];else q[++r]=v[i];
}
}
in[x]=0;
}
return d[TT]<inf;
}
bool check(){
flow=cost=0;
for(t=i=1;i<=TT;i++)g[i]=lim[i]=0;
for(i=1;i<=n;i++){
add(S,i,0,K,0);
add(i+n,T,0,K,0);
if(i<n)add(i+n,i+1,0,K,0);
add(i,i+n,a[i]>MID?a[i]-MID:0,mx,1);
}
add(T,S,0,K,0);
for(i=1;i<=T;i++)if(lim[i]>0)add(SS,i,0,lim[i],0),flow+=lim[i];else add(i,TT,0,-lim[i],0);
while(spfa()){
for(tmp=inf,i=TT;i!=SS;i=u[f[i]])if(tmp>c[f[i]])tmp=c[f[i]];
for(flow-=tmp,cost+=d[i=TT]*tmp;i!=SS;i=u[f[i]])c[f[i]]-=tmp,c[f[i]^1]+=tmp;
}
return !flow&&cost<=m;
}
int main(){
scanf("%d%d%d",&n,&K,&m);
S=n*2+1;T=S+1;SS=T+1;TT=SS+1;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
if(R<a[i])R=a[i];
}
mx=ans=R--;
while(L<=R){
MID=(L+R)>>1;
if(check())R=(ans=MID)-1;else L=MID+1;
}
return printf("%d",ans),0;
}
BZOJ1889 : Maximal的更多相关文章
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object
今天在进行代码检查的时候出现下面的异常: type parameters of <T>T cannot be determined; no unique maximal instance ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
随机推荐
- ActiveSync之HTTP
Exchange ActiveSync:HTTP协议让客户端能够同步存储在服务器上的数据. 互联网工程工作小组(IETF)要求所有互联网协议都比支持UTF-8编码.因此,ActiveSync传输所使用 ...
- 步步为营-77-Ajax简介
AJax:异步JavaScript和XML.Asynchronous JavaScript and XML 优点:无刷新 1 JavaScript下的Ajax 1.1 XMLHttpRequest对象 ...
- base | Thread类、ThreadData结构体 、CurrentThread命名空间
__thread __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量相比.__thread变量每一个线程有一份独立实体,各个线程的值互不干扰.可以用来修饰那些带有全局性且值可能变, ...
- order by 导出数据不一致的问题
转自 http://blog.csdn.net/wwwwei_csdn/article/details/78181197 前言 数据库分页是后台经常要使用的技术手段,有时候进行数据库查询会根据业务 ...
- NEST 中的日期数学表达式
Date math expressions Version: 5.x 英文原文地址:Date math expressions query/filter 中涉及到日期类型时(如:timeout 参数) ...
- nginx做代理离线下载插件
一.背景 被安装的服务器不能上网,无法下载插件,一个插件都还好,但是遇到插件依赖很强的需要几十个插件的依赖,这样就很麻烦. 二.环境 192.168.182.155 安装nginx 能 ...
- C语言关于进制转换,补码, 整数的位操作
菜单导航: 1.二进制.八进制.十进制.十六进制的相互转换 2.原码.反码.补码 3.举例证明整数在计算机内是以补码的形式存在(以负数为例) 4.整数的位操作:按位且&.或|.异或^.取反~ ...
- Factorial Trailing Zeroes Add to List
https://leetcode.com/problems/factorial-trailing-zeroes/#/description 想到了要找2x5:也想到了只要找5,剩下的2 管够.也想到了 ...
- Codeforces 725E Too Much Money (看题解)
Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...
- 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...