#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int mod = (int) 1e9 + 7;
const ll inf = 1LL << 60;
const int maxn = (int) 3e4 + 5;
const ld eps = 1e-9;
int n, m, b[maxn], p[maxn], f[maxn];
unordered_map<int, int> d[maxn];
vector<int> a[maxn];
deque<pair<int, ii> > pq;
int main() {
//freopen("test.txt", "r", stdin);
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
// b[i] = rand() % n;
// p[i] = rand() % 30000 + 1;
a[b[i]].pb(p[i]);
d[b[i]][p[i]] = mod;
}
rep(i, 0, n) {
sort(all(a[i]));
a[i].resize(unique(all(a[i])) - a[i].begin());
}
d[b[0]][p[0]] = 0;
pq.push_back(make_pair(0, ii(b[0], p[0])));
while(!pq.empty()) {
int du = pq.front().fi;
int u = pq.front().se.fi;
int t = pq.front().se.se;
pq.pop_front();
if(du != d[u][t]) continue;
rep(i, -1, 2) if(i) {
int v = u + i * t;
if(v < 0 || v >= n) continue;
if(d[v].count(t) && d[v][t] <= du + 1) continue;
d[v][t] = du + 1;
pq.push_back(make_pair(du + 1, ii(v, t)));
}
if(f[u]) continue;
for (auto s : a[u]) {
d[u][s] = du;
pq.push_front(make_pair(du, ii(u, s)));
}
f[u] = 1;
}
if(d[b[1]][p[1]] != mod) cout << d[b[1]][p[1]] << '\n';
else cout << "-1\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4884 KB |
Output is correct |
2 |
Correct |
0 ms |
4884 KB |
Output is correct |
3 |
Correct |
0 ms |
4884 KB |
Output is correct |
4 |
Correct |
0 ms |
4884 KB |
Output is correct |
5 |
Correct |
0 ms |
4884 KB |
Output is correct |
6 |
Correct |
0 ms |
4884 KB |
Output is correct |
7 |
Correct |
0 ms |
4884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4884 KB |
Output is correct |
2 |
Correct |
0 ms |
4884 KB |
Output is correct |
3 |
Correct |
0 ms |
4884 KB |
Output is correct |
4 |
Correct |
0 ms |
4884 KB |
Output is correct |
5 |
Correct |
0 ms |
4884 KB |
Output is correct |
6 |
Correct |
0 ms |
4884 KB |
Output is correct |
7 |
Correct |
0 ms |
4884 KB |
Output is correct |
8 |
Correct |
0 ms |
4884 KB |
Output is correct |
9 |
Correct |
0 ms |
4884 KB |
Output is correct |
10 |
Correct |
0 ms |
4884 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
0 ms |
4884 KB |
Output is correct |
13 |
Correct |
0 ms |
4884 KB |
Output is correct |
14 |
Correct |
0 ms |
5148 KB |
Output is correct |
15 |
Correct |
3 ms |
5148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4884 KB |
Output is correct |
2 |
Correct |
0 ms |
4884 KB |
Output is correct |
3 |
Correct |
0 ms |
4884 KB |
Output is correct |
4 |
Correct |
0 ms |
4884 KB |
Output is correct |
5 |
Correct |
0 ms |
4884 KB |
Output is correct |
6 |
Correct |
0 ms |
4884 KB |
Output is correct |
7 |
Correct |
0 ms |
4884 KB |
Output is correct |
8 |
Correct |
0 ms |
4884 KB |
Output is correct |
9 |
Correct |
0 ms |
4884 KB |
Output is correct |
10 |
Correct |
0 ms |
4884 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
0 ms |
4884 KB |
Output is correct |
13 |
Correct |
0 ms |
4884 KB |
Output is correct |
14 |
Correct |
3 ms |
5148 KB |
Output is correct |
15 |
Correct |
0 ms |
5148 KB |
Output is correct |
16 |
Correct |
0 ms |
4884 KB |
Output is correct |
17 |
Correct |
3 ms |
5544 KB |
Output is correct |
18 |
Correct |
0 ms |
4884 KB |
Output is correct |
19 |
Correct |
0 ms |
4884 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
0 ms |
4884 KB |
Output is correct |
22 |
Correct |
0 ms |
4884 KB |
Output is correct |
23 |
Correct |
0 ms |
5144 KB |
Output is correct |
24 |
Correct |
0 ms |
5544 KB |
Output is correct |
25 |
Correct |
3 ms |
5280 KB |
Output is correct |
26 |
Correct |
3 ms |
5280 KB |
Output is correct |
27 |
Correct |
0 ms |
5148 KB |
Output is correct |
28 |
Correct |
6 ms |
5932 KB |
Output is correct |
29 |
Correct |
19 ms |
7524 KB |
Output is correct |
30 |
Correct |
3 ms |
5544 KB |
Output is correct |
31 |
Correct |
9 ms |
6336 KB |
Output is correct |
32 |
Correct |
3 ms |
6072 KB |
Output is correct |
33 |
Correct |
39 ms |
10164 KB |
Output is correct |
34 |
Correct |
36 ms |
10164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4884 KB |
Output is correct |
2 |
Correct |
0 ms |
4884 KB |
Output is correct |
3 |
Correct |
0 ms |
4884 KB |
Output is correct |
4 |
Correct |
0 ms |
4884 KB |
Output is correct |
5 |
Correct |
0 ms |
4884 KB |
Output is correct |
6 |
Correct |
0 ms |
4884 KB |
Output is correct |
7 |
Correct |
0 ms |
4884 KB |
Output is correct |
8 |
Correct |
0 ms |
4884 KB |
Output is correct |
9 |
Correct |
0 ms |
4884 KB |
Output is correct |
10 |
Correct |
0 ms |
4884 KB |
Output is correct |
11 |
Correct |
0 ms |
5016 KB |
Output is correct |
12 |
Correct |
0 ms |
4884 KB |
Output is correct |
13 |
Correct |
0 ms |
4884 KB |
Output is correct |
14 |
Correct |
0 ms |
5148 KB |
Output is correct |
15 |
Correct |
0 ms |
5148 KB |
Output is correct |
16 |
Correct |
0 ms |
4884 KB |
Output is correct |
17 |
Correct |
3 ms |
5544 KB |
Output is correct |
18 |
Correct |
0 ms |
4884 KB |
Output is correct |
19 |
Correct |
0 ms |
4884 KB |
Output is correct |
20 |
Correct |
0 ms |
5016 KB |
Output is correct |
21 |
Correct |
0 ms |
4884 KB |
Output is correct |
22 |
Correct |
0 ms |
4884 KB |
Output is correct |
23 |
Correct |
3 ms |
5144 KB |
Output is correct |
24 |
Correct |
3 ms |
5544 KB |
Output is correct |
25 |
Correct |
3 ms |
5280 KB |
Output is correct |
26 |
Correct |
3 ms |
5280 KB |
Output is correct |
27 |
Correct |
3 ms |
5148 KB |
Output is correct |
28 |
Correct |
6 ms |
5932 KB |
Output is correct |
29 |
Correct |
16 ms |
7524 KB |
Output is correct |
30 |
Correct |
6 ms |
5544 KB |
Output is correct |
31 |
Correct |
13 ms |
6336 KB |
Output is correct |
32 |
Correct |
9 ms |
6072 KB |
Output is correct |
33 |
Correct |
39 ms |
10164 KB |
Output is correct |
34 |
Correct |
39 ms |
10164 KB |
Output is correct |
35 |
Correct |
56 ms |
9372 KB |
Output is correct |
36 |
Correct |
6 ms |
5676 KB |
Output is correct |
37 |
Correct |
99 ms |
12136 KB |
Output is correct |
38 |
Correct |
139 ms |
11748 KB |
Output is correct |
39 |
Correct |
116 ms |
11744 KB |
Output is correct |
40 |
Correct |
106 ms |
11744 KB |
Output is correct |
41 |
Correct |
119 ms |
11740 KB |
Output is correct |
42 |
Correct |
9 ms |
5320 KB |
Output is correct |
43 |
Correct |
6 ms |
5324 KB |
Output is correct |
44 |
Correct |
6 ms |
5200 KB |
Output is correct |
45 |
Correct |
253 ms |
26532 KB |
Output is correct |
46 |
Correct |
236 ms |
26532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4884 KB |
Output is correct |
2 |
Correct |
0 ms |
4884 KB |
Output is correct |
3 |
Correct |
0 ms |
4884 KB |
Output is correct |
4 |
Correct |
0 ms |
4884 KB |
Output is correct |
5 |
Correct |
0 ms |
4884 KB |
Output is correct |
6 |
Correct |
0 ms |
4884 KB |
Output is correct |
7 |
Correct |
0 ms |
4884 KB |
Output is correct |
8 |
Correct |
0 ms |
4884 KB |
Output is correct |
9 |
Correct |
0 ms |
4884 KB |
Output is correct |
10 |
Correct |
0 ms |
4884 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
0 ms |
4884 KB |
Output is correct |
13 |
Correct |
0 ms |
4884 KB |
Output is correct |
14 |
Correct |
3 ms |
5148 KB |
Output is correct |
15 |
Correct |
0 ms |
5148 KB |
Output is correct |
16 |
Correct |
0 ms |
4884 KB |
Output is correct |
17 |
Correct |
3 ms |
5544 KB |
Output is correct |
18 |
Correct |
3 ms |
4884 KB |
Output is correct |
19 |
Correct |
3 ms |
4884 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
0 ms |
4884 KB |
Output is correct |
22 |
Correct |
0 ms |
4884 KB |
Output is correct |
23 |
Correct |
6 ms |
5144 KB |
Output is correct |
24 |
Correct |
3 ms |
5544 KB |
Output is correct |
25 |
Correct |
0 ms |
5280 KB |
Output is correct |
26 |
Correct |
0 ms |
5280 KB |
Output is correct |
27 |
Correct |
3 ms |
5148 KB |
Output is correct |
28 |
Correct |
9 ms |
5932 KB |
Output is correct |
29 |
Correct |
23 ms |
7524 KB |
Output is correct |
30 |
Correct |
3 ms |
5544 KB |
Output is correct |
31 |
Correct |
9 ms |
6336 KB |
Output is correct |
32 |
Correct |
6 ms |
6072 KB |
Output is correct |
33 |
Correct |
43 ms |
10164 KB |
Output is correct |
34 |
Correct |
43 ms |
10164 KB |
Output is correct |
35 |
Correct |
66 ms |
9372 KB |
Output is correct |
36 |
Correct |
6 ms |
5676 KB |
Output is correct |
37 |
Correct |
103 ms |
12136 KB |
Output is correct |
38 |
Correct |
106 ms |
11748 KB |
Output is correct |
39 |
Correct |
103 ms |
11744 KB |
Output is correct |
40 |
Correct |
99 ms |
11744 KB |
Output is correct |
41 |
Correct |
99 ms |
11740 KB |
Output is correct |
42 |
Correct |
3 ms |
5320 KB |
Output is correct |
43 |
Correct |
16 ms |
5324 KB |
Output is correct |
44 |
Correct |
6 ms |
5200 KB |
Output is correct |
45 |
Correct |
253 ms |
26532 KB |
Output is correct |
46 |
Correct |
196 ms |
26532 KB |
Output is correct |
47 |
Correct |
796 ms |
37744 KB |
Output is correct |
48 |
Correct |
9 ms |
6600 KB |
Output is correct |
49 |
Correct |
13 ms |
6468 KB |
Output is correct |
50 |
Correct |
6 ms |
6072 KB |
Output is correct |
51 |
Correct |
276 ms |
19796 KB |
Output is correct |
52 |
Correct |
323 ms |
21112 KB |
Output is correct |
53 |
Correct |
49 ms |
9760 KB |
Output is correct |
54 |
Correct |
6 ms |
5808 KB |
Output is correct |
55 |
Correct |
0 ms |
6336 KB |
Output is correct |
56 |
Correct |
29 ms |
7656 KB |
Output is correct |
57 |
Correct |
273 ms |
24156 KB |
Output is correct |
58 |
Correct |
29 ms |
8476 KB |
Output is correct |
59 |
Correct |
39 ms |
9536 KB |
Output is correct |
60 |
Correct |
66 ms |
10868 KB |
Output is correct |
61 |
Correct |
49 ms |
10124 KB |
Output is correct |
62 |
Correct |
383 ms |
27440 KB |
Output is correct |
63 |
Execution timed out |
1000 ms |
90020 KB |
Execution timed out |
64 |
Halted |
0 ms |
0 KB |
- |