https://vjudge.net/problem/POJ-1064
二分就相当于不停地折半试。
C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef unsigned long long ll; 12 using namespace std; 13 int n, m; 14 double L[10010], sum=0; 15 int C(double x)//x为每条边长度 16 { 17 int ans = 0; 18 for(int i = 0; i < n; i++){ 19 ans += (int)(L[i]/x); 20 } 21 if(ans < m) return 0; 22 return 1; 23 } 24 int main() 25 { 26 cin >> n >> m; 27 for(int i = 0; i < n; i++){ 28 cin >> L[i]; 29 sum += L[i]; 30 } 31 sum = sum/m+1; 32 double lb = 0, ub = sum;//ub也可以直接等于INF 33 for(int i = 0; i < 100; i++){ 34 double mid = (lb+ub)/2; 35 if(C(mid)){ 36 lb = mid; 37 } 38 else ub = mid; 39 } 40 printf("%.2lf ", floor(lb*100)/100); //只能这么写,floor不能换成int,总之这是一个精度坑 41 return 0; 42 }