#include <bits/stdc++.h>
#define N 30005
using namespace std;
vector<int> v[N];
int group[N];
int groupx[N],groupy[N];
bool vis[N];
vector<int> mp[N];
vector<pair<int,int>> extend;
vector<pair<int,int>> extend2;
vector<int> nxt;
int n;
void add(int a,int b){
if(mp[a].empty()){
mp[a].resize(n / groupx[a] + 1);
}
if(mp[a][b])return;
mp[a][b] = 1;
if(b*groupx[a] + groupy[a] < n && !vis[b*groupx[a] + groupy[a]]){
vis[b*groupx[a] + groupy[a]] = 1;
nxt.push_back(b*groupx[a] + groupy[a]);
}
if(b+1 < mp[a].size() && !mp[a][b+1])
extend2.push_back({a,b+1});
if(b-1 >= 0 && !mp[a][b-1])
extend2.push_back({a,b-1});
}
int x[N],y[N];
void solve(){
int m;
cin >> n >> m;
int st = -1;
int nd = -1;
vector<pair<pair<int,int>,int>> compress;
for(int i=1;i<=m;i++){
int x[i],y[i];
cin >> x[i] >> y[i];
if(st == -1)st = x[i];
else if(nd == -1)nd = x[i];
v[x[i]].push_back(i);
compress.push_back({{y[i],x[i]%y[i]},i});
}
sort(compress.begin(),compress.end());
for(int i=0,cnt=0;i<compress.size();i++){
if(i && compress[i].first != compress[i-1].first)
cnt++;
group[compress[i].second] = cnt;
groupx[cnt] = compress[i].first.first;
groupy[cnt] = compress[i].first.second;
}
vector<int> nws;
vis[st] = 1;
nws.push_back(st);
int cnt = 0;
while(nws.size() || extend.size()){
nxt.clear();
extend2.clear();
for(auto u:nws){
if(u == nd){
cout << cnt << endl;
return;
}
for(auto c:v[u]){
add(group[c],u / groupx[group[c]]);
}
}
for(auto u:extend2)
extend.push_back(u);
extend2.clear();
for(auto u:extend){
add(u.first,u.second);
}
for(auto u:extend2)
extend.push_back(u);
swap(nws,nxt);
cnt++;
}
cout << -1 << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
skyscraper.cpp: In function 'void add(int, int)':
skyscraper.cpp:23:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if(b+1 < mp[a].size() && !mp[a][b+1])
| ~~~~^~~~~~~~~~~~~~
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:44:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0,cnt=0;i<compress.size();i++){
| ~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1684 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1744 KB |
Output is correct |
2 |
Correct |
2 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1744 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1736 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
2 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1752 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1736 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1736 KB |
Output is correct |
5 |
Correct |
2 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1652 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Execution timed out |
1090 ms |
1748 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
2 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1744 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
4 ms |
2028 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Execution timed out |
1088 ms |
1744 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
2 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1736 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
1880 KB |
Output is correct |
16 |
Execution timed out |
1092 ms |
1748 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |