#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define f first
#define s second
const int MAX = 30005;
const int SQRT = 125;
const int INF = 1000000000;
int p[MAX], len[MAX]; // position, length
vector<pair<pii, int> > g[MAX][SQRT];
vector<int> powers[MAX]; // powers of doges at a given position
int dist[MAX][SQRT];
priority_queue<pair<int, pair<int, int> > > pq;
int xd = 0;
void dijk(int i, int j) {
dist[i][j] = 0;
//cout << i << " " << j << " " << 0 << endl;
pq.push({0, {i, j}});
while (!pq.empty()) {
xd += 1;
auto top = pq.top(); pq.pop();
int d = -top.f;
pair<int, int> loc = top.s;
if (d > dist[loc.f][loc.s]) {
continue;
}
//cout << loc.f << " " << loc.s << endl;
for (auto i : g[loc.f][loc.s]) {
//cout << i.f.f << " " << i.f.s << endl;
int d_new = (d + i.s);
if (d_new < dist[i.f.f][i.f.s]) {
dist[i.f.f][i.f.s] = d_new;
//cout << "came from: " << loc.f << " " << loc.s << endl;
//cout << i.f.f << " " << i.f.s << " " << d_new << endl;
pq.push({-d_new, i.f});
}
}
}
}
int main() {
//freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
for (int i=0; i<MAX; i++) {
for (int j=0; j<SQRT; j++) {
dist[i][j] = INF;
}
}
int n, m; scanf("%d %d", &n, &m);
for (int i=0; i<m; i++) {
scanf("%d %d", &p[i], &len[i]);
powers[p[i]].push_back(len[i]);
}
for (int i=0; i<n; i++) {
for (int j=1; j<SQRT; j++) {
g[i][j].push_back({{i, 0}, 0});
}
for (int j : powers[i]) {
if (j < SQRT) {
g[i][0].push_back({{i, j}, 0});
}
}
}
for (int i=0; i<m; i++) {
if (len[i] > SQRT) {
for (int j=p[i]+len[i]; j<n; j+=len[i]) {
g[p[i]][0].push_back({{j, 0}, (j-p[i])/len[i]});
}
for (int j=p[i]-len[i]; j>=0; j-=len[i]) {
g[p[i]][0].push_back({{j, 0}, (p[i]-j)/len[i]});
}
}
}
for (int i=0; i<n; i++) {
for (int j=1; j<SQRT; j++) {
if (i-j >= 0) { g[i][j].push_back({{i-j, j}, 1}); }
if (i+j < n) { g[i][j].push_back({{i+j, j}, 1}); }
}
}
/*
int ct = 0;
for (int i=0; i<MAX; i++) {
for (int j=0; j<SQRT; j++) {
ct += g[i][j].size();
}
}
cout << ct << endl;
*/
dijk(p[0], 0);
if (dist[p[1]][0] == INF) { printf("%d\n", -1); }
else { printf("%d\n", dist[p[1]][0]); }
//cout << xd << endl;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:59:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m; scanf("%d %d", &n, &m);
^
skyscraper.cpp:61:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &p[i], &len[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
105512 KB |
Output is correct |
2 |
Correct |
23 ms |
105512 KB |
Output is correct |
3 |
Correct |
33 ms |
105512 KB |
Output is correct |
4 |
Correct |
23 ms |
105512 KB |
Output is correct |
5 |
Correct |
29 ms |
105512 KB |
Output is correct |
6 |
Correct |
26 ms |
105512 KB |
Output is correct |
7 |
Correct |
33 ms |
105512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
105512 KB |
Output is correct |
2 |
Correct |
23 ms |
105512 KB |
Output is correct |
3 |
Correct |
26 ms |
105512 KB |
Output is correct |
4 |
Correct |
36 ms |
105512 KB |
Output is correct |
5 |
Correct |
26 ms |
105512 KB |
Output is correct |
6 |
Correct |
29 ms |
105512 KB |
Output is correct |
7 |
Correct |
23 ms |
105512 KB |
Output is correct |
8 |
Correct |
19 ms |
105644 KB |
Output is correct |
9 |
Correct |
26 ms |
105776 KB |
Output is correct |
10 |
Correct |
26 ms |
106040 KB |
Output is correct |
11 |
Correct |
29 ms |
106172 KB |
Output is correct |
12 |
Correct |
29 ms |
106040 KB |
Output is correct |
13 |
Correct |
29 ms |
106040 KB |
Output is correct |
14 |
Correct |
39 ms |
106172 KB |
Output is correct |
15 |
Correct |
39 ms |
106172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
105512 KB |
Output is correct |
2 |
Correct |
29 ms |
105512 KB |
Output is correct |
3 |
Correct |
26 ms |
105512 KB |
Output is correct |
4 |
Correct |
29 ms |
105512 KB |
Output is correct |
5 |
Correct |
39 ms |
105512 KB |
Output is correct |
6 |
Correct |
36 ms |
105512 KB |
Output is correct |
7 |
Correct |
23 ms |
105512 KB |
Output is correct |
8 |
Correct |
33 ms |
105644 KB |
Output is correct |
9 |
Correct |
29 ms |
105776 KB |
Output is correct |
10 |
Correct |
39 ms |
106040 KB |
Output is correct |
11 |
Correct |
16 ms |
106172 KB |
Output is correct |
12 |
Correct |
33 ms |
106040 KB |
Output is correct |
13 |
Correct |
26 ms |
106040 KB |
Output is correct |
14 |
Correct |
33 ms |
106172 KB |
Output is correct |
15 |
Correct |
29 ms |
106172 KB |
Output is correct |
16 |
Correct |
43 ms |
106704 KB |
Output is correct |
17 |
Correct |
49 ms |
110924 KB |
Output is correct |
18 |
Correct |
59 ms |
118712 KB |
Output is correct |
19 |
Correct |
73 ms |
120692 KB |
Output is correct |
20 |
Correct |
86 ms |
120692 KB |
Output is correct |
21 |
Correct |
36 ms |
108152 KB |
Output is correct |
22 |
Correct |
73 ms |
118980 KB |
Output is correct |
23 |
Correct |
56 ms |
117524 KB |
Output is correct |
24 |
Correct |
83 ms |
119900 KB |
Output is correct |
25 |
Correct |
69 ms |
120692 KB |
Output is correct |
26 |
Correct |
69 ms |
120692 KB |
Output is correct |
27 |
Correct |
79 ms |
120696 KB |
Output is correct |
28 |
Correct |
73 ms |
120692 KB |
Output is correct |
29 |
Correct |
76 ms |
120692 KB |
Output is correct |
30 |
Correct |
89 ms |
120560 KB |
Output is correct |
31 |
Correct |
113 ms |
120700 KB |
Output is correct |
32 |
Correct |
69 ms |
120692 KB |
Output is correct |
33 |
Correct |
119 ms |
120692 KB |
Output is correct |
34 |
Correct |
123 ms |
120692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
105512 KB |
Output is correct |
2 |
Correct |
26 ms |
105512 KB |
Output is correct |
3 |
Correct |
23 ms |
105512 KB |
Output is correct |
4 |
Correct |
26 ms |
105512 KB |
Output is correct |
5 |
Correct |
26 ms |
105512 KB |
Output is correct |
6 |
Correct |
23 ms |
105512 KB |
Output is correct |
7 |
Correct |
26 ms |
105512 KB |
Output is correct |
8 |
Correct |
26 ms |
105644 KB |
Output is correct |
9 |
Correct |
26 ms |
105776 KB |
Output is correct |
10 |
Correct |
36 ms |
106040 KB |
Output is correct |
11 |
Correct |
16 ms |
106172 KB |
Output is correct |
12 |
Correct |
26 ms |
106040 KB |
Output is correct |
13 |
Correct |
33 ms |
106040 KB |
Output is correct |
14 |
Correct |
29 ms |
106172 KB |
Output is correct |
15 |
Correct |
33 ms |
106172 KB |
Output is correct |
16 |
Correct |
33 ms |
106704 KB |
Output is correct |
17 |
Correct |
43 ms |
110924 KB |
Output is correct |
18 |
Correct |
79 ms |
118712 KB |
Output is correct |
19 |
Correct |
69 ms |
120692 KB |
Output is correct |
20 |
Correct |
66 ms |
120692 KB |
Output is correct |
21 |
Correct |
29 ms |
108152 KB |
Output is correct |
22 |
Correct |
69 ms |
118980 KB |
Output is correct |
23 |
Correct |
76 ms |
117524 KB |
Output is correct |
24 |
Correct |
76 ms |
119900 KB |
Output is correct |
25 |
Correct |
73 ms |
120692 KB |
Output is correct |
26 |
Correct |
86 ms |
120692 KB |
Output is correct |
27 |
Correct |
69 ms |
120696 KB |
Output is correct |
28 |
Correct |
89 ms |
120692 KB |
Output is correct |
29 |
Correct |
93 ms |
120692 KB |
Output is correct |
30 |
Correct |
83 ms |
120560 KB |
Output is correct |
31 |
Correct |
76 ms |
120700 KB |
Output is correct |
32 |
Correct |
86 ms |
120692 KB |
Output is correct |
33 |
Correct |
113 ms |
120692 KB |
Output is correct |
34 |
Correct |
106 ms |
120692 KB |
Output is correct |
35 |
Correct |
106 ms |
116996 KB |
Output is correct |
36 |
Correct |
53 ms |
113696 KB |
Output is correct |
37 |
Correct |
123 ms |
121672 KB |
Output is correct |
38 |
Correct |
129 ms |
122068 KB |
Output is correct |
39 |
Correct |
109 ms |
122068 KB |
Output is correct |
40 |
Correct |
149 ms |
122068 KB |
Output is correct |
41 |
Correct |
139 ms |
122068 KB |
Output is correct |
42 |
Correct |
89 ms |
121140 KB |
Output is correct |
43 |
Correct |
69 ms |
121184 KB |
Output is correct |
44 |
Correct |
73 ms |
121240 KB |
Output is correct |
45 |
Correct |
156 ms |
125256 KB |
Output is correct |
46 |
Correct |
179 ms |
125172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
105512 KB |
Output is correct |
2 |
Correct |
33 ms |
105512 KB |
Output is correct |
3 |
Correct |
33 ms |
105512 KB |
Output is correct |
4 |
Correct |
29 ms |
105512 KB |
Output is correct |
5 |
Correct |
39 ms |
105512 KB |
Output is correct |
6 |
Correct |
29 ms |
105512 KB |
Output is correct |
7 |
Correct |
26 ms |
105512 KB |
Output is correct |
8 |
Correct |
33 ms |
105644 KB |
Output is correct |
9 |
Correct |
26 ms |
105776 KB |
Output is correct |
10 |
Correct |
29 ms |
106040 KB |
Output is correct |
11 |
Correct |
26 ms |
106172 KB |
Output is correct |
12 |
Correct |
23 ms |
106040 KB |
Output is correct |
13 |
Correct |
33 ms |
106040 KB |
Output is correct |
14 |
Correct |
26 ms |
106172 KB |
Output is correct |
15 |
Correct |
36 ms |
106172 KB |
Output is correct |
16 |
Correct |
23 ms |
106704 KB |
Output is correct |
17 |
Correct |
46 ms |
110924 KB |
Output is correct |
18 |
Correct |
56 ms |
118712 KB |
Output is correct |
19 |
Correct |
63 ms |
120692 KB |
Output is correct |
20 |
Correct |
73 ms |
120692 KB |
Output is correct |
21 |
Correct |
23 ms |
108152 KB |
Output is correct |
22 |
Correct |
66 ms |
118980 KB |
Output is correct |
23 |
Correct |
83 ms |
117524 KB |
Output is correct |
24 |
Correct |
76 ms |
119900 KB |
Output is correct |
25 |
Correct |
76 ms |
120692 KB |
Output is correct |
26 |
Correct |
76 ms |
120692 KB |
Output is correct |
27 |
Correct |
73 ms |
120696 KB |
Output is correct |
28 |
Correct |
83 ms |
120692 KB |
Output is correct |
29 |
Correct |
109 ms |
120692 KB |
Output is correct |
30 |
Correct |
76 ms |
120560 KB |
Output is correct |
31 |
Correct |
103 ms |
120700 KB |
Output is correct |
32 |
Correct |
79 ms |
120692 KB |
Output is correct |
33 |
Correct |
106 ms |
120692 KB |
Output is correct |
34 |
Correct |
96 ms |
120692 KB |
Output is correct |
35 |
Correct |
89 ms |
116996 KB |
Output is correct |
36 |
Correct |
56 ms |
113696 KB |
Output is correct |
37 |
Correct |
96 ms |
121672 KB |
Output is correct |
38 |
Correct |
93 ms |
122068 KB |
Output is correct |
39 |
Correct |
103 ms |
122068 KB |
Output is correct |
40 |
Correct |
123 ms |
122068 KB |
Output is correct |
41 |
Correct |
99 ms |
122068 KB |
Output is correct |
42 |
Correct |
56 ms |
121140 KB |
Output is correct |
43 |
Correct |
69 ms |
121184 KB |
Output is correct |
44 |
Correct |
76 ms |
121240 KB |
Output is correct |
45 |
Correct |
176 ms |
125256 KB |
Output is correct |
46 |
Correct |
136 ms |
125172 KB |
Output is correct |
47 |
Correct |
533 ms |
207732 KB |
Output is correct |
48 |
Memory limit exceeded |
379 ms |
262144 KB |
Memory limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |