#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 3e4 + 7;
const int M = 3e4 + 7;
const int Q = 175;
int n, m, a[M], b[M];
vector<int> v[N];
int col0 = 0, col1 = 1;
int d[N][Q];
void bfs(int s, int x, int t) {
int S = sqrt(n);
FOR(i, 0, n)
FOR(j, 0, S + 1) d[i][j] = 1e9;
priority_queue<piii, vector<piii>, greater<piii>> pq;
if (x <= S) {
d[s][x] = 0;
pq.push({0, {s, x}});
}
else {
d[s][S + 1] = 0;
pq.push({0, {s, S + 1}});
int u = x;
int cnt = 1, w = 0;
for (int t = s - u; t >= 0; t-=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
cnt = 1;
for (int t = s + u; t < n; t+=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
}
while (!pq.empty()) {
piii tmp = pq.top();
pq.pop();
int w = tmp.X, s = tmp.Y.X, x = tmp.Y.Y;
// cout<<s<<" "<<x<<" "<<w<<"\n";
if (w != d[s][x]) continue;
if (x <= S) {
if (s - x >= 0 && minimize(d[s - x][x], w + 1)) pq.push({w + 1, {s - x, x}});
if (s + x < n && minimize(d[s + x][x], w + 1)) pq.push({w + 1, {s + x, x}});
}
for (auto u : v[s])
if (u <= S) {
if (minimize(d[s][u], w)) pq.push({w, {s, u}});
}
else {
int cnt = 1;
for (int t = s - u; t >= 0; t-=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
cnt = 1;
for (int t = s + u; t < n; t+=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
}
v[s].clear();
}
int res = 1e9;
FOR(j, 0, S + 1) minimize(res, d[t][j]);
cout<<(res == 1e9 ? -1 : res);
}
int main() {
// freopen("APIO15_SKYSCRAPER.inp", "r", stdin);
// freopen("APIO15_SKYSCRAPER.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
REP(i, 0, m) {
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
bfs(a[0], b[0], a[1]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2992 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
4 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
3060 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
3104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
3164 KB |
Output is correct |
11 |
Correct |
2 ms |
3416 KB |
Output is correct |
12 |
Correct |
2 ms |
3160 KB |
Output is correct |
13 |
Correct |
1 ms |
3160 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
3160 KB |
Output is correct |
11 |
Correct |
1 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5220 KB |
Output is correct |
22 |
Correct |
1 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5132 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
6 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5404 KB |
Output is correct |
33 |
Correct |
10 ms |
5512 KB |
Output is correct |
34 |
Correct |
10 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
3160 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
3160 KB |
Output is correct |
11 |
Correct |
1 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
3160 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
1 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5188 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
6 ms |
5352 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5364 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
10 ms |
5468 KB |
Output is correct |
34 |
Correct |
10 ms |
5468 KB |
Output is correct |
35 |
Correct |
8 ms |
5724 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
10 ms |
5724 KB |
Output is correct |
38 |
Correct |
11 ms |
5656 KB |
Output is correct |
39 |
Correct |
12 ms |
5724 KB |
Output is correct |
40 |
Correct |
11 ms |
5860 KB |
Output is correct |
41 |
Correct |
11 ms |
5720 KB |
Output is correct |
42 |
Correct |
5 ms |
5724 KB |
Output is correct |
43 |
Correct |
5 ms |
5724 KB |
Output is correct |
44 |
Correct |
4 ms |
5724 KB |
Output is correct |
45 |
Correct |
13 ms |
5672 KB |
Output is correct |
46 |
Correct |
13 ms |
5924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
3160 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
3160 KB |
Output is correct |
11 |
Correct |
1 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5160 KB |
Output is correct |
19 |
Correct |
1 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5300 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5300 KB |
Output is correct |
28 |
Correct |
2 ms |
5396 KB |
Output is correct |
29 |
Correct |
8 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
10 ms |
5516 KB |
Output is correct |
34 |
Correct |
10 ms |
5720 KB |
Output is correct |
35 |
Correct |
9 ms |
5816 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
10 ms |
5744 KB |
Output is correct |
38 |
Correct |
11 ms |
5724 KB |
Output is correct |
39 |
Correct |
13 ms |
5980 KB |
Output is correct |
40 |
Correct |
11 ms |
5720 KB |
Output is correct |
41 |
Correct |
11 ms |
5724 KB |
Output is correct |
42 |
Correct |
5 ms |
5724 KB |
Output is correct |
43 |
Correct |
5 ms |
5724 KB |
Output is correct |
44 |
Correct |
4 ms |
5724 KB |
Output is correct |
45 |
Correct |
13 ms |
5724 KB |
Output is correct |
46 |
Correct |
13 ms |
5724 KB |
Output is correct |
47 |
Correct |
48 ms |
14420 KB |
Output is correct |
48 |
Correct |
12 ms |
20056 KB |
Output is correct |
49 |
Correct |
9 ms |
22364 KB |
Output is correct |
50 |
Correct |
11 ms |
22108 KB |
Output is correct |
51 |
Correct |
35 ms |
23068 KB |
Output is correct |
52 |
Correct |
44 ms |
23068 KB |
Output is correct |
53 |
Correct |
13 ms |
22548 KB |
Output is correct |
54 |
Correct |
5 ms |
21720 KB |
Output is correct |
55 |
Correct |
5 ms |
21716 KB |
Output is correct |
56 |
Correct |
13 ms |
22876 KB |
Output is correct |
57 |
Correct |
66 ms |
21820 KB |
Output is correct |
58 |
Correct |
12 ms |
22184 KB |
Output is correct |
59 |
Correct |
12 ms |
22108 KB |
Output is correct |
60 |
Correct |
21 ms |
22108 KB |
Output is correct |
61 |
Correct |
14 ms |
22192 KB |
Output is correct |
62 |
Correct |
38 ms |
22876 KB |
Output is correct |
63 |
Correct |
40 ms |
23796 KB |
Output is correct |
64 |
Correct |
44 ms |
24024 KB |
Output is correct |
65 |
Correct |
234 ms |
24008 KB |
Output is correct |
66 |
Correct |
673 ms |
26404 KB |
Output is correct |
67 |
Correct |
668 ms |
25356 KB |
Output is correct |