forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestlife.cpp
100 lines (83 loc) · 2.63 KB
/
longestlife.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define eps 0.0000001
struct event {
bool pill;
ld time;
ll x, y;
ld slope, yInt;
ld prevSlope, prevYInt;
};
bool operator< (const event &lhs, const event &rhs) {
return lhs.time > rhs.time;
}
int main() {
ll n, p, c;
cin >> n >> p >> c;
priority_queue<event> q;
ll t, x, y;
for(int i = 0; i < p; ++i) {
cin >> t >> x >> y;
event temp;
temp.time = t;
temp.x = x;
temp.y = y;
temp.pill = true;
q.push(temp);
}
ld topSlope = -1;
ld topYInt = n;
while(!q.empty()) {
event curr = q.top();
q.pop();
if(curr.pill) {
//cout << "pill: " << curr.time << ' ' << curr.x << ' ' << curr.y << '\n';
ld yCordinate = topSlope*curr.time + topYInt;
yCordinate -= c;
ld newSlope = -curr.y/(ld)curr.x;
yCordinate -= newSlope*curr.time;
event temp;
temp.pill = false;
temp.slope = newSlope;
temp.yInt = yCordinate;
temp.prevSlope = topSlope;
temp.prevYInt = topYInt;
temp.time = -(yCordinate - topYInt) / (newSlope - topSlope);
if(temp.slope*temp.time + temp.yInt <= 0) {
continue;
}
//cout << "here1\n";
//cout << "times: " << curr.time << ' ' << temp.time << '\n';
if(temp.time > curr.time) {
//cout << "pushing line: " << temp.slope << ' ' << temp.yInt << '\n';
q.push(temp);
}
} else {//line
//cout << "line: " << curr.slope << ' ' << curr.yInt << '\n';
if(topSlope == curr.prevSlope && topYInt == curr.prevYInt) {//replace
topSlope = curr.slope;
topYInt = curr.yInt;
} else {
event temp;
temp.pill = false;
temp.slope = curr.slope;
temp.yInt = curr.yInt;
temp.prevSlope = topSlope;
temp.prevYInt = topYInt;
temp.time = -(curr.yInt - topYInt) / (curr.slope - topSlope);
if(temp.slope*temp.time + temp.yInt <= 0) {
continue;
}
//cout << "here2\n";
if(temp.time > curr.time) {
//cout << "pushing line: " << temp.slope << ' ' << temp.yInt << '\n';
q.push(temp);
}
}
}
}
cout << setprecision(8) << fixed << -topYInt/topSlope << '\n';
return 0;
}