PKU 1064 Cable master
题目链接:点击打开链接
有n段绳子,给定n段绳子的长度,单位为厘米。求能够把这些绳子分成k段的最长的段的长度。题目中的trick是最小是1cm,长度不能小于1cm,因此要转换成int来解,然后二分可以截得的绳子的长度。
#include <iostream> #include <string> #include <algorithm> using namespace std; #define INF 100000000 int l[10009]; int n, k; bool C(int len) { if (len == 0) return false; int sum = 0; for (int i=0; i<n; i++) { sum += (int)(l[i] / len); } if (sum >= k) return true; return false; } int main() { cout.precision(2); while ( cin >> n >> k ) { double x; for (int i=0; i<n; i++) { cin >> x; l[i] = x * 100; } int low = 0, up = INF; for (int i=0; i<100; i++) { int mid = (low + up) / 2; if (C(mid)) low = mid; else up = mid; } if (low < 1) puts("0.00"); else cout << fixed << low * 1.0 / 100 << endl; } return 0; }