# 再谈二分

#include<cstdio>
using namespace std;
int n,m,a[139],l,r,mid,b[139];
long long ans;
int main(){
register int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
a[n+1]=m;
l=0;r=2100000001;
while(l+1<r){
mid=(l+r)>>1;
ans=0;
for(i=1;i<=n+1;i++) b[i]=a[i];
for(i=1;i<=n+1;i++){
if(b[i]<mid) ans+=mid-b[i];
}
if(ans<=mid)l=mid;
else r=mid;
}
printf("%d",l);
}

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[1000039],l,r,mid,ans,tot,pus,flag,s[1000039];
int main() {
register int i;
scanf("%d%d",&n,&m);
for(i=1; i<=2*n*m; i++) scanf("%d",&a[i]);
sort(a+1,a+2*n*m+1);
l=-1;
r=1000000001;
while(l+1<r) {
mid=(l+r)>>1;
ans=tot=pus=flag=0;
memset(s,0,sizeof(s));
for(i=2; i<=2*n*m; i++) {
if(a[i]-a[i-1]<=mid&&!s[i-1])ans++,s[i]=s[i-1]=ans;
if(ans==n)break;
}
if(ans==n) {
for(i=2*n*m; i>=2; i--) {
if(!s[i]) tot++;
if(s[i]&&s[i]==s[i-1]) {
pus++;
if(tot<2*pus*(m-1)) {
flag=1;
break;
}
}
}
} else flag=1;
if(flag) l=mid;
else r=mid;
}
printf("%d\n",r);
}

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,a[1039],b[59],l,r,mid,ans,tot,flag,now,s[59],q[1059];
inline void dfs(register int x,register int last) {
if(flag) return;
if(!x){flag=1;return;}
if(now+q[mid]>ans) return;
for(register int i=last; i<=n; i++) {
if(b[i]>=a[x]){
b[i]-=a[x];
if(b[i]<a[1]) now+=b[i];
if(a[x]==a[x-1]) dfs(x-1,i);
else dfs(x-1,1);
if(b[i]<a[1]) now-=b[i];
b[i]+=a[x];
if(flag) return;
}
}
}
int main() {
register int i;
scanf("%d",&n);
for(i=1; i<=n; i++)scanf("%d",&b[i]),ans+=b[i];
scanf("%d",&m);
for(i=1; i<=m; i++) scanf("%d",&a[i]);
sort(a+1,a+m+1);
sort(b+1,b+n+1);
for(i=1;i<=m;i++) q[i]=q[i-1]+a[i];
while(q[m]>ans) m--;
l=1;
r=m;
while(l<=r) {
mid=(l+r)>>1;
flag=0;now=0;
dfs(mid,1);
if(flag) l=mid+1;
else r=mid-1;
}
if(!n) printf("14");
else printf("%d",l-1);
}

if(!n) printf("14");

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客