二分答案,判断是否存在合法方案使得每个数都不超过$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. Centos7上配置网络和本地yum方法

    配置网络yum源 前提:1.这个系统能上网 2.vim /etc/resolv.conf nameserver 8.8.8.8 nameserver 114.114.114.114 操作如下: 1.m ...

  2. requests禁止重定向

    response = requests.get( 'http://weixin.sogou.com/weixin?query=%E9%A3%8E%E6%99%AF&type=2&pag ...

  3. OpenCV-Python入门教程2-打开摄像头

    一.打开摄像头 import cv2 # 打开摄像头并灰度化显示 capture = cv2.VideoCapture(0) while(True): # 获取一帧 ret, frame = capt ...

  4. python 在WINDOS虚拟环境部署

    #查看电脑的版本 C:\Users\lys>pip -V pip 8.1.1 from e:\python\python3.5\lib\site-packages (python 3.5) #安 ...

  5. Vue-切割json数组字符串并循环输出

    Json "detailList":[ " 04/08/2016 - LA - - Dept. - TITLE (Lien Reported)", " ...

  6. GreenDao3.2的简单使用

    Android -- GreenDao3.2的简单使用http://www.cnblogs.com/wjtaigwh/p/6394288.html https://github.com/greenro ...

  7. mysql配置完半同步复制之后报错[ERROR] The server quit without updating PID file

    修改配置,MySQL启动报:[ERROR] The server quit without updating PID file [root@localhost mysql]# /etc/init.d/ ...

  8. BZOJ4816 [Sdoi2017]数字表格 数论 莫比乌斯反演

    原文链接http://www.cnblogs.com/zhouzhendong/p/8666106.html 题目传送门 - BZOJ4816 题意 定义$f(0)=0,f(1)=1,f(i)=f(i ...

  9. Codeforces 1000G Two-Paths 树形动态规划 LCA

    原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...

  10. BZOJ3675 [Apio2014]序列分割 动态规划 斜率优化

    原文链接http://www.cnblogs.com/zhouzhendong/p/8697258.html 题目传送门 - BZOJ3675 题意 对于一个非负整数序列,小H需要重复k次以下的步骤: ...