#include <bits/stdc++.h>
using namespace std;
inline long long msb(long long val){return 63-__builtin_clzll(val);}
inline int msb(int val){return 31-__builtin_clz(val);}
//~ #define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
#define emplace_back push_back
template<class T> bool chmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool chmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 30000, M = 30000;
int n, m, k;
bitset<N> vis[N];
int len[N];
vector<int> who_is_here[N];
void solve(int test_case){
int i, j;
cin >> n >> m;
int start;
for(i=0;i<m;i++){
int pos;
cin >> pos >> len[i];
if(i){
who_is_here[pos].pb(i);
}else {
start = pos;
}
}
//~ priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> q;
deque<array<int,3>> q;
q.emplace_back({0,start,0});
vis[start][len[0]] = 1;
while(!q.empty()){
auto [dist, pos, i] = q.front(); q.pop_front();
if(i == 1){
cout << dist << '\n';
return;
}
while(who_is_here[pos].size()){
auto x = who_is_here[pos].back();
who_is_here[pos].pop_back();
if(x == 1){
cout << dist << '\n';
return;
}
if(vis[pos][len[x]])continue;
vis[pos][len[x]] = 1;
q.push_front({dist,pos,x});
}
if(pos - len[i] >=0 && !vis[pos-len[i]][len[i]]){
vis[pos-len[i]][len[i]] = 1;
q.push_back({dist+1,pos-len[i],i});
}
if(pos + len[i] < n && !vis[pos+len[i]][len[i]]){
vis[pos+len[i]][len[i]] = 1;
q.push_back({dist+1,pos+len[i],i});
}
}
cout << -1 << '\n';
return;
}
signed main(){
FASTIO;
//~ #define MULTITEST 1
#if MULTITEST
int _T;
cin >> _T;
for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve(int)':
skyscraper.cpp:23:9: warning: unused variable 'j' [-Wunused-variable]
23 | int i, j;
| ^
skyscraper.cpp:25:6: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
25 | int start;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
4 ms |
3660 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
6 ms |
8364 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
6 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
6 ms |
7628 KB |
Output is correct |
28 |
Correct |
7 ms |
8396 KB |
Output is correct |
29 |
Correct |
7 ms |
8268 KB |
Output is correct |
30 |
Correct |
7 ms |
8268 KB |
Output is correct |
31 |
Correct |
7 ms |
8268 KB |
Output is correct |
32 |
Correct |
6 ms |
8268 KB |
Output is correct |
33 |
Correct |
10 ms |
8396 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
6 ms |
3752 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
8 ms |
8396 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
5 ms |
7276 KB |
Output is correct |
27 |
Correct |
6 ms |
7632 KB |
Output is correct |
28 |
Correct |
7 ms |
8396 KB |
Output is correct |
29 |
Correct |
7 ms |
8348 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
7 ms |
8268 KB |
Output is correct |
32 |
Correct |
7 ms |
8268 KB |
Output is correct |
33 |
Correct |
10 ms |
8396 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 KB |
Output is correct |
35 |
Correct |
14 ms |
6732 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
12 ms |
8528 KB |
Output is correct |
38 |
Correct |
13 ms |
8908 KB |
Output is correct |
39 |
Correct |
8 ms |
1740 KB |
Output is correct |
40 |
Correct |
9 ms |
3916 KB |
Output is correct |
41 |
Correct |
12 ms |
8164 KB |
Output is correct |
42 |
Correct |
9 ms |
7628 KB |
Output is correct |
43 |
Correct |
10 ms |
8012 KB |
Output is correct |
44 |
Correct |
10 ms |
8684 KB |
Output is correct |
45 |
Correct |
31 ms |
9040 KB |
Output is correct |
46 |
Correct |
20 ms |
8968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
3660 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
6 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
7 ms |
7628 KB |
Output is correct |
28 |
Correct |
7 ms |
8388 KB |
Output is correct |
29 |
Correct |
7 ms |
8268 KB |
Output is correct |
30 |
Correct |
6 ms |
8324 KB |
Output is correct |
31 |
Correct |
8 ms |
8268 KB |
Output is correct |
32 |
Correct |
6 ms |
8268 KB |
Output is correct |
33 |
Correct |
9 ms |
8396 KB |
Output is correct |
34 |
Correct |
8 ms |
8380 KB |
Output is correct |
35 |
Correct |
13 ms |
6732 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
11 ms |
8524 KB |
Output is correct |
38 |
Correct |
14 ms |
8912 KB |
Output is correct |
39 |
Correct |
8 ms |
1744 KB |
Output is correct |
40 |
Correct |
9 ms |
4044 KB |
Output is correct |
41 |
Correct |
12 ms |
8148 KB |
Output is correct |
42 |
Correct |
10 ms |
7592 KB |
Output is correct |
43 |
Correct |
10 ms |
8012 KB |
Output is correct |
44 |
Correct |
12 ms |
8652 KB |
Output is correct |
45 |
Correct |
26 ms |
9060 KB |
Output is correct |
46 |
Correct |
18 ms |
9024 KB |
Output is correct |
47 |
Correct |
29 ms |
31692 KB |
Output is correct |
48 |
Correct |
7 ms |
1484 KB |
Output is correct |
49 |
Correct |
8 ms |
1484 KB |
Output is correct |
50 |
Correct |
5 ms |
1356 KB |
Output is correct |
51 |
Correct |
67 ms |
86088 KB |
Output is correct |
52 |
Correct |
76 ms |
93172 KB |
Output is correct |
53 |
Correct |
20 ms |
18520 KB |
Output is correct |
54 |
Correct |
37 ms |
61132 KB |
Output is correct |
55 |
Correct |
47 ms |
77816 KB |
Output is correct |
56 |
Correct |
76 ms |
112148 KB |
Output is correct |
57 |
Correct |
5 ms |
2636 KB |
Output is correct |
58 |
Correct |
73 ms |
111500 KB |
Output is correct |
59 |
Correct |
70 ms |
103752 KB |
Output is correct |
60 |
Correct |
71 ms |
103716 KB |
Output is correct |
61 |
Correct |
73 ms |
98556 KB |
Output is correct |
62 |
Correct |
124 ms |
112196 KB |
Output is correct |
63 |
Correct |
212 ms |
111620 KB |
Output is correct |
64 |
Correct |
232 ms |
111668 KB |
Output is correct |
65 |
Correct |
302 ms |
111812 KB |
Output is correct |
66 |
Correct |
505 ms |
111928 KB |
Output is correct |
67 |
Correct |
278 ms |
111780 KB |
Output is correct |