二分答案,判断是否存在合法方案使得每个数都不超过$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. 步步为营101-同一个PCode下重复的OrderNumber重新排序

    USE [K2_WorkFlow_Test] GO /****** Object: StoredProcedure [dbo].[sp_UpdateBPM_DictionaryForOrderNumb ...

  2. 如果拷贝项目出现各种找不到文件的时候,基本就是没有标记,或者文件名的问题,Could not find resource mybatis.xml,解决方法

    Could not find resource mybatis.xml

  3. python练习册0005

    第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 本题用了几个os模块的命令, import os from PIL import Image p ...

  4. python pip install mysql-python报错

    报错: 下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

  5. Redis都有哪些数据类型

    string 这是最基本的类型了,就是普通的set和get,做简单的kv缓存 hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象没嵌套其他的对象)给缓存 ...

  6. 将Elasticsearch的快照备份到HDFS

    1.安装Elasticsearch插件repository-hdfs 下载地址:https://artifacts.elastic.co/downloads/elasticsearch-plugins ...

  7. DBEntityEntry类

    DBEntityEntry是一个重要的类,可用于检索有关实体的各种信息.您可以使用DBContext的Entry方法获取特定实体的DBEntityEntry实例. DBEntityEntry允许您访问 ...

  8. thinkphp调用微信jssdk开发

    一:准备文件,并将文件置于网站根目录下 access_token.json {"access_token":"","expire_time" ...

  9. angular 2+ 变化检测系列一(基础概念)

    什么是变化检测? 变化检测的基本功能就是获取应用程序的内部状态(state),并且是将这种状态对用户界面保持可见.状态可以是javascript中的任何的数据结构,比如对象,数组,(数字,布尔,字符串 ...

  10. 分布式系统的BASE理论

    一.BASE理论 eBay的架构师Dan Pritchett源于对大规模分布式系统的实践总结,在ACM上发表文章提出BASE理论,BASE理论是对CAP理论的延伸,核心思想是即使无法做到强一致性(St ...