#include <bits/stdc++.h>
using namespace std;
int dist[10500005], a[30005][175];
vector<int> adj[10500005];
deque<int> deq;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, s, t, sz;
cin >> n >> m;
sz=n;
for (int i=0; i<m; i++)
{
int b, p;
cin >> b >> p;
if (i==0)
s=b;
else if (i==1)
t=b;
if (p*p<=m)
{
if (!a[b][p])
{
a[b%p][p]=sz;
sz++;
adj[a[b%p][p]].push_back(b%p*2);
for (int j=b%p+p; j<n; j+=p)
{
a[j][p]=sz;
sz++;
adj[a[j][p]].push_back(j*2);
adj[a[j][p]].push_back(a[j-p][p]*2+1);
adj[a[j-p][p]].push_back(a[j][p]*2+1);
}
}
adj[b].push_back(a[b][p]*2);
}
else
{
int prev=b;
for (int j=b-p; j>=0; j-=p)
{
adj[prev].push_back(sz*2+1);
adj[sz].push_back(j*2);
prev=sz;
sz++;
}
prev=b;
for (int j=b+p; j<n; j+=p)
{
adj[prev].push_back(sz*2+1);
adj[sz].push_back(j*2);
prev=sz;
sz++;
}
}
}
for (int i=0; i<sz; i++)
dist[i]=1e9;
dist[s]=0;
deq.push_back(s);
while (!deq.empty())
{
int u=deq.front();
deq.pop_front();
for (int i=0; i<adj[u].size(); i++)
{
int v=adj[u][i]>>1, w=adj[u][i]&1;
if (dist[v]==1e9)
{
dist[v]=dist[u]+w;
if (w)
deq.push_back(v);
else
deq.push_front(v);
}
}
}
if (dist[t]==1e9)
cout << -1;
else
cout << dist[t];
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
skyscraper.cpp:81:12: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | if (dist[t]==1e9)
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
246792 KB |
Output is correct |
2 |
Correct |
124 ms |
246852 KB |
Output is correct |
3 |
Correct |
110 ms |
246932 KB |
Output is correct |
4 |
Correct |
111 ms |
246852 KB |
Output is correct |
5 |
Correct |
114 ms |
246820 KB |
Output is correct |
6 |
Correct |
126 ms |
246920 KB |
Output is correct |
7 |
Correct |
109 ms |
246852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
246856 KB |
Output is correct |
2 |
Correct |
111 ms |
246876 KB |
Output is correct |
3 |
Correct |
107 ms |
246868 KB |
Output is correct |
4 |
Correct |
110 ms |
246848 KB |
Output is correct |
5 |
Correct |
111 ms |
246820 KB |
Output is correct |
6 |
Correct |
114 ms |
246804 KB |
Output is correct |
7 |
Correct |
110 ms |
246848 KB |
Output is correct |
8 |
Correct |
110 ms |
246932 KB |
Output is correct |
9 |
Correct |
112 ms |
246908 KB |
Output is correct |
10 |
Correct |
112 ms |
246948 KB |
Output is correct |
11 |
Correct |
109 ms |
247088 KB |
Output is correct |
12 |
Correct |
108 ms |
246980 KB |
Output is correct |
13 |
Correct |
112 ms |
246980 KB |
Output is correct |
14 |
Correct |
128 ms |
247072 KB |
Output is correct |
15 |
Correct |
111 ms |
247108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
246796 KB |
Output is correct |
2 |
Correct |
109 ms |
246860 KB |
Output is correct |
3 |
Correct |
113 ms |
246796 KB |
Output is correct |
4 |
Correct |
110 ms |
246768 KB |
Output is correct |
5 |
Correct |
111 ms |
246764 KB |
Output is correct |
6 |
Correct |
140 ms |
246812 KB |
Output is correct |
7 |
Correct |
113 ms |
246772 KB |
Output is correct |
8 |
Correct |
110 ms |
246904 KB |
Output is correct |
9 |
Correct |
110 ms |
246928 KB |
Output is correct |
10 |
Correct |
114 ms |
246900 KB |
Output is correct |
11 |
Correct |
111 ms |
247036 KB |
Output is correct |
12 |
Correct |
118 ms |
246980 KB |
Output is correct |
13 |
Correct |
110 ms |
246916 KB |
Output is correct |
14 |
Correct |
111 ms |
247104 KB |
Output is correct |
15 |
Correct |
112 ms |
247080 KB |
Output is correct |
16 |
Correct |
112 ms |
247036 KB |
Output is correct |
17 |
Correct |
121 ms |
247840 KB |
Output is correct |
18 |
Correct |
111 ms |
248208 KB |
Output is correct |
19 |
Correct |
111 ms |
248324 KB |
Output is correct |
20 |
Correct |
110 ms |
248304 KB |
Output is correct |
21 |
Correct |
115 ms |
247168 KB |
Output is correct |
22 |
Correct |
118 ms |
248196 KB |
Output is correct |
23 |
Correct |
118 ms |
248160 KB |
Output is correct |
24 |
Correct |
113 ms |
248580 KB |
Output is correct |
25 |
Correct |
113 ms |
248456 KB |
Output is correct |
26 |
Correct |
119 ms |
248592 KB |
Output is correct |
27 |
Correct |
113 ms |
248388 KB |
Output is correct |
28 |
Correct |
114 ms |
249004 KB |
Output is correct |
29 |
Correct |
119 ms |
250328 KB |
Output is correct |
30 |
Correct |
112 ms |
248772 KB |
Output is correct |
31 |
Correct |
117 ms |
249480 KB |
Output is correct |
32 |
Correct |
117 ms |
249076 KB |
Output is correct |
33 |
Correct |
126 ms |
252356 KB |
Output is correct |
34 |
Correct |
127 ms |
252444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
246792 KB |
Output is correct |
2 |
Correct |
128 ms |
246824 KB |
Output is correct |
3 |
Correct |
117 ms |
246796 KB |
Output is correct |
4 |
Correct |
110 ms |
246840 KB |
Output is correct |
5 |
Correct |
110 ms |
246848 KB |
Output is correct |
6 |
Correct |
111 ms |
246824 KB |
Output is correct |
7 |
Correct |
133 ms |
246856 KB |
Output is correct |
8 |
Correct |
119 ms |
246912 KB |
Output is correct |
9 |
Correct |
113 ms |
246876 KB |
Output is correct |
10 |
Correct |
109 ms |
247000 KB |
Output is correct |
11 |
Correct |
117 ms |
247088 KB |
Output is correct |
12 |
Correct |
109 ms |
246916 KB |
Output is correct |
13 |
Correct |
110 ms |
246960 KB |
Output is correct |
14 |
Correct |
113 ms |
247064 KB |
Output is correct |
15 |
Correct |
119 ms |
247152 KB |
Output is correct |
16 |
Correct |
113 ms |
247148 KB |
Output is correct |
17 |
Correct |
110 ms |
247776 KB |
Output is correct |
18 |
Correct |
110 ms |
248204 KB |
Output is correct |
19 |
Correct |
112 ms |
248416 KB |
Output is correct |
20 |
Correct |
110 ms |
248356 KB |
Output is correct |
21 |
Correct |
111 ms |
247120 KB |
Output is correct |
22 |
Correct |
111 ms |
248196 KB |
Output is correct |
23 |
Correct |
113 ms |
248104 KB |
Output is correct |
24 |
Correct |
113 ms |
248588 KB |
Output is correct |
25 |
Correct |
111 ms |
248388 KB |
Output is correct |
26 |
Correct |
115 ms |
248656 KB |
Output is correct |
27 |
Correct |
113 ms |
248436 KB |
Output is correct |
28 |
Correct |
112 ms |
249028 KB |
Output is correct |
29 |
Correct |
120 ms |
250348 KB |
Output is correct |
30 |
Correct |
116 ms |
248808 KB |
Output is correct |
31 |
Correct |
118 ms |
249376 KB |
Output is correct |
32 |
Correct |
112 ms |
249040 KB |
Output is correct |
33 |
Correct |
126 ms |
252380 KB |
Output is correct |
34 |
Correct |
141 ms |
252472 KB |
Output is correct |
35 |
Correct |
127 ms |
250844 KB |
Output is correct |
36 |
Correct |
112 ms |
248132 KB |
Output is correct |
37 |
Correct |
140 ms |
254056 KB |
Output is correct |
38 |
Correct |
149 ms |
253252 KB |
Output is correct |
39 |
Correct |
147 ms |
253496 KB |
Output is correct |
40 |
Correct |
134 ms |
253252 KB |
Output is correct |
41 |
Correct |
139 ms |
253268 KB |
Output is correct |
42 |
Correct |
113 ms |
248748 KB |
Output is correct |
43 |
Correct |
113 ms |
248704 KB |
Output is correct |
44 |
Correct |
114 ms |
248560 KB |
Output is correct |
45 |
Runtime error |
139 ms |
262148 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
246848 KB |
Output is correct |
2 |
Correct |
110 ms |
246876 KB |
Output is correct |
3 |
Correct |
110 ms |
246880 KB |
Output is correct |
4 |
Correct |
110 ms |
246852 KB |
Output is correct |
5 |
Correct |
117 ms |
246768 KB |
Output is correct |
6 |
Correct |
111 ms |
246812 KB |
Output is correct |
7 |
Correct |
111 ms |
246852 KB |
Output is correct |
8 |
Correct |
113 ms |
246824 KB |
Output is correct |
9 |
Correct |
112 ms |
246928 KB |
Output is correct |
10 |
Correct |
110 ms |
246996 KB |
Output is correct |
11 |
Correct |
112 ms |
246992 KB |
Output is correct |
12 |
Correct |
114 ms |
246928 KB |
Output is correct |
13 |
Correct |
110 ms |
246972 KB |
Output is correct |
14 |
Correct |
111 ms |
247088 KB |
Output is correct |
15 |
Correct |
111 ms |
247128 KB |
Output is correct |
16 |
Correct |
111 ms |
247108 KB |
Output is correct |
17 |
Correct |
119 ms |
247912 KB |
Output is correct |
18 |
Correct |
116 ms |
248272 KB |
Output is correct |
19 |
Correct |
109 ms |
248388 KB |
Output is correct |
20 |
Correct |
114 ms |
248328 KB |
Output is correct |
21 |
Correct |
107 ms |
247084 KB |
Output is correct |
22 |
Correct |
113 ms |
248288 KB |
Output is correct |
23 |
Correct |
112 ms |
248192 KB |
Output is correct |
24 |
Correct |
117 ms |
248500 KB |
Output is correct |
25 |
Correct |
112 ms |
248388 KB |
Output is correct |
26 |
Correct |
117 ms |
248592 KB |
Output is correct |
27 |
Correct |
111 ms |
248496 KB |
Output is correct |
28 |
Correct |
119 ms |
248940 KB |
Output is correct |
29 |
Correct |
124 ms |
250396 KB |
Output is correct |
30 |
Correct |
120 ms |
248744 KB |
Output is correct |
31 |
Correct |
117 ms |
249332 KB |
Output is correct |
32 |
Correct |
113 ms |
248964 KB |
Output is correct |
33 |
Correct |
126 ms |
252424 KB |
Output is correct |
34 |
Correct |
128 ms |
252352 KB |
Output is correct |
35 |
Correct |
125 ms |
250820 KB |
Output is correct |
36 |
Correct |
116 ms |
248256 KB |
Output is correct |
37 |
Correct |
141 ms |
253988 KB |
Output is correct |
38 |
Correct |
138 ms |
253204 KB |
Output is correct |
39 |
Correct |
136 ms |
253252 KB |
Output is correct |
40 |
Correct |
146 ms |
253320 KB |
Output is correct |
41 |
Correct |
137 ms |
253220 KB |
Output is correct |
42 |
Correct |
150 ms |
248788 KB |
Output is correct |
43 |
Correct |
124 ms |
248676 KB |
Output is correct |
44 |
Correct |
114 ms |
248584 KB |
Output is correct |
45 |
Runtime error |
141 ms |
262148 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |