// headers
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// types
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using dbl = double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using piipi = pair<pii, int>;
using pipii = pair<int, pii>;
using plpii = pair<ll, pii>;
using ppii = pair<pii, pii>;
// loops
#define forx(i, x, y) for (int i = (x); i <= (y); ++i)
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define for1(i, n) for (int i = 1; i <= (n); ++i)
#define rofx(i, x, y) for (int i = x; i >= y; --i)
#define rofn(i, n) for (int i = n-1; i >= 0; --i)
#define rof1(i, n) for (int i = n; i >= 1; --i)
#define fora(x, v) for (auto x : v)
// define shortcuts
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define X first
#define Y second
#define MP make_pair
#define MT make_tuple
// functions
template<class T> inline bool hasbit(T x, int i) { return x&(1<<i); }
template<class T> inline T togbit(T x, int i) { return x|(1<<i); }
template<class T> inline T setbit(T x, int i) { return x|(1<<i); }
template<class T> inline T rembit(T x, int i) { return x&~(1<<i); }
template<class T> inline bool setmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool setmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> int compress(vector<T> &v) { sort(all(v)); v.resize(unique(all(v))-v.begin()); return v.size(); }
// read functions
int read_int() { int x; scanf("%d", &x); return x; }
ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
dbl read_dbl() { dbl x; scanf("%lf", &x); return x; }
void _R(int &x) { x = read_int(); }
void _R(ll &x) { x = read_ll(); }
void _R(dbl &x) { x = read_dbl(); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
// print functions
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%" PRId64, x); }
void _W(const ull &x) { printf("%" PRIu64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
// pseudo-random number generator
template<class T, T x1, T x2, T x3, int y1, int y2, int y3>
struct PRNG {
using S = typename make_signed<T>::type;
T s;
PRNG(T _s = 0) : s(_s) {}
T next() {
T z = (s += x1);
z = (z ^ (z >> y1)) * x2;
z = (z ^ (z >> y2)) * x3;
return z ^ (z >> y3);
}
T next(T n) { return next() % n; }
S next(S l, S r) { return l + next(r - l + 1); }
T operator()() { return next(); }
T operator()(T n) { return next(n); }
S operator()(S l, S r) { return next(l, r); }
static T gen(T s) { return PRNG(s)(); }
template<class U>
void shuffle(U first, U last) {
size_t n = last - first;
for (size_t i = 0; i < n; i++) swap(first[i], first[next(i + 1)]);
}
};
PRNG<uint32_t, 0x9E3779B1, 0x85EBCA6B, 0xC2B2AE35, 16, 13, 16> r32;
PRNG<uint64_t, 0x9E3779B97F4A7C15, 0xBF58476D1CE4E5B9, 0x94D049BB133111EB, 30, 27, 31> r64;
// program
int _main();
int main() {
//ios::sync_with_stdio(false), cin.tie(0);
//time_t begin = clock();
int ret = _main();
//time_t end = clock();
//fprintf(stderr, "Program took %.3f seconds to compute\n", ((double)end-begin)/CLOCKS_PER_SEC);
return ret;
}
/************************************************************
CODE STARTS BELOW THIS POINT
************************************************************/
const int N = 30010;
const int SN = 175;
const int M = 30010;
const int INF = 1e9;
int prec[SN][N];
int B[M], P[M];
int dist[N];
bool vis[N];
vector<int> mods[M];
int _main()
{
int n, m; R(n, m);
int sn = sqrt(n);
for1(i, sn) forn(j, n) prec[i][j] = INF;
forn(i, n) dist[i] = INF;
int s, t;
forn(i, m) {
R(B[i], P[i]);
mods[B[i]].pb(P[i]);
if (i == 0) s = B[i];
if (i == 1) t = B[i];
}
priority_queue<pii, vector<pii>, greater<pii>> Q;
dist[s] = 0;
Q.push({dist[s], s});
while (!Q.empty()) {
int u = Q.top().S, d = Q.top().F;
Q.pop();
if (vis[u]) continue;
vis[u] = true;
if (u == t) {
W(d);
return 0;
}
fora(p, mods[u]) {
if (p <= sn)
prec[p][u] = d;
for (int v = u-p, i = 1; v >= 0; v -= p, ++i) {
if (p <= sn) {
if (prec[p][v] <= d+i) break;
prec[p][v] = d+i;
}
if (setmin(dist[v], d+i))
Q.push({dist[v], v});
}
for (int v = u+p, i = 1; v < n; v += p, ++i) {
if (p <= sn) {
if (prec[p][v] <= d+i) break;
prec[p][v] = d+i;
}
if (setmin(dist[v], d+i))
Q.push({dist[v], v});
}
}
}
W(-1);
return 0;
}
Compilation message
skyscraper.cpp: In function 'll read_ll()':
skyscraper.cpp:49:42: warning: format '%ld' expects argument of type 'long int*', but argument 2 has type 'll* {aka long long int*}' [-Wformat=]
ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
~~^
skyscraper.cpp: In function 'ull read_ull()':
skyscraper.cpp:50:45: warning: format '%lu' expects argument of type 'long unsigned int*', but argument 2 has type 'ull* {aka long long unsigned int*}' [-Wformat=]
ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
~~^
skyscraper.cpp: In function 'void _W(const ll&)':
skyscraper.cpp:60:44: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
void _W(const ll &x) { printf("%" PRId64, x); }
~^
skyscraper.cpp: In function 'void _W(const ull&)':
skyscraper.cpp:61:45: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'ull {aka long long unsigned int}' [-Wformat=]
void _W(const ull &x) { printf("%" PRIu64, x); }
~^
skyscraper.cpp: In function 'int read_int()':
skyscraper.cpp:48:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int read_int() { int x; scanf("%d", &x); return x; }
~~~~~^~~~~~~~~~
skyscraper.cpp: In function 'll read_ll()':
skyscraper.cpp:49:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'ull read_ull()':
skyscraper.cpp:50:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'dbl read_dbl()':
skyscraper.cpp:51:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
dbl read_dbl() { dbl x; scanf("%lf", &x); return x; }
~~~~~^~~~~~~~~~~
skyscraper.cpp: In function 'int _main()':
skyscraper.cpp:142:9: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (u == t) {
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
1124 KB |
Output is correct |
3 |
Correct |
2 ms |
1200 KB |
Output is correct |
4 |
Correct |
3 ms |
1200 KB |
Output is correct |
5 |
Correct |
3 ms |
1272 KB |
Output is correct |
6 |
Correct |
3 ms |
1324 KB |
Output is correct |
7 |
Correct |
3 ms |
1324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1324 KB |
Output is correct |
2 |
Correct |
3 ms |
1324 KB |
Output is correct |
3 |
Correct |
3 ms |
1340 KB |
Output is correct |
4 |
Correct |
3 ms |
1340 KB |
Output is correct |
5 |
Correct |
3 ms |
1340 KB |
Output is correct |
6 |
Correct |
4 ms |
1340 KB |
Output is correct |
7 |
Correct |
2 ms |
1340 KB |
Output is correct |
8 |
Correct |
3 ms |
1340 KB |
Output is correct |
9 |
Correct |
2 ms |
1340 KB |
Output is correct |
10 |
Correct |
4 ms |
1340 KB |
Output is correct |
11 |
Correct |
4 ms |
1360 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
3 ms |
1364 KB |
Output is correct |
14 |
Correct |
3 ms |
1364 KB |
Output is correct |
15 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1364 KB |
Output is correct |
2 |
Correct |
3 ms |
1364 KB |
Output is correct |
3 |
Correct |
2 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Correct |
3 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
4 ms |
1364 KB |
Output is correct |
14 |
Correct |
4 ms |
1404 KB |
Output is correct |
15 |
Correct |
3 ms |
1404 KB |
Output is correct |
16 |
Correct |
4 ms |
1404 KB |
Output is correct |
17 |
Correct |
5 ms |
1532 KB |
Output is correct |
18 |
Correct |
4 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1920 KB |
Output is correct |
20 |
Correct |
6 ms |
1984 KB |
Output is correct |
21 |
Correct |
4 ms |
1984 KB |
Output is correct |
22 |
Correct |
4 ms |
2076 KB |
Output is correct |
23 |
Correct |
5 ms |
2124 KB |
Output is correct |
24 |
Correct |
6 ms |
2224 KB |
Output is correct |
25 |
Correct |
4 ms |
2260 KB |
Output is correct |
26 |
Correct |
4 ms |
2260 KB |
Output is correct |
27 |
Correct |
5 ms |
2260 KB |
Output is correct |
28 |
Correct |
5 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2332 KB |
Output is correct |
30 |
Correct |
6 ms |
2332 KB |
Output is correct |
31 |
Correct |
7 ms |
2332 KB |
Output is correct |
32 |
Correct |
5 ms |
2332 KB |
Output is correct |
33 |
Correct |
7 ms |
2352 KB |
Output is correct |
34 |
Correct |
7 ms |
2364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2364 KB |
Output is correct |
2 |
Correct |
2 ms |
2364 KB |
Output is correct |
3 |
Correct |
2 ms |
2364 KB |
Output is correct |
4 |
Correct |
3 ms |
2364 KB |
Output is correct |
5 |
Correct |
3 ms |
2364 KB |
Output is correct |
6 |
Correct |
2 ms |
2364 KB |
Output is correct |
7 |
Correct |
2 ms |
2364 KB |
Output is correct |
8 |
Correct |
2 ms |
2364 KB |
Output is correct |
9 |
Correct |
3 ms |
2364 KB |
Output is correct |
10 |
Correct |
3 ms |
2364 KB |
Output is correct |
11 |
Correct |
4 ms |
2364 KB |
Output is correct |
12 |
Correct |
4 ms |
2364 KB |
Output is correct |
13 |
Correct |
3 ms |
2364 KB |
Output is correct |
14 |
Correct |
3 ms |
2364 KB |
Output is correct |
15 |
Correct |
3 ms |
2364 KB |
Output is correct |
16 |
Correct |
4 ms |
2364 KB |
Output is correct |
17 |
Correct |
3 ms |
2364 KB |
Output is correct |
18 |
Correct |
4 ms |
2364 KB |
Output is correct |
19 |
Correct |
4 ms |
2364 KB |
Output is correct |
20 |
Correct |
5 ms |
2364 KB |
Output is correct |
21 |
Correct |
5 ms |
2364 KB |
Output is correct |
22 |
Correct |
4 ms |
2364 KB |
Output is correct |
23 |
Correct |
4 ms |
2364 KB |
Output is correct |
24 |
Correct |
5 ms |
2364 KB |
Output is correct |
25 |
Correct |
4 ms |
2372 KB |
Output is correct |
26 |
Correct |
4 ms |
2392 KB |
Output is correct |
27 |
Correct |
3 ms |
2408 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
5 ms |
2572 KB |
Output is correct |
30 |
Correct |
6 ms |
2572 KB |
Output is correct |
31 |
Correct |
5 ms |
2572 KB |
Output is correct |
32 |
Correct |
5 ms |
2572 KB |
Output is correct |
33 |
Correct |
7 ms |
2592 KB |
Output is correct |
34 |
Correct |
5 ms |
2732 KB |
Output is correct |
35 |
Correct |
13 ms |
2908 KB |
Output is correct |
36 |
Correct |
5 ms |
2908 KB |
Output is correct |
37 |
Correct |
10 ms |
3260 KB |
Output is correct |
38 |
Correct |
14 ms |
3676 KB |
Output is correct |
39 |
Correct |
14 ms |
3812 KB |
Output is correct |
40 |
Correct |
13 ms |
4096 KB |
Output is correct |
41 |
Correct |
13 ms |
4512 KB |
Output is correct |
42 |
Correct |
10 ms |
4644 KB |
Output is correct |
43 |
Correct |
18 ms |
4812 KB |
Output is correct |
44 |
Correct |
11 ms |
5020 KB |
Output is correct |
45 |
Correct |
14 ms |
5356 KB |
Output is correct |
46 |
Correct |
19 ms |
5568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5568 KB |
Output is correct |
2 |
Correct |
3 ms |
5568 KB |
Output is correct |
3 |
Correct |
3 ms |
5568 KB |
Output is correct |
4 |
Correct |
3 ms |
5568 KB |
Output is correct |
5 |
Correct |
3 ms |
5568 KB |
Output is correct |
6 |
Correct |
3 ms |
5568 KB |
Output is correct |
7 |
Correct |
4 ms |
5568 KB |
Output is correct |
8 |
Correct |
3 ms |
5568 KB |
Output is correct |
9 |
Correct |
3 ms |
5568 KB |
Output is correct |
10 |
Correct |
3 ms |
5568 KB |
Output is correct |
11 |
Correct |
3 ms |
5568 KB |
Output is correct |
12 |
Correct |
3 ms |
5568 KB |
Output is correct |
13 |
Correct |
3 ms |
5568 KB |
Output is correct |
14 |
Correct |
3 ms |
5568 KB |
Output is correct |
15 |
Correct |
3 ms |
5568 KB |
Output is correct |
16 |
Correct |
3 ms |
5568 KB |
Output is correct |
17 |
Correct |
3 ms |
5568 KB |
Output is correct |
18 |
Correct |
3 ms |
5568 KB |
Output is correct |
19 |
Correct |
3 ms |
5568 KB |
Output is correct |
20 |
Correct |
4 ms |
5568 KB |
Output is correct |
21 |
Correct |
4 ms |
5568 KB |
Output is correct |
22 |
Correct |
3 ms |
5568 KB |
Output is correct |
23 |
Correct |
5 ms |
5568 KB |
Output is correct |
24 |
Correct |
6 ms |
5568 KB |
Output is correct |
25 |
Correct |
5 ms |
5568 KB |
Output is correct |
26 |
Correct |
5 ms |
5568 KB |
Output is correct |
27 |
Correct |
5 ms |
5568 KB |
Output is correct |
28 |
Correct |
5 ms |
5568 KB |
Output is correct |
29 |
Correct |
7 ms |
5568 KB |
Output is correct |
30 |
Correct |
4 ms |
5568 KB |
Output is correct |
31 |
Correct |
5 ms |
5568 KB |
Output is correct |
32 |
Correct |
5 ms |
5568 KB |
Output is correct |
33 |
Correct |
9 ms |
5568 KB |
Output is correct |
34 |
Correct |
7 ms |
5568 KB |
Output is correct |
35 |
Correct |
14 ms |
5672 KB |
Output is correct |
36 |
Correct |
7 ms |
5672 KB |
Output is correct |
37 |
Correct |
19 ms |
6112 KB |
Output is correct |
38 |
Correct |
27 ms |
6404 KB |
Output is correct |
39 |
Correct |
15 ms |
6540 KB |
Output is correct |
40 |
Correct |
17 ms |
6804 KB |
Output is correct |
41 |
Correct |
24 ms |
7200 KB |
Output is correct |
42 |
Correct |
14 ms |
7420 KB |
Output is correct |
43 |
Correct |
16 ms |
7644 KB |
Output is correct |
44 |
Correct |
13 ms |
7724 KB |
Output is correct |
45 |
Correct |
15 ms |
8100 KB |
Output is correct |
46 |
Correct |
13 ms |
8348 KB |
Output is correct |
47 |
Correct |
21 ms |
14420 KB |
Output is correct |
48 |
Correct |
27 ms |
22788 KB |
Output is correct |
49 |
Correct |
23 ms |
25004 KB |
Output is correct |
50 |
Correct |
30 ms |
27540 KB |
Output is correct |
51 |
Correct |
50 ms |
30772 KB |
Output is correct |
52 |
Correct |
47 ms |
30772 KB |
Output is correct |
53 |
Correct |
38 ms |
30772 KB |
Output is correct |
54 |
Correct |
19 ms |
30772 KB |
Output is correct |
55 |
Correct |
22 ms |
30772 KB |
Output is correct |
56 |
Correct |
44 ms |
31280 KB |
Output is correct |
57 |
Correct |
23 ms |
31280 KB |
Output is correct |
58 |
Correct |
31 ms |
31280 KB |
Output is correct |
59 |
Correct |
34 ms |
31280 KB |
Output is correct |
60 |
Correct |
30 ms |
31280 KB |
Output is correct |
61 |
Correct |
28 ms |
31280 KB |
Output is correct |
62 |
Correct |
49 ms |
32088 KB |
Output is correct |
63 |
Correct |
56 ms |
32736 KB |
Output is correct |
64 |
Correct |
58 ms |
34060 KB |
Output is correct |
65 |
Correct |
77 ms |
34188 KB |
Output is correct |
66 |
Correct |
136 ms |
34428 KB |
Output is correct |
67 |
Correct |
128 ms |
34604 KB |
Output is correct |