注意几点:

  • 二分停止的条件是上下边界的差值小于 eps ,而非时间的值与t的差值小于 eps 。

  • cout的double只能输出小数点后 6 位,要输出更多需在输出语句前加

    1
    cout << setprecision(t);

    便可输出小数点后 t 位

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>
#include<cmath>
#include <iomanip>
using namespace std;
typedef pair<int, int> pii;
int n, t;
const double eps = 1e-9;
double c=0;
double ed, st;
vector<pii>vc;
double solve() {
double ans = 0.0;
for (auto it : vc) {
ans += 1.0*it.first / (it.second + c);
}
return ans;
}
int main() {
cin >> n >> t;
int mins=1e9;
for (int i = 0; i < n; i++) {
int d, s;
cin >> d >> s;
if (s <mins)mins = s;
vc.push_back(pii(d, s));
}
st = 0-mins;
ed = 1e9;
c = (st + ed)*1.0 / 2;
double tmp;
while (ed-st>=eps) {
c = 1.0*(st + ed) / 2;
tmp = solve() - t;
if (tmp >=eps) {
st = c;
}
else {
ed = c;
}
}
cout << setprecision(7);
cout << c;
return 0;
}