二分答案,判断是否存在合法方案使得每个数都不超过$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. Mom and Dad

    Mom  Poodwaddle Life clock Dad Poodwaddlw Life clock Happiness is the meaning and the purpose of lif ...

  2. asp.net core ioc 依赖注入

    1.生命周期 内置的IOC有三种生命周期: Transient: Transient服务在每次被请求时都会被创建.这种生命周期比较适用于轻量级的无状态服务. Scoped: Scoped生命周期的服务 ...

  3. 下载离线VS2017

    1.下载工具 版本 文件 Visual Studio Enterprise (企业版) vs_enterprise.exe Visual Studio Professional (专业版) vs_pr ...

  4. gradle repo conf - maven-central.storage-download.googleapis.com

    repositories { google() jcenter() maven { // The google mirror is less flaky than mavenCentral() url ...

  5. 一脸懵逼学习Struts数据校验以及数据回显,模型驱动,防止表单重复提交的应用。

    1:Struts2表单数据校验: (1)前台校验,也称之为客户端校验,主要是通过Javascript编程的方式进行数据的验证. (2)后台校验,也称之为服务器校验,这里指的是使用Struts2通过xm ...

  6. [转] js对象监听实现

    前言 随着前端交互复杂度的提升,各类框架如angular,react,vue等也层出不穷,这些框架一个比较重要的技术点就是数据绑定.数据的监听有较多的实现方案,本文将粗略的描述一番,并对其中一个兼容性 ...

  7. Windows 7 64bit VS2015 配置CUDA

    1. 更新驱动 下载系统显卡驱动,首先在设备管理器中查看自己的显卡型号,我的是GeForce GTX 960,然后在官网下载对应的驱动程序并安装. 官网网址:NVIDIA 驱动程序下载   2. 安装 ...

  8. alpha冲刺5/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺5 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 最近事情有点多,只是跟进了下进度,写了写博客 接下来 ...

  9. [转]如何将mysql表结构导出成Excel格式的(并带备注)

    方法一: 1.使用一个MySQL管理工具:SQLyog,点击菜单栏“数据库”下拉的最后一项: 导出的格式如下: 2.要想转成Excel格式的只需手动将该表复制到Excel中去. 方法二: 1.以下用的 ...

  10. 基于Postman的API自动化测试

    https://segmentfault.com/a/1190000005055899 1. 安装 两种安装方式,我热衷于以chrome插件形式安装 Chrome插件 Mac App 2. 发送请求 ...