二分答案,判断是否存在合法方案使得每个数都不超过$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的更多相关文章

  1. [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 ...

  2. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  3. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  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 ...

  7. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  8. [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 ...

  9. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

随机推荐

  1. 记录sql server中数据创建时间和最后修改时间,方便查找问题

    getdate()用例: 2008-12-29 16:25:46.635 1.创建时间:将字段设置为datetime类型,并设置默认值为 getdate() 2.修改时间:通过触发器,在 update ...

  2. mysql 检查一个字符串是不是身份证号

    )CHARSET utf8) ) BEGIN DECLARE flag BOOL DEFAULT FALSE; AND number REGEXP CONCAT('^(([1][1-5])|([2][ ...

  3. Spark的Streaming + Flume进行数据采集(flume主动推送或者Spark Stream主动拉取)

    1.针对国外的开源技术,还是学会看国外的英文说明来的直接,迅速,这里简单贴一下如何看: 2.进入到flume的conf目录,创建一个flume-spark-push.sh的文件: [hadoop@sl ...

  4. 秒懂C#通过Emit动态生成代码

    首先需要声明一个程序集名称, 1 // specify a new assembly name 2 var assemblyName = new AssemblyName("Kitty&qu ...

  5. Azure 国内版 如何用powershell修改linux系统的密码

    国内版不像国际版本那样,一个UI按钮就解决问题,国内版很多功能上线比较慢,我们只能用powershell工具进行命令行 式的更改,也当温习一下命令了,好久不用了. $vm = Get-AzureVM ...

  6. Orchard是如何工作的?

    文章翻译自http://docs.orchardproject.net/Documentation/How-Orchard-works 对Orchard的理解还不深刻,翻译可能有不好的地方.     ...

  7. 【Android】Android apk默认安装位置设置

    在Android工程中,设置apk的默认安装位置 在AndroidManifest.xml文件Manifest标签中添加android:installLocation属性 android:instal ...

  8. Nessus忘记密码的解决

    进入到Nessus安装目录下

  9. 移动端iscroll实现日期选择

    哎,说多了都是泪: 引入相关JS文件 <script type="text/javascript" src="js/jquery-1.9.1.min.js" ...

  10. Python 类的内置方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-11-04 # p ...