-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathracing.cpp
executable file
·124 lines (99 loc) · 2.25 KB
/
racing.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
#include <cmath>
#include <algorithm>
#define INT_MIN std::numeric_limits<int>::min()
using namespace std;
typedef long double ld;
typedef pair<ld, ld> gem;
struct edge {
int length = 1;
int to;
};
ld slope(gem A, gem B)
{
auto delta_x = B.first - A.first;
auto delta_y = B.second - A.second;
return delta_y / delta_x;
}
void topo_sort(int v, vector<bool> &visited, stack<int> &Stack,
vector< vector<edge> > &Graph)
{
visited[v] = true;
for (auto i = Graph[v].begin(); i != Graph[v].end(); ++i) {
if (!visited[i->to])
topo_sort(i->to, visited, Stack, Graph);
}
Stack.push(v);
}
vector<gem> construct_gems(int N, ld h_start)
{
vector<gem> gems(N + 1);
gems[0] = make_pair(h_start, 0.0);
for (int i = 1; i <= N; i++) {
gem a;
cin >> a.first >> a.second;
gems[i] = a;
}
return gems;
}
vector< vector<edge> > construct_graph(int N, int R, vector<gem> &gems)
{
vector< vector<edge> > graph(N + 1, vector<edge>());
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (gems[j].second > gems[i].second &&
abs(slope(gems[j], gems[i])) >= R) {
edge z;
z.to = j;
graph[i].push_back(z);
}
}
}
return graph;
}
int longest_path(int N, vector< vector<edge> > &graph)
{
stack<int> Stack;
vector<int> dist(N + 1, INT_MIN);
vector<bool> visited(N + 1, false);
dist[0] = 0;
for (int i = 0; i <= N; i++) {
if (visited[i] == false)
topo_sort(i, visited, Stack, graph);
}
while (Stack.empty() == false) {
int u = Stack.top();
Stack.pop();
if (dist[u] != INT_MIN) {
for (auto i = graph[u].begin(); i != graph[u].end();
++i) {
dist[i->to] = max(dist[i->to],
(dist[u] + i->length));
}
}
}
auto ans = *max_element(dist.begin(), dist.end());
return ans;
}
int solve(ld N, ld R, ld W, ld H, ld h_start, vector<gem> &gems)
{
gems[0].first = h_start;
auto graph = construct_graph(N, R, gems);
auto ans = longest_path(N, graph);
return ans;
}
int main()
{
ld N, R, W, H; /* gems, ratio, width, height */
cin >> N >> R >> W >> H;
auto gems = construct_gems(N, 0.0);
int max_ans = 0;
for (ld i = 0; i <= W; ++i) {
max_ans = max(max_ans, solve(N, R, W, H, i, gems));
}
cout << max_ans << endl;
return 0;
}