标签:就是 else 简单的 main col const cstring for ace
这道题就是二分枚举加贪心,小蓝书上一开始就讲的,但是我给忘了,很难受
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5+5; long long n,m; long long a[maxn]; bool judge(long long ans); int main() { long long mid; while(scanf("%lld%lld",&n,&m) != EOF) { long long l,r; long long ans; for(long long i = 0; i < n; ++i) scanf("%lld",a+i); sort(a,a+n); l = 0,r=a[n-1]+1;
//枚举长度为mid的区间 相当于每个点管理一定长度的区间
//遇见管理不到的点 就再加一个点去管理mid的区间
//相当于暴力了 也挺简单的 while(l <= r) { mid = (l+r)/2; if(judge(mid)) { ans = mid; r = mid-1; } else { l = mid+1; } } printf("%lld\n",ans); } } bool judge(long long ans) { long long num=1; long long ma = a[0]+ans; for(long long i=0;i<n;++i) { if(ma < a[i]) { ma = a[i]+ans; num++; } } return num <= m; }
标签:就是 else 简单的 main col const cstring for ace
原文地址:https://www.cnblogs.com/mltang/p/8977993.html