/*
Editor: Abdelrahman Hossam
Nickname: Blobo2_Blobo2
IOI next year isA :)
*/
/*#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")*/
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define endl "\n"
#define all(v) v.begin(),v.end()
#define gen(arr,n,nxt) generate(arr,arr+n,nxt)
#define Blobo2_el_7akim_elly_3ayz_yro7_IOI_w_3ayz_yakol_jilaty ios_base::sync_with_stdio(false);cin.tie(0);
const int mo=1e9+7,INF=1e18;
int nxt(){int x;cin>>x;return x;}
signed main(){
Blobo2_el_7akim_elly_3ayz_yro7_IOI_w_3ayz_yakol_jilaty
int n=nxt(),m=nxt();
vector<pair<int,int> >v;
map<pair<int,int>,int>mp;
for(int i=0;i<m;i++){
int b=nxt(),p=nxt();
v.push_back({b,p});
}
int from = v[0].first,to = v[1].first;
vector<pair<int,int> >v2;
sort(all(v));
for(int i=0;i<m;i++){
if(v2.size()&&v2.back()==v[i])continue;
v2.push_back(v[i]);
}
v=v2;
vector<pair<int,int> >adj[n+1];
for(int i=0;i<v.size();i++){
for(int j=v[i].first+v[i].second;j<n;j+=v[i].second){
adj[v[i].first].push_back({j,(j-v[i].first)/v[i].second});
if(mp[{j,v[i].second}])break;
}
}
for(int i=0;i<v.size();i++){
for(int j=v[i].first-v[i].second;j>=0;j-=v[i].second){
adj[v[i].first].push_back({j,(v[i].first-j)/v[i].second});
if(mp[{j,v[i].second}])break;
}
}
int dis[n],vis[n];
for(int i=0;i<n;i++)
dis[i]=INF,vis[i]=0;
dis[from]=0;
priority_queue<pair<int,int> >q;
q.push({0,from});
while(!q.empty()) {
int a=q.top().second;q.pop();
if(vis[a])continue;
vis[a]=1;
for(auto u:adj[a]){
int b=u.first,w=u.second;
if(dis[a]+w<dis[b]){
dis[b]=dis[a]+w;
q.push({-dis[b],b});
}
}
}
if(dis[to]==INF)cout<<-1<<endl;
else cout<<dis[to]<<endl;
return 0;
}
Compilation message
skyscraper.cpp:19:24: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
19 | const int mo=1e9+7,INF=1e18;
| ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
skyscraper.cpp:46:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
6 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
716 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
260 ms |
32624 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
660 KB |
Output is correct |
23 |
Correct |
3 ms |
716 KB |
Output is correct |
24 |
Correct |
5 ms |
1228 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
4 ms |
972 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
7 ms |
1868 KB |
Output is correct |
29 |
Correct |
23 ms |
4844 KB |
Output is correct |
30 |
Correct |
6 ms |
1740 KB |
Output is correct |
31 |
Correct |
12 ms |
2764 KB |
Output is correct |
32 |
Correct |
8 ms |
2148 KB |
Output is correct |
33 |
Correct |
62 ms |
8996 KB |
Output is correct |
34 |
Correct |
58 ms |
9016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
7 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
716 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
257 ms |
32648 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
3 ms |
716 KB |
Output is correct |
24 |
Correct |
5 ms |
1228 KB |
Output is correct |
25 |
Correct |
4 ms |
844 KB |
Output is correct |
26 |
Correct |
3 ms |
972 KB |
Output is correct |
27 |
Correct |
3 ms |
844 KB |
Output is correct |
28 |
Correct |
7 ms |
1864 KB |
Output is correct |
29 |
Correct |
25 ms |
4904 KB |
Output is correct |
30 |
Correct |
6 ms |
1740 KB |
Output is correct |
31 |
Correct |
12 ms |
2872 KB |
Output is correct |
32 |
Correct |
8 ms |
2124 KB |
Output is correct |
33 |
Correct |
53 ms |
9072 KB |
Output is correct |
34 |
Correct |
55 ms |
9056 KB |
Output is correct |
35 |
Correct |
52 ms |
7028 KB |
Output is correct |
36 |
Correct |
7 ms |
1356 KB |
Output is correct |
37 |
Correct |
132 ms |
13356 KB |
Output is correct |
38 |
Correct |
97 ms |
11588 KB |
Output is correct |
39 |
Correct |
132 ms |
11764 KB |
Output is correct |
40 |
Correct |
101 ms |
11588 KB |
Output is correct |
41 |
Correct |
130 ms |
11492 KB |
Output is correct |
42 |
Correct |
9 ms |
1232 KB |
Output is correct |
43 |
Correct |
10 ms |
1232 KB |
Output is correct |
44 |
Correct |
263 ms |
33012 KB |
Output is correct |
45 |
Correct |
333 ms |
33656 KB |
Output is correct |
46 |
Correct |
318 ms |
33664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
6 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
716 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
273 ms |
32596 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
3 ms |
716 KB |
Output is correct |
24 |
Correct |
5 ms |
1228 KB |
Output is correct |
25 |
Correct |
3 ms |
836 KB |
Output is correct |
26 |
Correct |
3 ms |
972 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
8 ms |
1868 KB |
Output is correct |
29 |
Correct |
27 ms |
4904 KB |
Output is correct |
30 |
Correct |
6 ms |
1740 KB |
Output is correct |
31 |
Correct |
12 ms |
2816 KB |
Output is correct |
32 |
Correct |
8 ms |
2124 KB |
Output is correct |
33 |
Correct |
59 ms |
9224 KB |
Output is correct |
34 |
Correct |
55 ms |
9020 KB |
Output is correct |
35 |
Correct |
51 ms |
7040 KB |
Output is correct |
36 |
Correct |
7 ms |
1356 KB |
Output is correct |
37 |
Correct |
129 ms |
13420 KB |
Output is correct |
38 |
Correct |
92 ms |
11464 KB |
Output is correct |
39 |
Correct |
98 ms |
11688 KB |
Output is correct |
40 |
Correct |
121 ms |
11684 KB |
Output is correct |
41 |
Correct |
98 ms |
11480 KB |
Output is correct |
42 |
Correct |
9 ms |
1232 KB |
Output is correct |
43 |
Correct |
8 ms |
1232 KB |
Output is correct |
44 |
Correct |
270 ms |
33212 KB |
Output is correct |
45 |
Correct |
441 ms |
33596 KB |
Output is correct |
46 |
Correct |
418 ms |
33768 KB |
Output is correct |
47 |
Correct |
735 ms |
57064 KB |
Output is correct |
48 |
Correct |
123 ms |
17284 KB |
Output is correct |
49 |
Correct |
63 ms |
10812 KB |
Output is correct |
50 |
Correct |
66 ms |
11072 KB |
Output is correct |
51 |
Correct |
196 ms |
24780 KB |
Output is correct |
52 |
Correct |
238 ms |
26600 KB |
Output is correct |
53 |
Correct |
34 ms |
7216 KB |
Output is correct |
54 |
Correct |
9 ms |
3532 KB |
Output is correct |
55 |
Correct |
17 ms |
5588 KB |
Output is correct |
56 |
Execution timed out |
1082 ms |
79516 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |