forked from sukhoeing/aoapc-bac2nd-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa1616.cc
69 lines (67 loc) · 1.63 KB
/
UVa1616.cc
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Caravan Robbers, ACM/ICPC NEERC 2012, UVa1616
// 陈锋
#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const double eps = 1e-7;
int dcmp(double x) {
if (fabs(x) < eps)
return 0;
return x < 0 ? -1 : 1;
}
int dcmp(double x, double y) { return dcmp(x - y); }
struct Seg {
int a, b;
bool operator<(const Seg &s) const { return a <= s.a; }
};
const int NN = 1e5 + 4;
int n;
Seg segs[NN];
bool tryLen(const double l) {
double lb = 0;
_for(i, 0, n) {
const Seg &s = segs[i];
lb = max((double)s.a, lb) + l;
if (lb > s.b)
return false;
}
return true;
}
void output(double l) {
double ip = floor(l + eps);
if (dcmp(ip, l) == 0) {
printf("%.0lf/1\n", ip);
return;
}
int p = 1, q = 1; // p/q
double ans = 1; // 目前为止最接近l的p/q值
for (int i = 1; i <= n; i++) { // i作为分母
int cp = (int)(floor(l * (double)i + 0.5)); // 可能的分子
double x = (double)cp / i;
if (fabs(x - l) < fabs(ans - l))
q = i, p = cp, ans = x;
}
int g = gcd(p, q);
printf("%d/%d\n", p / g, q / g);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
while (cin >> n) {
_for(i, 0, n) cin >> segs[i].a >> segs[i].b;
sort(segs, segs + n);
double l = 1, r = 1e6 / n;
assert(dcmp(l, r) <= 0);
_for(b, 0, 50) {
double m = (l + r) / 2;
if (!tryLen(m))
r = m;
else
l = m;
}
output((l + r) / 2);
}
return 0;
}
// 20497012 1616 Caravan Robbers Accepted C++11 0.070 2017-12-16
// 11:44:52