题目分析:
- 有n条绳子,分为相同的k条,求每条最长
- 浮点数二分法:一条绳子最多分(a[i] / x)条,答案区间[l,r] (db l = 0,r = inf)
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
#define db double
#define ll long long
#define mm(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define pb push_back
#define el endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define fgx cerr<<"-------------------------"<<endl
#define shutio ios::sync_with_stdio(false),cin.tie(0)
#define mk make_pair
#define lowbit(x) (x) & (-x)
#define fi first
#define se second
const int N = 1e4 + 10;
int n,k;
db a[N];
char s[100];
bool check(db x){
int tot = 0;
for(int i = 0; i < n; i ++ ){
tot += (a[i] / x);
}
return tot>=k;
}
int main() {
shutio;
cin >> n >> k;
for(int i = 0; i < n; i ++ ) cin >> a[i];
db l = 0,r = inf;
while(r - l > eps){
db mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
sprintf(s + 1,"%.3f",l);
s[strlen(s + 1)] = '\0';
printf("%s",s + 1);
return 0;
}