Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed.
You can put a bomb in some point with non-negative integer coordinate, and blow it up. At this moment all people will start running with their maximum speed in the direction they are facing. Also, two strange rays will start propagating from the bomb with speed s: one to the right, and one to the left. Of course, the speed s is strictly greater than people's maximum speed.
The rays are strange because if at any moment the position and the direction of movement of some ray and some person coincide, then the speed of the person immediately increases by the speed of the ray.
You need to place the bomb is such a point that the minimum time moment in which there is a person that has run through point 0, and there is a person that has run through point 106, is as small as possible. In other words, find the minimum time moment t such that there is a point you can place the bomb to so that at time moment t some person has run through 0, and some person has run through point106.
The first line contains two integers n and s (2 ≤ n ≤ 105, 2 ≤ s ≤ 106) — the number of people and the rays' speed.
The next n lines contain the description of people. The i-th of these lines contains three integers xi, vi and ti (0 < xi < 106, 1 ≤ vi < s,1 ≤ ti ≤ 2) — the coordinate of the i-th person on the line, his maximum speed and the direction he will run to (1 is to the left, i.e. in the direction of coordinate decrease, 2 is to the right, i.e. in the direction of coordinate increase), respectively.
It is guaranteed that the points 0 and 106 will be reached independently of the bomb's position.
Print the minimum time needed for both points 0 and 106 to be reached.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6. Namely, if your answer is a, and the jury's answer is b, then your answer is accepted, if .
2 999
400000 1 2
500000 1 1
500000.000000000000000000000000000000
2 1000
400000 500 1
600000 500 2
400.000000000000000000000000000000
In the first example, it is optimal to place the bomb at a point with a coordinate of 400000. Then at time 0, the speed of the first person becomes 1000 and he reaches the point 106 at the time 600. The bomb will not affect on the second person, and he will reach the 0point at the time 500000.
In the second example, it is optimal to place the bomb at the point 500000. The rays will catch up with both people at the time 200. At this time moment, the first is at the point with a coordinate of 300000, and the second is at the point with a coordinate of 700000. Their speed will become 1500 and at the time 400 they will simultaneously run through points 0 and 106.
题目大意 数轴上有n个(每个人的位置大于0且小于106),每个人有一个朝向和一个最大速度。有一个神奇的炸弹,可以在一个非负整数点引爆(并不知道引爆的位置),并向两边射出速度s个单位每秒的光线(其速度严格大于人的速度),如果这个光线碰到人,且和人的朝向一样,那么人的最大速度会加上这个速度,引爆后所有人开始向他面向的方向全速奔跑(不考虑体力)。问最少需要多长时间使得点0和1e6被人到达了(不一定是同一个人)。
显然是二分答案。现在来思考判定。
当前二分的答案为mid,现在判断是否有人能够达到点0和1e6。
如果没有,就考虑一下用炸弹爆炸后的光线来加速。显然可以放炸弹的地方是一个区间(假设我们会算它,然后继续)。
然后判定的问题转化成判断两组区间,是否存在一对(在不同组内)的交集包含整点,这个就可以通过排序加二分查找解决,做法类似于codeforces 822C。
现在来思考如何计算这个区间(假定读者小学奥数学得还不错)
假定人在点B处,它的终点为C,在点A处放置炸弹,AB = s1, BC = dist,光线在点D处追上人,耗时t0,人的速度为v0,光线的速度为vs。所以有:
有了最大的追及时间就可以得到追及路程s1 = t0vs。
然后就可以交代码去codeforces了。
然而昨天晚上打比赛的时候,为了图快,没仔细读题,把题目大意读成所有人都离开(0, 1e6)的最少耗时,然而谜之Wrong Answer on pretest 3。还耗掉我一个小时,早知道该去切D题。
Code
1 /** 2 * Codeforces 3 * Problem#832C 4 * Accepted 5 * Time:155ms 6 * Memory:7400k 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <ctime> 11 #include <cmath> 12 #include <cctype> 13 #include <cstring> 14 #include <cstdlib> 15 #include <fstream> 16 #include <sstream> 17 #include <algorithm> 18 #include <map> 19 #include <set> 20 #include <stack> 21 #include <queue> 22 #include <vector> 23 #include <stack> 24 #ifndef WIN32 25 #define Auto "%lld" 26 #else 27 #define Auto "%I64d" 28 #endif 29 using namespace std; 30 typedef bool boolean; 31 const signed int inf = (signed)((1u << 31) - 1); 32 const signed long long llf = (signed long long)((1ull << 61) - 1); 33 const double eps = 1e-9; 34 const int binary_limit = 256; 35 #define smin(a, b) a = min(a, b) 36 #define smax(a, b) a = max(a, b) 37 #define max3(a, b, c) max(a, max(b, c)) 38 #define min3(a, b, c) min(a, min(b, c)) 39 template<typename T> 40 inline boolean readInteger(T& u){ 41 char x; 42 int aFlag = 1; 43 while(!isdigit((x = getchar())) && x != '-' && x != -1); 44 if(x == -1) { 45 ungetc(x, stdin); 46 return false; 47 } 48 if(x == '-'){ 49 x = getchar(); 50 aFlag = -1; 51 } 52 for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); 53 ungetc(x, stdin); 54 u *= aFlag; 55 return true; 56 } 57 58 int opt = 1; 59 typedef class Segment { 60 public: 61 double l; 62 double r; 63 64 Segment(double l = 0.0, double r = 0.0):l(l), r(r) { } 65 66 Segment getBing(Segment b) { 67 return Segment(max(l, b.l), min(r, b.r)); 68 } 69 70 boolean hasint() { 71 return (int)l != (int)r; 72 } 73 74 boolean isResonable() { 75 return l <= r; 76 } 77 78 boolean operator < (Segment b) const { 79 if(opt == 1) { 80 if(l != b.l) return l < b.l; 81 return r < b.r; 82 } 83 if(r != b.r) return r < b.r; 84 return l < b.l; 85 } 86 87 }Segment; 88 89 boolean operator < (int x, Segment a) { 90 if(opt == 1) { 91 return x < a.l; 92 } 93 return x < a.r; 94 } 95 96 int n, s; 97 int pos[100005], spe[100005], ds[100005]; 98 99 inline void init() { 100 readInteger(n); 101 readInteger(s); 102 for(int i = 1; i <= n; i++) { 103 readInteger(pos[i]); 104 readInteger(spe[i]); 105 readInteger(ds[i]); 106 } 107 } 108 109 inline double calcS(double vs, double v0, double mid, double dist) { 110 double t0 = (mid * (v0 + vs) - dist) / vs; 111 return t0 * (vs - v0); 112 } 113 114 /* 115 inline int ceil(double a) { 116 if(a == (int)a) return a; 117 return (int)a + 1; 118 } 119 */ 120 121 inline boolean check(double mid) { 122 double dist, t; 123 boolean f1 = false, f2 = false; 124 vector<Segment> v1, v2, v3; 125 for(int i = 1; i <= n && (!f1 || !f2); i++) { 126 if(ds[i] == 1 && !f1) { 127 dist = pos[i]; 128 t = dist / spe[i]; 129 if(t > mid) { 130 double s1 = calcS(s, spe[i], mid, dist); 131 if(s1 < 0) continue; 132 v1.push_back(Segment(pos[i], pos[i] + s1)); 133 v2.push_back(Segment(pos[i], pos[i] + s1)); 134 } else f1 = true; 135 } else if(ds[i] == 2 && !f2) { 136 dist = 1e6 - pos[i]; 137 t = dist / spe[i]; 138 if(t > mid) { 139 double s1 = calcS(s, spe[i], mid, dist); 140 if(s1 < 0) continue; 141 v3.push_back(Segment(pos[i] - s1, pos[i])); 142 } else f2 = true; 143 } 144 } 145 if(f1 && f2) return true; 146 if(f1 && !v3.empty()) return true; 147 if(f2 && !v1.empty()) return true; 148 opt = 1; 149 sort(v1.begin(), v1.end()); 150 opt = 2; 151 sort(v2.begin(), v2.end()); 152 int sv1 = (signed)v1.size(); 153 for(int i = 0, s; i < v3.size(); i++) { 154 Segment b = v3[i]; 155 opt = 1; 156 s = sv1 - (upper_bound(v1.begin(), v1.end(), (int)v3[i].r) - v1.begin()); 157 opt = 2; 158 s += upper_bound(v2.begin(), v2.end(), (int)ceil(v3[i].l)) - v2.begin(); 159 if(s < sv1) 160 return true; 161 } 162 return false; 163 } 164 165 inline void solve() { 166 int cnt = 0; 167 double l = 0.0, r = 1e6; 168 while(l + eps < r && cnt <= binary_limit) { 169 double mid = (l + r) / 2; 170 cnt++; 171 if(check(mid)) r = mid; 172 else l = mid; 173 } 174 printf("%.9lf", r); 175 } 176 177 int main() { 178 init(); 179 solve(); 180 return 0; 181 }