// CF template, version 3.0
#include <bits/stdc++.h>
using namespace std;
#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}
#define int long long int
signed main() {
improvePerformance;
get(n);
get(m);
vector<pair<int, int>> doges;
vector<vector<bool>> has(n, vector<bool>(2001, false));
vector<bool> one(n, false);
vector<vector<int>> div(n + 1);
for (int d = 1; d <= n; d++) {
for (int j = d; j <= n; j += d) {
div[j].push_back(d);
}
}
forto(m, i) {
get(b);
get(p);
doges.push_back({b, p});
has[b][p] = true;
one[b] = true;
}
vector<vector<pair<int, int>>> adjList(n);
forto(n, i) {
forto(n, j) {
if (i == j) continue;
if (!one[j]) continue;
int maxdiv = -1;
int dist = abs(j - i);
for (int divisor: div[dist]) {
if (has[i][divisor]) maxdiv = divisor;
}
if (maxdiv != -1) adjList[i].push_back({j, dist / maxdiv});
}
}
vector<int> shortest(n, 1e18);
vector<bool> visited(n, false);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int start = doges[0].first;
pq.push({0, start});
while (!pq.empty()) {
auto top = pq.top();
int dist = top.first;
int node = top.second;
pq.pop();
if (visited[node]) continue;
visited[node] = true;
shortest[node] = min(shortest[node], dist);
for (auto con: adjList[node]) {
int dest = con.first;
int weight = con.second;
if (!visited[dest]) pq.push({shortest[node] + weight, dest});
}
}
int pos = doges[1].first;
if (shortest[pos] == 1e18) out(-1);
else out(shortest[pos]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
skyscraper.cpp:23:5: note: in expansion of macro 'get'
23 | get(n);
| ^~~
skyscraper.cpp:10:23: warning: unnecessary parentheses in declaration of 'm' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
skyscraper.cpp:24:5: note: in expansion of macro 'get'
24 | get(m);
| ^~~
skyscraper.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
skyscraper.cpp:34:5: note: in expansion of macro 'forto'
34 | forto(m, i) {
| ^~~~~
skyscraper.cpp:10:23: warning: unnecessary parentheses in declaration of 'b' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
skyscraper.cpp:35:9: note: in expansion of macro 'get'
35 | get(b);
| ^~~
skyscraper.cpp:10:23: warning: unnecessary parentheses in declaration of 'p' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
skyscraper.cpp:36:9: note: in expansion of macro 'get'
36 | get(p);
| ^~~
skyscraper.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
skyscraper.cpp:42:5: note: in expansion of macro 'forto'
42 | forto(n, i) {
| ^~~~~
skyscraper.cpp:15:35: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
skyscraper.cpp:43:9: note: in expansion of macro 'forto'
43 | forto(n, j) {
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
9 ms |
1288 KB |
Output is correct |
18 |
Correct |
35 ms |
1160 KB |
Output is correct |
19 |
Correct |
27 ms |
1284 KB |
Output is correct |
20 |
Correct |
397 ms |
81992 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
30 ms |
1172 KB |
Output is correct |
23 |
Correct |
36 ms |
1100 KB |
Output is correct |
24 |
Correct |
57 ms |
1400 KB |
Output is correct |
25 |
Correct |
54 ms |
1360 KB |
Output is correct |
26 |
Correct |
8 ms |
1332 KB |
Output is correct |
27 |
Correct |
10 ms |
1232 KB |
Output is correct |
28 |
Correct |
43 ms |
1668 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
8 ms |
1264 KB |
Output is correct |
31 |
Correct |
7 ms |
1236 KB |
Output is correct |
32 |
Correct |
6 ms |
1236 KB |
Output is correct |
33 |
Correct |
6 ms |
1364 KB |
Output is correct |
34 |
Correct |
9 ms |
1336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
9 ms |
1288 KB |
Output is correct |
18 |
Correct |
39 ms |
1152 KB |
Output is correct |
19 |
Correct |
32 ms |
1280 KB |
Output is correct |
20 |
Correct |
414 ms |
82040 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
28 ms |
1172 KB |
Output is correct |
23 |
Correct |
32 ms |
1100 KB |
Output is correct |
24 |
Correct |
51 ms |
1532 KB |
Output is correct |
25 |
Correct |
54 ms |
1356 KB |
Output is correct |
26 |
Correct |
8 ms |
1304 KB |
Output is correct |
27 |
Correct |
7 ms |
1224 KB |
Output is correct |
28 |
Correct |
40 ms |
1604 KB |
Output is correct |
29 |
Correct |
5 ms |
1252 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
32 |
Correct |
9 ms |
1236 KB |
Output is correct |
33 |
Correct |
9 ms |
1352 KB |
Output is correct |
34 |
Correct |
9 ms |
1324 KB |
Output is correct |
35 |
Correct |
38 ms |
5480 KB |
Output is correct |
36 |
Correct |
14 ms |
1492 KB |
Output is correct |
37 |
Correct |
73 ms |
9432 KB |
Output is correct |
38 |
Correct |
71 ms |
8532 KB |
Output is correct |
39 |
Correct |
72 ms |
8892 KB |
Output is correct |
40 |
Correct |
73 ms |
8680 KB |
Output is correct |
41 |
Correct |
73 ms |
8396 KB |
Output is correct |
42 |
Correct |
12 ms |
2000 KB |
Output is correct |
43 |
Correct |
10 ms |
2152 KB |
Output is correct |
44 |
Correct |
408 ms |
82596 KB |
Output is correct |
45 |
Correct |
14 ms |
2764 KB |
Output is correct |
46 |
Correct |
14 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
8 ms |
1364 KB |
Output is correct |
18 |
Correct |
35 ms |
1160 KB |
Output is correct |
19 |
Correct |
27 ms |
1280 KB |
Output is correct |
20 |
Correct |
393 ms |
81992 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
29 ms |
1152 KB |
Output is correct |
23 |
Correct |
31 ms |
1108 KB |
Output is correct |
24 |
Correct |
49 ms |
1412 KB |
Output is correct |
25 |
Correct |
54 ms |
1472 KB |
Output is correct |
26 |
Correct |
8 ms |
1336 KB |
Output is correct |
27 |
Correct |
7 ms |
1236 KB |
Output is correct |
28 |
Correct |
39 ms |
1624 KB |
Output is correct |
29 |
Correct |
5 ms |
1296 KB |
Output is correct |
30 |
Correct |
6 ms |
1212 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
32 |
Correct |
6 ms |
1272 KB |
Output is correct |
33 |
Correct |
6 ms |
1364 KB |
Output is correct |
34 |
Correct |
6 ms |
1368 KB |
Output is correct |
35 |
Correct |
37 ms |
5440 KB |
Output is correct |
36 |
Correct |
14 ms |
1488 KB |
Output is correct |
37 |
Correct |
90 ms |
9548 KB |
Output is correct |
38 |
Correct |
70 ms |
8524 KB |
Output is correct |
39 |
Correct |
72 ms |
8940 KB |
Output is correct |
40 |
Correct |
73 ms |
8656 KB |
Output is correct |
41 |
Correct |
70 ms |
8388 KB |
Output is correct |
42 |
Correct |
17 ms |
2024 KB |
Output is correct |
43 |
Correct |
10 ms |
2000 KB |
Output is correct |
44 |
Correct |
396 ms |
82668 KB |
Output is correct |
45 |
Correct |
13 ms |
2656 KB |
Output is correct |
46 |
Correct |
13 ms |
2652 KB |
Output is correct |
47 |
Execution timed out |
1090 ms |
21088 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |