#include <iostream>
#include <set>
#include <vector>
#include <set>
using namespace std;
/*
If P[i] < √N, there are at most √N distinct values of i.
If P[i] > √N, i has edges to at most √N positions.
*/
vector<int> mindist(30000, 1e9);
struct distcomp
{
int x;
};
bool operator < (distcomp A, distcomp B)
{
if(mindist[A.x] == mindist[B.x]) return A.x < B.x;
return mindist[A.x] < mindist[B.x];
}
int main()
{
int N, M;
cin >> N >> M;
int pos0, pos1;
set<int> doges[N];
vector<int> edge[N];
vector<int> dist[N];
int B, P;
for(int i = 0; i < M; i++)
{
cin >> B >> P;
if(i == 0) pos0 = B;
if(i == 1) pos1 = B;
doges[B].insert(P);
}
for(int pos = 0; pos < N; pos++)
{
for(int jump: doges[pos])
{
for(int x = pos + jump; x < N; x += jump)
{
edge[pos].push_back(x);
dist[pos].push_back((x - pos)/jump);
if(doges[x].find(jump) != doges[x].end()) break;
}
for(int x = pos - jump; x >= 0; x -= jump)
{
edge[pos].push_back(x);
dist[pos].push_back((pos - x)/jump);
if(doges[x].find(jump) != doges[x].end()) break;
}
}
}
set<distcomp> S;
mindist[pos0] = 0;
for(int i = 0; i < N; i++) S.insert(distcomp{i});
int u, v, w;
while(!S.empty())
{
u = S.begin()->x;
S.erase(S.begin());
for(int i = 0; i < edge[u].size(); i++)
{
v = edge[u][i];
w = dist[u][i];
if(mindist[v] > mindist[u] + w)
{
S.erase(distcomp{v});
mindist[v] = mindist[u] + w;
S.insert(distcomp{v});
}
}
}
// for(int i = 0; i < N; i++)
// {
// cout << i << ' ' << mindist[i] << ": ";
// for(int j = 0; j < edge[i].size(); j++) cout << edge[i][j] << ' ' << dist[i][j] << " | ";
// cout << '\n';
// }
//
//
// cout << pos1 << ' ' << mindist[pos1] << '\n';
if(mindist[pos1] == 1e9) cout << "-1\n";
else cout << mindist[pos1] << '\n';
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:74:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 0; i < edge[u].size(); i++)
| ~~^~~~~~~~~~~~~~~~
skyscraper.cpp:65:17: warning: 'pos0' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | mindist[pos0] = 0;
| ^
skyscraper.cpp:98:20: warning: 'pos1' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | if(mindist[pos1] == 1e9) cout << "-1\n";
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
3 ms |
748 KB |
Output is correct |
18 |
Correct |
2 ms |
748 KB |
Output is correct |
19 |
Correct |
2 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
3 ms |
748 KB |
Output is correct |
24 |
Correct |
4 ms |
1004 KB |
Output is correct |
25 |
Correct |
4 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
748 KB |
Output is correct |
28 |
Correct |
5 ms |
1004 KB |
Output is correct |
29 |
Correct |
8 ms |
1388 KB |
Output is correct |
30 |
Correct |
3 ms |
1004 KB |
Output is correct |
31 |
Correct |
4 ms |
1132 KB |
Output is correct |
32 |
Correct |
3 ms |
1004 KB |
Output is correct |
33 |
Correct |
10 ms |
2028 KB |
Output is correct |
34 |
Correct |
10 ms |
2028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
3 ms |
748 KB |
Output is correct |
18 |
Correct |
2 ms |
748 KB |
Output is correct |
19 |
Correct |
2 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
3 ms |
748 KB |
Output is correct |
24 |
Correct |
5 ms |
1004 KB |
Output is correct |
25 |
Correct |
4 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
748 KB |
Output is correct |
28 |
Correct |
4 ms |
1024 KB |
Output is correct |
29 |
Correct |
7 ms |
1388 KB |
Output is correct |
30 |
Correct |
4 ms |
1004 KB |
Output is correct |
31 |
Correct |
4 ms |
1132 KB |
Output is correct |
32 |
Correct |
3 ms |
1004 KB |
Output is correct |
33 |
Correct |
10 ms |
2028 KB |
Output is correct |
34 |
Correct |
10 ms |
2028 KB |
Output is correct |
35 |
Correct |
28 ms |
2924 KB |
Output is correct |
36 |
Correct |
6 ms |
1004 KB |
Output is correct |
37 |
Correct |
28 ms |
3564 KB |
Output is correct |
38 |
Correct |
37 ms |
3948 KB |
Output is correct |
39 |
Correct |
36 ms |
3820 KB |
Output is correct |
40 |
Correct |
38 ms |
3820 KB |
Output is correct |
41 |
Correct |
37 ms |
3820 KB |
Output is correct |
42 |
Correct |
15 ms |
876 KB |
Output is correct |
43 |
Correct |
15 ms |
748 KB |
Output is correct |
44 |
Correct |
15 ms |
876 KB |
Output is correct |
45 |
Correct |
38 ms |
6764 KB |
Output is correct |
46 |
Correct |
38 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
3 ms |
748 KB |
Output is correct |
18 |
Correct |
2 ms |
748 KB |
Output is correct |
19 |
Correct |
2 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
876 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
3 ms |
748 KB |
Output is correct |
24 |
Correct |
4 ms |
1004 KB |
Output is correct |
25 |
Correct |
4 ms |
876 KB |
Output is correct |
26 |
Correct |
3 ms |
876 KB |
Output is correct |
27 |
Correct |
3 ms |
748 KB |
Output is correct |
28 |
Correct |
3 ms |
1004 KB |
Output is correct |
29 |
Correct |
6 ms |
1388 KB |
Output is correct |
30 |
Correct |
3 ms |
1004 KB |
Output is correct |
31 |
Correct |
4 ms |
1132 KB |
Output is correct |
32 |
Correct |
3 ms |
1004 KB |
Output is correct |
33 |
Correct |
10 ms |
2028 KB |
Output is correct |
34 |
Correct |
10 ms |
2028 KB |
Output is correct |
35 |
Correct |
29 ms |
2828 KB |
Output is correct |
36 |
Correct |
7 ms |
1004 KB |
Output is correct |
37 |
Correct |
35 ms |
3564 KB |
Output is correct |
38 |
Correct |
38 ms |
3820 KB |
Output is correct |
39 |
Correct |
36 ms |
3820 KB |
Output is correct |
40 |
Correct |
36 ms |
3820 KB |
Output is correct |
41 |
Correct |
36 ms |
3820 KB |
Output is correct |
42 |
Correct |
19 ms |
876 KB |
Output is correct |
43 |
Correct |
19 ms |
748 KB |
Output is correct |
44 |
Correct |
18 ms |
876 KB |
Output is correct |
45 |
Correct |
38 ms |
6764 KB |
Output is correct |
46 |
Correct |
41 ms |
6764 KB |
Output is correct |
47 |
Correct |
79 ms |
12140 KB |
Output is correct |
48 |
Correct |
37 ms |
7680 KB |
Output is correct |
49 |
Correct |
34 ms |
6764 KB |
Output is correct |
50 |
Correct |
27 ms |
6764 KB |
Output is correct |
51 |
Correct |
96 ms |
9856 KB |
Output is correct |
52 |
Correct |
100 ms |
10220 KB |
Output is correct |
53 |
Correct |
49 ms |
7020 KB |
Output is correct |
54 |
Correct |
21 ms |
4844 KB |
Output is correct |
55 |
Correct |
17 ms |
5356 KB |
Output is correct |
56 |
Correct |
38 ms |
7916 KB |
Output is correct |
57 |
Correct |
78 ms |
13036 KB |
Output is correct |
58 |
Correct |
30 ms |
5480 KB |
Output is correct |
59 |
Correct |
35 ms |
5740 KB |
Output is correct |
60 |
Correct |
38 ms |
6636 KB |
Output is correct |
61 |
Correct |
36 ms |
5868 KB |
Output is correct |
62 |
Correct |
57 ms |
11500 KB |
Output is correct |
63 |
Correct |
142 ms |
27416 KB |
Output is correct |
64 |
Correct |
150 ms |
30828 KB |
Output is correct |
65 |
Correct |
195 ms |
45464 KB |
Output is correct |
66 |
Correct |
336 ms |
64560 KB |
Output is correct |
67 |
Correct |
342 ms |
64560 KB |
Output is correct |