Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

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.

Input

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 xivi 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.

Output

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 .

Examples

input

2 999400000 1 2500000 1 1

output

500000.000000000000000000000000000000

input

2 1000400000 500 1600000 500 2

output

400.000000000000000000000000000000

Note

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 }
时间: 2024-12-09 18:42:40

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论的相关文章

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules

Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】

简易字符串匹配,题意不难 1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int main(){ 9 int i, j, k, t, n; 10 int num, flag, ans; 11 char a[300]; 12 sc

Codeforces Round #466 (Div. 2) Problem A - E

从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Numbers Problem D Alena And The Heater Problem E Cashback Problem F Machine Learning (Lazy Tag) 小结 这场比赛和同学一起打.本来应该是很开心的事情,结果留下好多遗憾. 第一个遗憾是没能在20分钟内消灭A.B题

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开始到倒数第二项,当前的a[i]=(a[i-1],a[i],a[i+1])三项排序后的中间项,比如连续3项为 1 0 1,那么中间的就变为1,然后题目让你输出达到稳定状态时所需的最小步数,不能的话输出-1. 无论给你啥数列,都能达到稳态.所以不可能输出-1: 还有一开始就稳定不变,或经过几次变换而稳定

Codeforces Round #425 (Div. 2) B. Petya and Exam(暴力大法好)

题目链接:http://codeforces.com/problemset/problem/832/B 题意:给定一些小写字符(定义为好字符,除这些好字符外的其他小写字符都是坏字符).和字符串S,S里面除了小写字母以外还有?字符,?可以用任何的好字符替代:*字符,*只可以用坏字符串和 空替代,然后给出n个字符串去匹配S字符,问是否能成功. 题解:QAQ,这道题写的我头发又掉了好多. 设拿来匹配S的字符串为T 字符串要匹配,长度要相同吧.*既然这么强大,可以空,那么T的长度最多比S的长度少1:然后

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

Codeforces Round #305 (Div. 2), problem: (A) Mike and Fax

#include<iostream> #include<cstdio> #include<cstring> using namespace std; char a[1000+100]; bool judge(int m,int n) { for(int i=m;i<=(m+n)/2;i++) if(a[i]!=a[m+n-i]) return 0; return 1; } int main() { int k; while(~scanf("%s"