AtCoder Beginner Contest 174E Logs(二分)
题意:n段木棍,每个长度为a[i],求切割K次后,使得的最长的木棍长度最短,输出此时最长的木棍长度。n<2e5,k<1e9
题解:切割次数越多,切割后的长度越短,满足单调性,可二分切割后,最长的木棍不大于多少,check判断切割次数是否小于K次即可。
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(0) #define fre freopen("C:\\in.txt", "r", stdin) #define _for(i,a,b) for(int i=a; i< b; i++) #define _rep(i,a,b) for(int i=a; i<=b; i++) #define lowbit(a) ((a)&-(a)) #define inf 0x3f3f3f3f #define endl "\n" using namespace std; typedef long long ll; template <class T> void read(T &x) { char c; bool op=0; while(c=getchar(), c<'0'||c>'9') if(c=='-') op=1; x=c-'0'; while(c=getchar(), c>='0'&&c<='9') x=x*10+c-'0'; if(op) x=-x; } const int maxn=2e5+5; int T, n, k, a[maxn]; int check(int x) { ll step=0; _rep(i, 1, n) if(a[i]>x) step+=a[i]/x; return step<=k? true:false; } int main() { //read(T); T=1; while(T--) { read(n), read(k); _rep(i, 1, n) read(a[i]); int l=1, r=1e9+5; while(l<=r){ int mid=(l+r)>>1; if(check(mid)) r=mid-1; else l=mid+1; } printf("%d\n", l); } return 0; }