// #define TASK "sweets"
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse4.2,avx2")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unroll-all-loops")
#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_cxx;`
// using namespace __gnu_pbds;
using namespace std;
// template<class K>
// using ordered_set = tree<K, null_type, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
// template<class K, class T>
// using ordered_map = tree<K, T, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
// mt19937 rnd((int)chrono::high_resolution_clock::now().time_since_epoch().count());
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define pb push_back
#define pii pair<int, int>
#define len(x) (long long)x.size()
// #define int long long
typedef long long ll;
typedef long double ld;
const long long INF = (int)numeric_limits<int>::max() >> 1;
const long long MAXN = (long long)1e5 + 10;
const long long MOD = (long long)1e9 + 7;
const long double EPS = (long double)1e-12;
// char _buf_[(int)6e6];
// size_t _p_ = 0;
// inline void *operator new(size_t _n_) {
// _p_ += _n_;
// return _buf_ + _p_ - _n_;
// }
// inline void operator delete(void*) {};
ll power(ll x, ll n, ll mod = 1e9 + 7) {
if (n == 0) return 1ll;
if (n & 1ll) return power(x, n - 1ll, mod) * x % mod;
ll tmp = power(x, n >> 1ll, mod);
return (tmp * tmp) % mod;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd (b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template<typename A, typename B>
bool cmax(A &a, const B &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<typename A, typename B>
bool cmin(A &a, const B &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
vector<int> b, p;
vector<vector<pii>> g;
int n, s, f, m;
int solve() {
vector<int> dist(n, INF), used(n, 0);
dist[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> cur;
cur.push({0, s});
while (!cur.empty()) {
pii v = cur.top();
cur.pop();
if (used[v.S]) continue;
used[v.S] = 1;
for (auto u: g[v.S]) {
int cost = u.S;
if (cmin(dist[u.F], dist[v.S] + cost))
cur.push({dist[u.F], u.F});
}
}
return dist[f];
}
signed main() {
#ifndef LOCAL
#ifdef TASK
freopen(TASK".in", "r", stdin);
freopen(TASK".out", "w", stdout);
#endif
#endif
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// == SOLUTION == //
cin >> n >> m;
b.resize(m); p.resize(m);
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
}
s = b[0]; f = b[1];
set<int> Power[n];
for (int i = 0; i < m; i++) {
Power[b[i]].insert(p[i]);
}
vector<pii> players;
for (int i = 0; i < m; i++) {
players.pb({b[i], p[i]});
}
sort(all(players));
players.resize(unique(all(players)) - players.begin());
p.clear(); b.clear();
p.resize(len(players)); b.resize(len(players));
for (int i = 0; i < len(players); i++) {
b[i] = players[i].F;
p[i] = players[i].S;
}
m = len(players);
g.resize(n);
for (int i = 0; i < m; i++) {
int x = b[i];
int dx = p[i];
int cnt = 0;
int cur = x + dx;
while (cur < n) {
cnt++;
g[x].pb({cur, cnt});
if (Power[cur].find(dx) != Power[cur].end()) break;
cur += dx;
}
cnt = 0;
cur = x - dx;
while(cur >= 0) {
cnt++;
g[x].pb({cur, cnt});
if (Power[cur].find(dx) != Power[cur].end()) break;
cur -= dx;
}
}
int ans = solve();
if (ans == INF) {
cout << -1 << "\n";
return 0;
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
13 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
608 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
556 KB |
Output is correct |
22 |
Correct |
3 ms |
600 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
1020 KB |
Output is correct |
32 |
Correct |
3 ms |
768 KB |
Output is correct |
33 |
Correct |
7 ms |
2176 KB |
Output is correct |
34 |
Correct |
7 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
608 KB |
Output is correct |
20 |
Correct |
3 ms |
768 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
896 KB |
Output is correct |
31 |
Correct |
4 ms |
1024 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
2176 KB |
Output is correct |
34 |
Correct |
8 ms |
2176 KB |
Output is correct |
35 |
Correct |
21 ms |
3068 KB |
Output is correct |
36 |
Correct |
6 ms |
896 KB |
Output is correct |
37 |
Correct |
24 ms |
3836 KB |
Output is correct |
38 |
Correct |
37 ms |
4144 KB |
Output is correct |
39 |
Correct |
32 ms |
4212 KB |
Output is correct |
40 |
Correct |
33 ms |
4204 KB |
Output is correct |
41 |
Correct |
34 ms |
4116 KB |
Output is correct |
42 |
Correct |
8 ms |
1280 KB |
Output is correct |
43 |
Correct |
8 ms |
1280 KB |
Output is correct |
44 |
Correct |
8 ms |
1280 KB |
Output is correct |
45 |
Correct |
41 ms |
7144 KB |
Output is correct |
46 |
Correct |
31 ms |
7156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
432 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
768 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
7 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
896 KB |
Output is correct |
31 |
Correct |
4 ms |
1024 KB |
Output is correct |
32 |
Correct |
4 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
2176 KB |
Output is correct |
34 |
Correct |
8 ms |
2176 KB |
Output is correct |
35 |
Correct |
31 ms |
3196 KB |
Output is correct |
36 |
Correct |
6 ms |
896 KB |
Output is correct |
37 |
Correct |
25 ms |
3836 KB |
Output is correct |
38 |
Correct |
33 ms |
4212 KB |
Output is correct |
39 |
Correct |
33 ms |
4212 KB |
Output is correct |
40 |
Correct |
32 ms |
4212 KB |
Output is correct |
41 |
Correct |
30 ms |
4212 KB |
Output is correct |
42 |
Correct |
11 ms |
1280 KB |
Output is correct |
43 |
Correct |
8 ms |
1280 KB |
Output is correct |
44 |
Correct |
9 ms |
1408 KB |
Output is correct |
45 |
Correct |
35 ms |
7156 KB |
Output is correct |
46 |
Correct |
33 ms |
7156 KB |
Output is correct |
47 |
Correct |
61 ms |
12276 KB |
Output is correct |
48 |
Correct |
24 ms |
6140 KB |
Output is correct |
49 |
Correct |
23 ms |
5364 KB |
Output is correct |
50 |
Correct |
16 ms |
5120 KB |
Output is correct |
51 |
Correct |
58 ms |
8836 KB |
Output is correct |
52 |
Correct |
60 ms |
9336 KB |
Output is correct |
53 |
Correct |
31 ms |
6404 KB |
Output is correct |
54 |
Correct |
6 ms |
3200 KB |
Output is correct |
55 |
Correct |
9 ms |
3580 KB |
Output is correct |
56 |
Correct |
27 ms |
5756 KB |
Output is correct |
57 |
Correct |
50 ms |
11900 KB |
Output is correct |
58 |
Correct |
14 ms |
4344 KB |
Output is correct |
59 |
Correct |
15 ms |
4860 KB |
Output is correct |
60 |
Correct |
17 ms |
5372 KB |
Output is correct |
61 |
Correct |
19 ms |
4864 KB |
Output is correct |
62 |
Correct |
40 ms |
10100 KB |
Output is correct |
63 |
Correct |
93 ms |
26880 KB |
Output is correct |
64 |
Correct |
86 ms |
30444 KB |
Output is correct |
65 |
Correct |
148 ms |
39832 KB |
Output is correct |
66 |
Correct |
257 ms |
66420 KB |
Output is correct |
67 |
Correct |
267 ms |
66368 KB |
Output is correct |