#include <bits/stdc++.h>
using namespace std;
vector<pair<int, bool> > adj[10500005];
int dist[10500005], a[30005][175];
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, 0});
for (int j=b%p+p; j<n; j+=p)
{
a[j][p]=sz;
sz++;
adj[a[j][p]].push_back({j, 0});
adj[a[j][p]].push_back({a[j-p][p], 1});
adj[a[j-p][p]].push_back({a[j][p], 1});
}
}
adj[b].push_back({a[b][p], 0});
}
else
{
int prev=b;
for (int j=b-p; j>=0; j-=p)
{
adj[prev].push_back({sz, 1});
adj[sz].push_back({j, 0});
prev=sz;
sz++;
}
prev=b;
for (int j=b+p; j<n; j+=p)
{
adj[prev].push_back({sz, 1});
adj[sz].push_back({j, 0});
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].first;
bool w=adj[u][i].second;
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<std::pair<int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
skyscraper.cpp:82:12: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | if (dist[t]==1e9)
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
246888 KB |
Output is correct |
2 |
Correct |
111 ms |
246808 KB |
Output is correct |
3 |
Correct |
111 ms |
246884 KB |
Output is correct |
4 |
Correct |
121 ms |
246764 KB |
Output is correct |
5 |
Correct |
110 ms |
246848 KB |
Output is correct |
6 |
Correct |
110 ms |
246868 KB |
Output is correct |
7 |
Correct |
110 ms |
246852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
246864 KB |
Output is correct |
2 |
Correct |
113 ms |
246832 KB |
Output is correct |
3 |
Correct |
111 ms |
246852 KB |
Output is correct |
4 |
Correct |
114 ms |
246812 KB |
Output is correct |
5 |
Correct |
111 ms |
246856 KB |
Output is correct |
6 |
Correct |
117 ms |
246904 KB |
Output is correct |
7 |
Correct |
124 ms |
246772 KB |
Output is correct |
8 |
Correct |
111 ms |
246820 KB |
Output is correct |
9 |
Correct |
111 ms |
246852 KB |
Output is correct |
10 |
Correct |
112 ms |
246980 KB |
Output is correct |
11 |
Correct |
113 ms |
247072 KB |
Output is correct |
12 |
Correct |
111 ms |
246980 KB |
Output is correct |
13 |
Correct |
113 ms |
246980 KB |
Output is correct |
14 |
Correct |
113 ms |
247200 KB |
Output is correct |
15 |
Correct |
114 ms |
247180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
246844 KB |
Output is correct |
2 |
Correct |
112 ms |
246796 KB |
Output is correct |
3 |
Correct |
112 ms |
246852 KB |
Output is correct |
4 |
Correct |
111 ms |
246808 KB |
Output is correct |
5 |
Correct |
115 ms |
246804 KB |
Output is correct |
6 |
Correct |
113 ms |
246852 KB |
Output is correct |
7 |
Correct |
112 ms |
246868 KB |
Output is correct |
8 |
Correct |
113 ms |
246820 KB |
Output is correct |
9 |
Correct |
112 ms |
246884 KB |
Output is correct |
10 |
Correct |
112 ms |
246968 KB |
Output is correct |
11 |
Correct |
112 ms |
247116 KB |
Output is correct |
12 |
Correct |
119 ms |
246916 KB |
Output is correct |
13 |
Correct |
111 ms |
246944 KB |
Output is correct |
14 |
Correct |
113 ms |
247108 KB |
Output is correct |
15 |
Correct |
112 ms |
247092 KB |
Output is correct |
16 |
Correct |
111 ms |
247116 KB |
Output is correct |
17 |
Correct |
113 ms |
247948 KB |
Output is correct |
18 |
Correct |
112 ms |
248360 KB |
Output is correct |
19 |
Correct |
120 ms |
248496 KB |
Output is correct |
20 |
Correct |
111 ms |
248336 KB |
Output is correct |
21 |
Correct |
111 ms |
247236 KB |
Output is correct |
22 |
Correct |
112 ms |
248316 KB |
Output is correct |
23 |
Correct |
118 ms |
248260 KB |
Output is correct |
24 |
Correct |
112 ms |
248680 KB |
Output is correct |
25 |
Correct |
113 ms |
248472 KB |
Output is correct |
26 |
Correct |
113 ms |
248624 KB |
Output is correct |
27 |
Correct |
111 ms |
248516 KB |
Output is correct |
28 |
Correct |
113 ms |
249148 KB |
Output is correct |
29 |
Correct |
118 ms |
250892 KB |
Output is correct |
30 |
Correct |
112 ms |
248900 KB |
Output is correct |
31 |
Correct |
117 ms |
249428 KB |
Output is correct |
32 |
Correct |
114 ms |
249028 KB |
Output is correct |
33 |
Correct |
134 ms |
253764 KB |
Output is correct |
34 |
Correct |
141 ms |
253636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
246852 KB |
Output is correct |
2 |
Correct |
111 ms |
246852 KB |
Output is correct |
3 |
Correct |
114 ms |
246820 KB |
Output is correct |
4 |
Correct |
111 ms |
246856 KB |
Output is correct |
5 |
Correct |
111 ms |
246776 KB |
Output is correct |
6 |
Correct |
110 ms |
246852 KB |
Output is correct |
7 |
Correct |
125 ms |
246828 KB |
Output is correct |
8 |
Correct |
118 ms |
246956 KB |
Output is correct |
9 |
Correct |
110 ms |
246836 KB |
Output is correct |
10 |
Correct |
112 ms |
247012 KB |
Output is correct |
11 |
Correct |
113 ms |
247144 KB |
Output is correct |
12 |
Correct |
111 ms |
247008 KB |
Output is correct |
13 |
Correct |
111 ms |
246984 KB |
Output is correct |
14 |
Correct |
111 ms |
247108 KB |
Output is correct |
15 |
Correct |
114 ms |
247152 KB |
Output is correct |
16 |
Correct |
113 ms |
247108 KB |
Output is correct |
17 |
Correct |
113 ms |
247916 KB |
Output is correct |
18 |
Correct |
113 ms |
248296 KB |
Output is correct |
19 |
Correct |
112 ms |
248460 KB |
Output is correct |
20 |
Correct |
113 ms |
248388 KB |
Output is correct |
21 |
Correct |
112 ms |
247120 KB |
Output is correct |
22 |
Correct |
113 ms |
248284 KB |
Output is correct |
23 |
Correct |
112 ms |
248256 KB |
Output is correct |
24 |
Correct |
115 ms |
248652 KB |
Output is correct |
25 |
Correct |
128 ms |
248516 KB |
Output is correct |
26 |
Correct |
117 ms |
248716 KB |
Output is correct |
27 |
Correct |
116 ms |
248584 KB |
Output is correct |
28 |
Correct |
114 ms |
249156 KB |
Output is correct |
29 |
Correct |
123 ms |
250864 KB |
Output is correct |
30 |
Correct |
114 ms |
248832 KB |
Output is correct |
31 |
Correct |
114 ms |
249456 KB |
Output is correct |
32 |
Correct |
114 ms |
249048 KB |
Output is correct |
33 |
Correct |
129 ms |
253708 KB |
Output is correct |
34 |
Correct |
133 ms |
253748 KB |
Output is correct |
35 |
Correct |
129 ms |
251860 KB |
Output is correct |
36 |
Correct |
114 ms |
248304 KB |
Output is correct |
37 |
Correct |
144 ms |
255772 KB |
Output is correct |
38 |
Correct |
140 ms |
254896 KB |
Output is correct |
39 |
Correct |
140 ms |
254908 KB |
Output is correct |
40 |
Correct |
142 ms |
254996 KB |
Output is correct |
41 |
Correct |
139 ms |
254876 KB |
Output is correct |
42 |
Correct |
116 ms |
249132 KB |
Output is correct |
43 |
Correct |
116 ms |
249100 KB |
Output is correct |
44 |
Correct |
116 ms |
248840 KB |
Output is correct |
45 |
Runtime error |
132 ms |
262148 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
246856 KB |
Output is correct |
2 |
Correct |
112 ms |
246872 KB |
Output is correct |
3 |
Correct |
119 ms |
246852 KB |
Output is correct |
4 |
Correct |
110 ms |
246916 KB |
Output is correct |
5 |
Correct |
111 ms |
246800 KB |
Output is correct |
6 |
Correct |
113 ms |
246836 KB |
Output is correct |
7 |
Correct |
111 ms |
246852 KB |
Output is correct |
8 |
Correct |
111 ms |
246852 KB |
Output is correct |
9 |
Correct |
111 ms |
246816 KB |
Output is correct |
10 |
Correct |
110 ms |
246932 KB |
Output is correct |
11 |
Correct |
112 ms |
247044 KB |
Output is correct |
12 |
Correct |
113 ms |
246980 KB |
Output is correct |
13 |
Correct |
115 ms |
247000 KB |
Output is correct |
14 |
Correct |
112 ms |
247100 KB |
Output is correct |
15 |
Correct |
111 ms |
247172 KB |
Output is correct |
16 |
Correct |
113 ms |
247168 KB |
Output is correct |
17 |
Correct |
113 ms |
248004 KB |
Output is correct |
18 |
Correct |
119 ms |
248372 KB |
Output is correct |
19 |
Correct |
113 ms |
248376 KB |
Output is correct |
20 |
Correct |
114 ms |
248388 KB |
Output is correct |
21 |
Correct |
112 ms |
247144 KB |
Output is correct |
22 |
Correct |
112 ms |
248280 KB |
Output is correct |
23 |
Correct |
115 ms |
248184 KB |
Output is correct |
24 |
Correct |
113 ms |
248624 KB |
Output is correct |
25 |
Correct |
116 ms |
248540 KB |
Output is correct |
26 |
Correct |
113 ms |
248632 KB |
Output is correct |
27 |
Correct |
114 ms |
248732 KB |
Output is correct |
28 |
Correct |
114 ms |
249256 KB |
Output is correct |
29 |
Correct |
128 ms |
250968 KB |
Output is correct |
30 |
Correct |
114 ms |
248864 KB |
Output is correct |
31 |
Correct |
115 ms |
249452 KB |
Output is correct |
32 |
Correct |
122 ms |
249124 KB |
Output is correct |
33 |
Correct |
130 ms |
253692 KB |
Output is correct |
34 |
Correct |
130 ms |
253724 KB |
Output is correct |
35 |
Correct |
127 ms |
251888 KB |
Output is correct |
36 |
Correct |
114 ms |
248200 KB |
Output is correct |
37 |
Correct |
143 ms |
255832 KB |
Output is correct |
38 |
Correct |
139 ms |
254916 KB |
Output is correct |
39 |
Correct |
140 ms |
254908 KB |
Output is correct |
40 |
Correct |
147 ms |
255012 KB |
Output is correct |
41 |
Correct |
139 ms |
254916 KB |
Output is correct |
42 |
Correct |
115 ms |
249180 KB |
Output is correct |
43 |
Correct |
118 ms |
249104 KB |
Output is correct |
44 |
Correct |
119 ms |
248836 KB |
Output is correct |
45 |
Runtime error |
133 ms |
262148 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |