#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(B == 0) pos0 = i;
if(B == 1) pos1 = i;
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});
}
}
}
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:88:20: warning: 'pos1' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | if(mindist[pos1] == 1e9) cout << "-1\n";
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Runtime error |
1 ms |
748 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |