#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=30010;
const int mxM=3000000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N, M;
vector <pii> BP;
vector <pair<int, bool>> adj[mxM];
int idx;
int X, Y;
list <int> lst;
int dis[mxM];
bool cmp1(pii a, pii b)
{
if(a.sec!=b.sec) return a.sec<b.sec;
if(a.fir%a.sec!=b.fir%b.sec) return a.fir%a.sec<b.fir%b.sec;
return a.fir<b.fir;
}
void zero_one_bfs()
{
int s=0, e=0;
while(!lst.empty())
{
int now=lst.front();
lst.pop_front();
for(auto nxt : adj[now])
{
if(dis[nxt.fir]>dis[now]+nxt.sec)
{
dis[nxt.fir]=dis[now]+nxt.sec;
if(dis[now]==dis[nxt.fir]) lst.push_front(nxt.fir);
else lst.push_back(nxt.fir);
}
}
}
}
int main()
{
gibon
cin >> N >> M;
idx=N;
BP.resize(M);
for(int i=0;i<M;i++)
{
cin >> BP[i].fir >> BP[i].sec;
if(i==0) X=BP[i].fir;
if(i==1) Y=BP[i].fir;
}
sort(BP.begin(), BP.end(), cmp1);
int cur=0;
while(cur<M)
{
int ncur=cur;
bool jog=false;
for(int j=BP[ncur].fir%BP[ncur].sec;j<N;j+=BP[ncur].sec)
{
if(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec)
{
adj[j].push_back({idx, 0});
while(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec) cur++;
}
adj[idx].push_back({j, 0});
if(!jog) jog=true;
else
{
adj[idx].push_back({idx-1, 1});
adj[idx-1].push_back({idx, 1});
}
idx++;
if(idx>=mxM) return 0;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=MOD;
}
dis[X]=0;
lst.push_front(X);
zero_one_bfs();
if(dis[Y]==MOD) cout << -1;
else cout << dis[Y];
}
Compilation message
skyscraper.cpp: In function 'void zero_one_bfs()':
skyscraper.cpp:34:9: warning: unused variable 's' [-Wunused-variable]
34 | int s=0, e=0;
| ^
skyscraper.cpp:34:14: warning: unused variable 'e' [-Wunused-variable]
34 | int s=0, e=0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
70708 KB |
Output is correct |
2 |
Correct |
39 ms |
70728 KB |
Output is correct |
3 |
Correct |
41 ms |
70756 KB |
Output is correct |
4 |
Correct |
44 ms |
70744 KB |
Output is correct |
5 |
Correct |
38 ms |
70680 KB |
Output is correct |
6 |
Correct |
40 ms |
70644 KB |
Output is correct |
7 |
Correct |
39 ms |
70632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
70692 KB |
Output is correct |
2 |
Correct |
40 ms |
70640 KB |
Output is correct |
3 |
Correct |
40 ms |
70692 KB |
Output is correct |
4 |
Correct |
40 ms |
70724 KB |
Output is correct |
5 |
Correct |
43 ms |
70696 KB |
Output is correct |
6 |
Correct |
40 ms |
70664 KB |
Output is correct |
7 |
Correct |
39 ms |
70716 KB |
Output is correct |
8 |
Correct |
38 ms |
70732 KB |
Output is correct |
9 |
Correct |
38 ms |
70780 KB |
Output is correct |
10 |
Correct |
40 ms |
70796 KB |
Output is correct |
11 |
Correct |
43 ms |
70936 KB |
Output is correct |
12 |
Correct |
39 ms |
70780 KB |
Output is correct |
13 |
Correct |
39 ms |
70768 KB |
Output is correct |
14 |
Correct |
41 ms |
71076 KB |
Output is correct |
15 |
Correct |
41 ms |
70980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
70732 KB |
Output is correct |
2 |
Correct |
39 ms |
70712 KB |
Output is correct |
3 |
Correct |
40 ms |
70652 KB |
Output is correct |
4 |
Correct |
42 ms |
70632 KB |
Output is correct |
5 |
Correct |
39 ms |
70744 KB |
Output is correct |
6 |
Correct |
40 ms |
70704 KB |
Output is correct |
7 |
Correct |
40 ms |
70740 KB |
Output is correct |
8 |
Correct |
40 ms |
70636 KB |
Output is correct |
9 |
Correct |
39 ms |
70756 KB |
Output is correct |
10 |
Correct |
39 ms |
70724 KB |
Output is correct |
11 |
Correct |
40 ms |
70932 KB |
Output is correct |
12 |
Correct |
39 ms |
70688 KB |
Output is correct |
13 |
Correct |
39 ms |
70776 KB |
Output is correct |
14 |
Correct |
42 ms |
71024 KB |
Output is correct |
15 |
Correct |
41 ms |
70980 KB |
Output is correct |
16 |
Correct |
39 ms |
70828 KB |
Output is correct |
17 |
Correct |
41 ms |
71420 KB |
Output is correct |
18 |
Correct |
40 ms |
70992 KB |
Output is correct |
19 |
Correct |
39 ms |
70948 KB |
Output is correct |
20 |
Correct |
39 ms |
70912 KB |
Output is correct |
21 |
Correct |
39 ms |
70772 KB |
Output is correct |
22 |
Correct |
41 ms |
70932 KB |
Output is correct |
23 |
Correct |
47 ms |
70988 KB |
Output is correct |
24 |
Correct |
41 ms |
71392 KB |
Output is correct |
25 |
Correct |
40 ms |
71116 KB |
Output is correct |
26 |
Correct |
41 ms |
71108 KB |
Output is correct |
27 |
Correct |
42 ms |
71132 KB |
Output is correct |
28 |
Correct |
46 ms |
71828 KB |
Output is correct |
29 |
Correct |
50 ms |
73880 KB |
Output is correct |
30 |
Correct |
48 ms |
71564 KB |
Output is correct |
31 |
Correct |
45 ms |
72444 KB |
Output is correct |
32 |
Correct |
46 ms |
71952 KB |
Output is correct |
33 |
Correct |
72 ms |
76844 KB |
Output is correct |
34 |
Correct |
73 ms |
76868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
70624 KB |
Output is correct |
2 |
Correct |
39 ms |
70744 KB |
Output is correct |
3 |
Correct |
40 ms |
70744 KB |
Output is correct |
4 |
Correct |
39 ms |
70716 KB |
Output is correct |
5 |
Correct |
40 ms |
70724 KB |
Output is correct |
6 |
Correct |
38 ms |
70716 KB |
Output is correct |
7 |
Correct |
38 ms |
70684 KB |
Output is correct |
8 |
Correct |
39 ms |
70708 KB |
Output is correct |
9 |
Correct |
39 ms |
70652 KB |
Output is correct |
10 |
Correct |
40 ms |
70840 KB |
Output is correct |
11 |
Correct |
47 ms |
70932 KB |
Output is correct |
12 |
Correct |
44 ms |
70756 KB |
Output is correct |
13 |
Correct |
41 ms |
70768 KB |
Output is correct |
14 |
Correct |
40 ms |
71076 KB |
Output is correct |
15 |
Correct |
40 ms |
71116 KB |
Output is correct |
16 |
Correct |
40 ms |
70860 KB |
Output is correct |
17 |
Correct |
41 ms |
71480 KB |
Output is correct |
18 |
Correct |
39 ms |
70996 KB |
Output is correct |
19 |
Correct |
41 ms |
70916 KB |
Output is correct |
20 |
Correct |
40 ms |
70852 KB |
Output is correct |
21 |
Correct |
45 ms |
70880 KB |
Output is correct |
22 |
Correct |
45 ms |
71020 KB |
Output is correct |
23 |
Correct |
41 ms |
71088 KB |
Output is correct |
24 |
Correct |
42 ms |
71396 KB |
Output is correct |
25 |
Correct |
41 ms |
71048 KB |
Output is correct |
26 |
Correct |
40 ms |
71108 KB |
Output is correct |
27 |
Correct |
40 ms |
71100 KB |
Output is correct |
28 |
Correct |
43 ms |
71952 KB |
Output is correct |
29 |
Correct |
61 ms |
73796 KB |
Output is correct |
30 |
Correct |
43 ms |
71680 KB |
Output is correct |
31 |
Correct |
49 ms |
72412 KB |
Output is correct |
32 |
Correct |
49 ms |
71848 KB |
Output is correct |
33 |
Correct |
63 ms |
76924 KB |
Output is correct |
34 |
Correct |
63 ms |
76868 KB |
Output is correct |
35 |
Correct |
71 ms |
76336 KB |
Output is correct |
36 |
Correct |
44 ms |
71596 KB |
Output is correct |
37 |
Correct |
92 ms |
80068 KB |
Output is correct |
38 |
Correct |
95 ms |
79460 KB |
Output is correct |
39 |
Correct |
91 ms |
79456 KB |
Output is correct |
40 |
Correct |
98 ms |
79556 KB |
Output is correct |
41 |
Correct |
94 ms |
79488 KB |
Output is correct |
42 |
Correct |
50 ms |
71364 KB |
Output is correct |
43 |
Correct |
50 ms |
71236 KB |
Output is correct |
44 |
Correct |
50 ms |
71112 KB |
Output is correct |
45 |
Correct |
199 ms |
95700 KB |
Output is correct |
46 |
Correct |
206 ms |
95712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70844 KB |
Output is correct |
2 |
Correct |
40 ms |
70644 KB |
Output is correct |
3 |
Correct |
39 ms |
70724 KB |
Output is correct |
4 |
Correct |
38 ms |
70732 KB |
Output is correct |
5 |
Correct |
39 ms |
70760 KB |
Output is correct |
6 |
Correct |
40 ms |
70724 KB |
Output is correct |
7 |
Correct |
39 ms |
70744 KB |
Output is correct |
8 |
Correct |
41 ms |
70724 KB |
Output is correct |
9 |
Correct |
38 ms |
70640 KB |
Output is correct |
10 |
Correct |
40 ms |
70756 KB |
Output is correct |
11 |
Correct |
41 ms |
70976 KB |
Output is correct |
12 |
Correct |
39 ms |
70776 KB |
Output is correct |
13 |
Correct |
39 ms |
70732 KB |
Output is correct |
14 |
Correct |
40 ms |
71000 KB |
Output is correct |
15 |
Correct |
40 ms |
70968 KB |
Output is correct |
16 |
Correct |
41 ms |
70932 KB |
Output is correct |
17 |
Correct |
46 ms |
71548 KB |
Output is correct |
18 |
Correct |
41 ms |
71008 KB |
Output is correct |
19 |
Correct |
39 ms |
71000 KB |
Output is correct |
20 |
Correct |
40 ms |
70944 KB |
Output is correct |
21 |
Correct |
47 ms |
70876 KB |
Output is correct |
22 |
Correct |
45 ms |
71008 KB |
Output is correct |
23 |
Correct |
40 ms |
71032 KB |
Output is correct |
24 |
Correct |
41 ms |
71404 KB |
Output is correct |
25 |
Correct |
41 ms |
71100 KB |
Output is correct |
26 |
Correct |
40 ms |
71144 KB |
Output is correct |
27 |
Correct |
40 ms |
71104 KB |
Output is correct |
28 |
Correct |
43 ms |
71880 KB |
Output is correct |
29 |
Correct |
52 ms |
73804 KB |
Output is correct |
30 |
Correct |
43 ms |
71540 KB |
Output is correct |
31 |
Correct |
46 ms |
72440 KB |
Output is correct |
32 |
Correct |
44 ms |
71876 KB |
Output is correct |
33 |
Correct |
64 ms |
76872 KB |
Output is correct |
34 |
Correct |
64 ms |
76908 KB |
Output is correct |
35 |
Correct |
73 ms |
76344 KB |
Output is correct |
36 |
Correct |
43 ms |
71688 KB |
Output is correct |
37 |
Correct |
107 ms |
80060 KB |
Output is correct |
38 |
Correct |
91 ms |
79512 KB |
Output is correct |
39 |
Correct |
104 ms |
79576 KB |
Output is correct |
40 |
Correct |
102 ms |
79528 KB |
Output is correct |
41 |
Correct |
92 ms |
79556 KB |
Output is correct |
42 |
Correct |
50 ms |
71364 KB |
Output is correct |
43 |
Correct |
51 ms |
71244 KB |
Output is correct |
44 |
Correct |
53 ms |
71148 KB |
Output is correct |
45 |
Correct |
221 ms |
95700 KB |
Output is correct |
46 |
Correct |
205 ms |
95700 KB |
Output is correct |
47 |
Correct |
276 ms |
109248 KB |
Output is correct |
48 |
Correct |
78 ms |
82756 KB |
Output is correct |
49 |
Correct |
66 ms |
78204 KB |
Output is correct |
50 |
Correct |
68 ms |
78192 KB |
Output is correct |
51 |
Correct |
139 ms |
87256 KB |
Output is correct |
52 |
Correct |
147 ms |
88676 KB |
Output is correct |
53 |
Correct |
67 ms |
75680 KB |
Output is correct |
54 |
Correct |
43 ms |
72284 KB |
Output is correct |
55 |
Correct |
47 ms |
73660 KB |
Output is correct |
56 |
Correct |
65 ms |
73572 KB |
Output is correct |
57 |
Correct |
168 ms |
92896 KB |
Output is correct |
58 |
Correct |
58 ms |
74464 KB |
Output is correct |
59 |
Correct |
71 ms |
76000 KB |
Output is correct |
60 |
Correct |
73 ms |
77332 KB |
Output is correct |
61 |
Correct |
69 ms |
76516 KB |
Output is correct |
62 |
Correct |
137 ms |
96280 KB |
Output is correct |
63 |
Correct |
788 ms |
187304 KB |
Output is correct |
64 |
Correct |
953 ms |
213552 KB |
Output is correct |
65 |
Incorrect |
412 ms |
210244 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |