#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;
vector<bool> vis[N];
int len[N];
vector<int> who_is_here[N];
void solve(int test_case){
int i, j;
for(i=0;i<N;i++)vis[i].assign(N,0);
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;
}
}
deque<tuple<int,int,int>> 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:26:6: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | int start;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
112488 KB |
Output is correct |
2 |
Correct |
54 ms |
112440 KB |
Output is correct |
3 |
Correct |
55 ms |
112460 KB |
Output is correct |
4 |
Correct |
54 ms |
112544 KB |
Output is correct |
5 |
Correct |
53 ms |
112452 KB |
Output is correct |
6 |
Correct |
53 ms |
112508 KB |
Output is correct |
7 |
Correct |
54 ms |
112464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
112536 KB |
Output is correct |
2 |
Correct |
51 ms |
112500 KB |
Output is correct |
3 |
Correct |
57 ms |
112472 KB |
Output is correct |
4 |
Correct |
54 ms |
112488 KB |
Output is correct |
5 |
Correct |
54 ms |
112580 KB |
Output is correct |
6 |
Correct |
54 ms |
112476 KB |
Output is correct |
7 |
Correct |
54 ms |
112524 KB |
Output is correct |
8 |
Correct |
57 ms |
112448 KB |
Output is correct |
9 |
Correct |
55 ms |
112528 KB |
Output is correct |
10 |
Correct |
52 ms |
112552 KB |
Output is correct |
11 |
Correct |
52 ms |
112476 KB |
Output is correct |
12 |
Correct |
54 ms |
112508 KB |
Output is correct |
13 |
Correct |
56 ms |
112516 KB |
Output is correct |
14 |
Correct |
55 ms |
112580 KB |
Output is correct |
15 |
Correct |
55 ms |
112556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
112520 KB |
Output is correct |
2 |
Correct |
54 ms |
112500 KB |
Output is correct |
3 |
Correct |
54 ms |
112520 KB |
Output is correct |
4 |
Correct |
53 ms |
112444 KB |
Output is correct |
5 |
Correct |
53 ms |
112432 KB |
Output is correct |
6 |
Correct |
53 ms |
112488 KB |
Output is correct |
7 |
Correct |
57 ms |
112508 KB |
Output is correct |
8 |
Correct |
53 ms |
112520 KB |
Output is correct |
9 |
Correct |
53 ms |
112488 KB |
Output is correct |
10 |
Correct |
53 ms |
112500 KB |
Output is correct |
11 |
Correct |
52 ms |
112532 KB |
Output is correct |
12 |
Correct |
53 ms |
112568 KB |
Output is correct |
13 |
Correct |
57 ms |
112568 KB |
Output is correct |
14 |
Correct |
56 ms |
112580 KB |
Output is correct |
15 |
Correct |
51 ms |
112608 KB |
Output is correct |
16 |
Correct |
55 ms |
112488 KB |
Output is correct |
17 |
Correct |
54 ms |
112504 KB |
Output is correct |
18 |
Correct |
53 ms |
112580 KB |
Output is correct |
19 |
Correct |
53 ms |
112500 KB |
Output is correct |
20 |
Correct |
56 ms |
112560 KB |
Output is correct |
21 |
Correct |
54 ms |
112508 KB |
Output is correct |
22 |
Correct |
54 ms |
112580 KB |
Output is correct |
23 |
Correct |
53 ms |
112504 KB |
Output is correct |
24 |
Correct |
55 ms |
112552 KB |
Output is correct |
25 |
Correct |
54 ms |
112552 KB |
Output is correct |
26 |
Correct |
53 ms |
112488 KB |
Output is correct |
27 |
Correct |
52 ms |
112512 KB |
Output is correct |
28 |
Correct |
63 ms |
112580 KB |
Output is correct |
29 |
Correct |
55 ms |
112476 KB |
Output is correct |
30 |
Correct |
54 ms |
112556 KB |
Output is correct |
31 |
Correct |
53 ms |
112544 KB |
Output is correct |
32 |
Correct |
52 ms |
112548 KB |
Output is correct |
33 |
Correct |
63 ms |
112656 KB |
Output is correct |
34 |
Correct |
55 ms |
112564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
112480 KB |
Output is correct |
2 |
Correct |
53 ms |
112444 KB |
Output is correct |
3 |
Correct |
53 ms |
112452 KB |
Output is correct |
4 |
Correct |
53 ms |
112464 KB |
Output is correct |
5 |
Correct |
56 ms |
112560 KB |
Output is correct |
6 |
Correct |
54 ms |
112512 KB |
Output is correct |
7 |
Correct |
53 ms |
112452 KB |
Output is correct |
8 |
Correct |
53 ms |
112544 KB |
Output is correct |
9 |
Correct |
52 ms |
112552 KB |
Output is correct |
10 |
Correct |
53 ms |
112488 KB |
Output is correct |
11 |
Correct |
53 ms |
112540 KB |
Output is correct |
12 |
Correct |
55 ms |
112576 KB |
Output is correct |
13 |
Correct |
53 ms |
112540 KB |
Output is correct |
14 |
Correct |
55 ms |
112496 KB |
Output is correct |
15 |
Correct |
64 ms |
112512 KB |
Output is correct |
16 |
Correct |
53 ms |
112576 KB |
Output is correct |
17 |
Correct |
56 ms |
112524 KB |
Output is correct |
18 |
Correct |
54 ms |
112544 KB |
Output is correct |
19 |
Correct |
55 ms |
112528 KB |
Output is correct |
20 |
Correct |
54 ms |
112612 KB |
Output is correct |
21 |
Correct |
54 ms |
112500 KB |
Output is correct |
22 |
Correct |
54 ms |
112552 KB |
Output is correct |
23 |
Correct |
54 ms |
112580 KB |
Output is correct |
24 |
Correct |
53 ms |
112584 KB |
Output is correct |
25 |
Correct |
55 ms |
112560 KB |
Output is correct |
26 |
Correct |
54 ms |
112568 KB |
Output is correct |
27 |
Correct |
52 ms |
112584 KB |
Output is correct |
28 |
Correct |
54 ms |
112612 KB |
Output is correct |
29 |
Correct |
57 ms |
112536 KB |
Output is correct |
30 |
Correct |
55 ms |
112452 KB |
Output is correct |
31 |
Correct |
54 ms |
112512 KB |
Output is correct |
32 |
Correct |
55 ms |
112580 KB |
Output is correct |
33 |
Correct |
59 ms |
112588 KB |
Output is correct |
34 |
Correct |
57 ms |
112532 KB |
Output is correct |
35 |
Correct |
68 ms |
113260 KB |
Output is correct |
36 |
Correct |
55 ms |
112544 KB |
Output is correct |
37 |
Correct |
61 ms |
113084 KB |
Output is correct |
38 |
Correct |
63 ms |
113644 KB |
Output is correct |
39 |
Correct |
60 ms |
113168 KB |
Output is correct |
40 |
Correct |
59 ms |
113176 KB |
Output is correct |
41 |
Correct |
60 ms |
113148 KB |
Output is correct |
42 |
Correct |
59 ms |
113092 KB |
Output is correct |
43 |
Correct |
58 ms |
113092 KB |
Output is correct |
44 |
Correct |
60 ms |
113068 KB |
Output is correct |
45 |
Correct |
81 ms |
113464 KB |
Output is correct |
46 |
Correct |
67 ms |
113336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
112532 KB |
Output is correct |
2 |
Correct |
52 ms |
112548 KB |
Output is correct |
3 |
Correct |
53 ms |
112532 KB |
Output is correct |
4 |
Correct |
52 ms |
112452 KB |
Output is correct |
5 |
Correct |
54 ms |
112504 KB |
Output is correct |
6 |
Correct |
54 ms |
112516 KB |
Output is correct |
7 |
Correct |
53 ms |
112548 KB |
Output is correct |
8 |
Correct |
52 ms |
112460 KB |
Output is correct |
9 |
Correct |
52 ms |
112452 KB |
Output is correct |
10 |
Correct |
55 ms |
112564 KB |
Output is correct |
11 |
Correct |
57 ms |
112512 KB |
Output is correct |
12 |
Correct |
54 ms |
112480 KB |
Output is correct |
13 |
Correct |
52 ms |
112484 KB |
Output is correct |
14 |
Correct |
54 ms |
112588 KB |
Output is correct |
15 |
Correct |
53 ms |
112544 KB |
Output is correct |
16 |
Correct |
53 ms |
112580 KB |
Output is correct |
17 |
Correct |
54 ms |
112556 KB |
Output is correct |
18 |
Correct |
53 ms |
112564 KB |
Output is correct |
19 |
Correct |
54 ms |
112476 KB |
Output is correct |
20 |
Correct |
54 ms |
112580 KB |
Output is correct |
21 |
Correct |
54 ms |
112516 KB |
Output is correct |
22 |
Correct |
52 ms |
112488 KB |
Output is correct |
23 |
Correct |
53 ms |
112572 KB |
Output is correct |
24 |
Correct |
54 ms |
112580 KB |
Output is correct |
25 |
Correct |
55 ms |
112592 KB |
Output is correct |
26 |
Correct |
52 ms |
112492 KB |
Output is correct |
27 |
Correct |
53 ms |
112580 KB |
Output is correct |
28 |
Correct |
54 ms |
112580 KB |
Output is correct |
29 |
Correct |
55 ms |
112544 KB |
Output is correct |
30 |
Correct |
54 ms |
112452 KB |
Output is correct |
31 |
Correct |
55 ms |
112464 KB |
Output is correct |
32 |
Correct |
54 ms |
112452 KB |
Output is correct |
33 |
Correct |
56 ms |
112552 KB |
Output is correct |
34 |
Correct |
56 ms |
112580 KB |
Output is correct |
35 |
Correct |
62 ms |
113220 KB |
Output is correct |
36 |
Correct |
55 ms |
112560 KB |
Output is correct |
37 |
Correct |
59 ms |
113072 KB |
Output is correct |
38 |
Correct |
63 ms |
113416 KB |
Output is correct |
39 |
Correct |
60 ms |
113156 KB |
Output is correct |
40 |
Correct |
59 ms |
113092 KB |
Output is correct |
41 |
Correct |
63 ms |
113144 KB |
Output is correct |
42 |
Correct |
57 ms |
113100 KB |
Output is correct |
43 |
Correct |
57 ms |
113124 KB |
Output is correct |
44 |
Correct |
60 ms |
113040 KB |
Output is correct |
45 |
Correct |
76 ms |
113460 KB |
Output is correct |
46 |
Correct |
68 ms |
113336 KB |
Output is correct |
47 |
Correct |
62 ms |
113304 KB |
Output is correct |
48 |
Correct |
59 ms |
113220 KB |
Output is correct |
49 |
Correct |
63 ms |
113252 KB |
Output is correct |
50 |
Correct |
59 ms |
113120 KB |
Output is correct |
51 |
Correct |
68 ms |
113572 KB |
Output is correct |
52 |
Correct |
70 ms |
113616 KB |
Output is correct |
53 |
Correct |
63 ms |
113504 KB |
Output is correct |
54 |
Correct |
54 ms |
112456 KB |
Output is correct |
55 |
Correct |
54 ms |
112452 KB |
Output is correct |
56 |
Correct |
64 ms |
113864 KB |
Output is correct |
57 |
Correct |
55 ms |
112624 KB |
Output is correct |
58 |
Correct |
66 ms |
112932 KB |
Output is correct |
59 |
Correct |
62 ms |
113124 KB |
Output is correct |
60 |
Correct |
63 ms |
113088 KB |
Output is correct |
61 |
Correct |
62 ms |
113124 KB |
Output is correct |
62 |
Correct |
96 ms |
113832 KB |
Output is correct |
63 |
Correct |
229 ms |
113568 KB |
Output is correct |
64 |
Correct |
264 ms |
113328 KB |
Output is correct |
65 |
Correct |
339 ms |
113348 KB |
Output is correct |
66 |
Correct |
541 ms |
113448 KB |
Output is correct |
67 |
Correct |
301 ms |
113456 KB |
Output is correct |