#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define debug(x) cerr<<#x<<" :"<<x<<"\n"
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
//#define int long long
typedef long long ll;
typedef long double ld;
const int maxn = 3e4 + 7;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int mlog = 20;
const int SQRT = 170;
int n, m;
int a[maxn], p[maxn];
vector<int> vec[maxn];
vector<pii> q[maxn * SQRT];
int ss, res;
int H[maxn][SQRT], F[maxn];
void add(int x,int u,int t)
{
if(u >= SQRT)
{
for(int i = 0; i < SQRT; i++)
{
if(x + i*u < n && !H[x+i*u][0])
ss++, q[t+i].pb(mp(x+i*u,0));
if(x - i*u >= 0 && !H[x-i*u][0])
ss++, q[t+i].pb(mp(x-i*u,0));
}
}
else if(!H[x][u]) ss++, q[t].pb(mp(x,u));
}
int32_t main()
{
FAST;
cin >> n >> m;
for(int i = 0; i < m; i++)
{
cin >> a[i] >> p[i];
vec[a[i]].pb(p[i]);
}
add(a[0], p[0], 0);
for(res = 0; ss; res++)
{
for(int i = 0; i < q[res].size(); i++, ss--)
{
int x = q[res][i].F;
int u = q[res][i].S;
if(x == a[1]) return cout << res << "\n", 0;
if(H[x][u]) continue;
H[x][u] = 1;
if(u) if(x + u < n && !H[x + u][u]) ss++ ,q[res + 1].pb(mp(x + u,u));
if(x - u >= 0 && !H[x - u][u]) ss++, q[res + 1].pb(mp(x - u,u));
if(F[x]) continue;
F[x] = 1;
for(int j = 0; j < vec[x].size(); j++)
add(x, vec[x][j], res);
}
}
cout << -1 << "\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:67:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < q[res].size(); i++, ss--)
| ~~^~~~~~~~~~~~~~~
skyscraper.cpp:83:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int j = 0; j < vec[x].size(); j++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
120708 KB |
Output is correct |
2 |
Correct |
78 ms |
120776 KB |
Output is correct |
3 |
Correct |
74 ms |
120900 KB |
Output is correct |
4 |
Correct |
71 ms |
120700 KB |
Output is correct |
5 |
Correct |
73 ms |
120792 KB |
Output is correct |
6 |
Correct |
73 ms |
120708 KB |
Output is correct |
7 |
Correct |
72 ms |
120804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
120756 KB |
Output is correct |
2 |
Correct |
78 ms |
120732 KB |
Output is correct |
3 |
Correct |
72 ms |
120772 KB |
Output is correct |
4 |
Correct |
72 ms |
120780 KB |
Output is correct |
5 |
Correct |
74 ms |
120692 KB |
Output is correct |
6 |
Correct |
73 ms |
120772 KB |
Output is correct |
7 |
Correct |
73 ms |
120772 KB |
Output is correct |
8 |
Correct |
73 ms |
120688 KB |
Output is correct |
9 |
Correct |
70 ms |
120732 KB |
Output is correct |
10 |
Correct |
72 ms |
120800 KB |
Output is correct |
11 |
Correct |
71 ms |
120824 KB |
Output is correct |
12 |
Correct |
73 ms |
120848 KB |
Output is correct |
13 |
Correct |
76 ms |
120876 KB |
Output is correct |
14 |
Correct |
74 ms |
120904 KB |
Output is correct |
15 |
Correct |
75 ms |
120924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
120772 KB |
Output is correct |
2 |
Correct |
72 ms |
120772 KB |
Output is correct |
3 |
Correct |
70 ms |
120764 KB |
Output is correct |
4 |
Correct |
72 ms |
120764 KB |
Output is correct |
5 |
Correct |
72 ms |
120700 KB |
Output is correct |
6 |
Correct |
72 ms |
120764 KB |
Output is correct |
7 |
Correct |
73 ms |
120824 KB |
Output is correct |
8 |
Correct |
73 ms |
120900 KB |
Output is correct |
9 |
Correct |
70 ms |
120836 KB |
Output is correct |
10 |
Correct |
73 ms |
120772 KB |
Output is correct |
11 |
Correct |
76 ms |
120772 KB |
Output is correct |
12 |
Correct |
75 ms |
120916 KB |
Output is correct |
13 |
Correct |
73 ms |
120908 KB |
Output is correct |
14 |
Correct |
72 ms |
120940 KB |
Output is correct |
15 |
Correct |
75 ms |
120880 KB |
Output is correct |
16 |
Correct |
71 ms |
120744 KB |
Output is correct |
17 |
Correct |
76 ms |
121412 KB |
Output is correct |
18 |
Correct |
73 ms |
120896 KB |
Output is correct |
19 |
Correct |
74 ms |
120900 KB |
Output is correct |
20 |
Correct |
75 ms |
122180 KB |
Output is correct |
21 |
Correct |
76 ms |
120720 KB |
Output is correct |
22 |
Correct |
73 ms |
120808 KB |
Output is correct |
23 |
Correct |
78 ms |
121924 KB |
Output is correct |
24 |
Correct |
74 ms |
122192 KB |
Output is correct |
25 |
Correct |
74 ms |
121656 KB |
Output is correct |
26 |
Correct |
81 ms |
122180 KB |
Output is correct |
27 |
Correct |
72 ms |
122260 KB |
Output is correct |
28 |
Correct |
77 ms |
122668 KB |
Output is correct |
29 |
Correct |
79 ms |
122796 KB |
Output is correct |
30 |
Correct |
74 ms |
122368 KB |
Output is correct |
31 |
Correct |
73 ms |
122572 KB |
Output is correct |
32 |
Correct |
75 ms |
122436 KB |
Output is correct |
33 |
Correct |
76 ms |
123256 KB |
Output is correct |
34 |
Correct |
73 ms |
122600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
120772 KB |
Output is correct |
2 |
Correct |
71 ms |
120772 KB |
Output is correct |
3 |
Correct |
75 ms |
120804 KB |
Output is correct |
4 |
Correct |
71 ms |
120772 KB |
Output is correct |
5 |
Correct |
72 ms |
120776 KB |
Output is correct |
6 |
Correct |
72 ms |
120792 KB |
Output is correct |
7 |
Correct |
72 ms |
120772 KB |
Output is correct |
8 |
Correct |
72 ms |
120772 KB |
Output is correct |
9 |
Correct |
71 ms |
120776 KB |
Output is correct |
10 |
Correct |
75 ms |
120900 KB |
Output is correct |
11 |
Correct |
70 ms |
120864 KB |
Output is correct |
12 |
Correct |
74 ms |
120876 KB |
Output is correct |
13 |
Correct |
73 ms |
120900 KB |
Output is correct |
14 |
Correct |
75 ms |
120936 KB |
Output is correct |
15 |
Correct |
72 ms |
120848 KB |
Output is correct |
16 |
Correct |
74 ms |
120824 KB |
Output is correct |
17 |
Correct |
72 ms |
121328 KB |
Output is correct |
18 |
Correct |
73 ms |
120832 KB |
Output is correct |
19 |
Correct |
71 ms |
120772 KB |
Output is correct |
20 |
Correct |
75 ms |
122244 KB |
Output is correct |
21 |
Correct |
74 ms |
120772 KB |
Output is correct |
22 |
Correct |
70 ms |
120772 KB |
Output is correct |
23 |
Correct |
75 ms |
121900 KB |
Output is correct |
24 |
Correct |
71 ms |
122196 KB |
Output is correct |
25 |
Correct |
80 ms |
121796 KB |
Output is correct |
26 |
Correct |
79 ms |
122244 KB |
Output is correct |
27 |
Correct |
79 ms |
122216 KB |
Output is correct |
28 |
Correct |
77 ms |
122652 KB |
Output is correct |
29 |
Correct |
77 ms |
122868 KB |
Output is correct |
30 |
Correct |
74 ms |
122368 KB |
Output is correct |
31 |
Correct |
75 ms |
122560 KB |
Output is correct |
32 |
Correct |
74 ms |
122440 KB |
Output is correct |
33 |
Correct |
76 ms |
123204 KB |
Output is correct |
34 |
Correct |
75 ms |
122688 KB |
Output is correct |
35 |
Correct |
85 ms |
122420 KB |
Output is correct |
36 |
Correct |
72 ms |
121468 KB |
Output is correct |
37 |
Correct |
81 ms |
122564 KB |
Output is correct |
38 |
Correct |
89 ms |
122904 KB |
Output is correct |
39 |
Correct |
79 ms |
121384 KB |
Output is correct |
40 |
Correct |
80 ms |
121412 KB |
Output is correct |
41 |
Correct |
80 ms |
122384 KB |
Output is correct |
42 |
Correct |
83 ms |
122604 KB |
Output is correct |
43 |
Correct |
83 ms |
122596 KB |
Output is correct |
44 |
Correct |
77 ms |
122512 KB |
Output is correct |
45 |
Correct |
96 ms |
126640 KB |
Output is correct |
46 |
Correct |
92 ms |
125308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
120748 KB |
Output is correct |
2 |
Correct |
75 ms |
120756 KB |
Output is correct |
3 |
Correct |
73 ms |
120772 KB |
Output is correct |
4 |
Correct |
76 ms |
120752 KB |
Output is correct |
5 |
Correct |
70 ms |
120724 KB |
Output is correct |
6 |
Correct |
70 ms |
120736 KB |
Output is correct |
7 |
Correct |
73 ms |
120712 KB |
Output is correct |
8 |
Correct |
70 ms |
120800 KB |
Output is correct |
9 |
Correct |
73 ms |
120912 KB |
Output is correct |
10 |
Correct |
73 ms |
120852 KB |
Output is correct |
11 |
Correct |
76 ms |
120832 KB |
Output is correct |
12 |
Correct |
72 ms |
120800 KB |
Output is correct |
13 |
Correct |
75 ms |
120912 KB |
Output is correct |
14 |
Correct |
73 ms |
120920 KB |
Output is correct |
15 |
Correct |
72 ms |
121024 KB |
Output is correct |
16 |
Correct |
77 ms |
120780 KB |
Output is correct |
17 |
Correct |
73 ms |
121332 KB |
Output is correct |
18 |
Correct |
73 ms |
120848 KB |
Output is correct |
19 |
Correct |
77 ms |
120864 KB |
Output is correct |
20 |
Correct |
75 ms |
122288 KB |
Output is correct |
21 |
Correct |
73 ms |
120960 KB |
Output is correct |
22 |
Correct |
73 ms |
120772 KB |
Output is correct |
23 |
Correct |
73 ms |
121948 KB |
Output is correct |
24 |
Correct |
78 ms |
122184 KB |
Output is correct |
25 |
Correct |
72 ms |
121712 KB |
Output is correct |
26 |
Correct |
73 ms |
122248 KB |
Output is correct |
27 |
Correct |
71 ms |
122180 KB |
Output is correct |
28 |
Correct |
76 ms |
122668 KB |
Output is correct |
29 |
Correct |
74 ms |
122860 KB |
Output is correct |
30 |
Correct |
72 ms |
122360 KB |
Output is correct |
31 |
Correct |
75 ms |
122576 KB |
Output is correct |
32 |
Correct |
75 ms |
122380 KB |
Output is correct |
33 |
Correct |
76 ms |
123204 KB |
Output is correct |
34 |
Correct |
73 ms |
122612 KB |
Output is correct |
35 |
Correct |
84 ms |
122588 KB |
Output is correct |
36 |
Correct |
78 ms |
121540 KB |
Output is correct |
37 |
Correct |
83 ms |
122640 KB |
Output is correct |
38 |
Correct |
84 ms |
122880 KB |
Output is correct |
39 |
Correct |
78 ms |
121232 KB |
Output is correct |
40 |
Correct |
89 ms |
121460 KB |
Output is correct |
41 |
Correct |
80 ms |
122436 KB |
Output is correct |
42 |
Correct |
78 ms |
122548 KB |
Output is correct |
43 |
Correct |
78 ms |
122516 KB |
Output is correct |
44 |
Correct |
80 ms |
122548 KB |
Output is correct |
45 |
Correct |
91 ms |
126720 KB |
Output is correct |
46 |
Correct |
91 ms |
125372 KB |
Output is correct |
47 |
Correct |
92 ms |
129220 KB |
Output is correct |
48 |
Correct |
82 ms |
121412 KB |
Output is correct |
49 |
Correct |
82 ms |
121540 KB |
Output is correct |
50 |
Correct |
77 ms |
121252 KB |
Output is correct |
51 |
Correct |
106 ms |
142248 KB |
Output is correct |
52 |
Correct |
110 ms |
142304 KB |
Output is correct |
53 |
Correct |
86 ms |
126720 KB |
Output is correct |
54 |
Correct |
85 ms |
141364 KB |
Output is correct |
55 |
Correct |
88 ms |
141536 KB |
Output is correct |
56 |
Correct |
96 ms |
142992 KB |
Output is correct |
57 |
Correct |
77 ms |
121352 KB |
Output is correct |
58 |
Correct |
98 ms |
143268 KB |
Output is correct |
59 |
Correct |
96 ms |
143008 KB |
Output is correct |
60 |
Correct |
97 ms |
142916 KB |
Output is correct |
61 |
Correct |
98 ms |
142660 KB |
Output is correct |
62 |
Correct |
149 ms |
150060 KB |
Output is correct |
63 |
Correct |
147 ms |
151836 KB |
Output is correct |
64 |
Correct |
156 ms |
153280 KB |
Output is correct |
65 |
Correct |
250 ms |
171104 KB |
Output is correct |
66 |
Correct |
415 ms |
203920 KB |
Output is correct |
67 |
Correct |
220 ms |
176572 KB |
Output is correct |