// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
int main() {
ll N,M; cin >> N >> M;
//ll K = 5*floor(sqrt(N+0.001)); //MODIFY
ll K = 1;
ll B[M],P[M];
ll val[N];
ll val2[K][N];
bool found[N];
bool found2[K][N];
vector<pii> doges[N];
for (ll i=0;i<M;i++) {
//val[i]=1e18;
cin >> B[i] >> P[i];
doges[B[i]].push_back({i,P[i]});
}
for (ll i=0;i<N;i++) {
for (ll k=0;k<K;k++) {
val2[k][i]=1e18;
found2[k][i]=0;
}
val[i]=1e18;
found[i]=0;
}
val[B[0]]=0;
priority_queue<pair<ll,ll>> PQ; //-val, position
priority_queue<array<ll,3>> PQ2; //-val, position, k
PQ.push({0,B[0]});
/*while(1) {
ll mv = 1e17; ll ctr = -1;
for (ll i=0;i<N;i++) {
if (val[i]<mv && !found[i]) {
mv = val[i]; ctr = i;
}
}
if (ctr==-1) {
cout << "-1"; exit(0);
} else if (ctr==B[1]) {
cout << val[B[1]]; exit(0);
} else {
for (pii x0: doges[ctr]) {
ll i = x0.first;
for (ll t=(-(B[i]/P[i]+2));t<(((N-B[i])/P[i])+2);t++) {
if ((B[i]+t*P[i])>=0 && (B[i]+t*P[i])<N) {
val[(B[i]+t*P[i])]=min(val[(B[i]+t*P[i])],mv+(ll)abs(t));
}
}
}
}
found[ctr]=1;
}*/
while(1) {
ll mv = 1e17; ll ctr = -1;
ll lpop = -1;
while (!PQ.empty()) {
pii q0 = PQ.top(); ll v = -q0.first; ll x = q0.second;
if (!found[x]) {
ctr = x; mv = v; lpop = 0;
break;
} else {
PQ.pop();
}
}
while (!PQ2.empty()) {
array<ll,3> a0 = PQ2.top(); ll v = -a0[0]; ll x = a0[1]; ll k = a0[2];
if (v<=mv) {
break;
}
if (!found2[k][x] && !found[x]) {
if (val2[k][x]<mv) {
mv = val2[k][x]; ctr = x; lpop = 1;
}
break;
} else if (!found2[k][x]) {
found2[k][x]=1;
if (((x-k)>=0)&&(val2[k][x-k]>(1+val2[k][x]))) {
val2[k][x-k]=1+val2[k][x];
PQ2.push({-val2[k][x-k],(x-k),k});
}
if (((x+k)<N)&&(val2[k][x+k]>(1+val2[k][x]))) {
val2[k][x+k]=1+val2[k][x];
PQ2.push({-val2[k][x+k],(x+k),k});
}
PQ2.pop();
} else {
PQ2.pop();
}
}
/*for (ll k=0;k<K;k++) {
while (!v2q[k].empty()) {
ll x = v2q[k].top();
if (!found2[k][x] && !found[x]) {
if (val2[k][x]<mv) {
mv = val2[k][x]; ctr = x; lpop = k;
}
break;
} else if (!found2[k][x]) {
found2[k][x]=1;
if (((x-k)>=0)&&(val2[k][x-k]>(1+val2[k][x]))) {
val2[k][x-k]=1+val2[k][x];
v2q[k].push(x-k);
}
if (((x+k)<N)&&(val2[k][x+k]>(1+val2[k][x]))) {
val2[k][x+k]=1+val2[k][x];
v2q[k].push(x+k);
}
}
}
}*/
if (lpop==-1) {
cout << "-1"; exit(0);
} else if (ctr==B[1]) {
//cout << val[B[1]]; exit(0);
ll ans = val[B[1]];
for (ll k=0;k<K;k++) {
ans = min(ans,val2[k][B[1]]);
}
cout << ans; exit(0);
} else {
for (pii x0: doges[ctr]) {
ll i = x0.first;
if (P[i]>=K) {
for (ll t=(-(B[i]/P[i]+2));t<(((N-B[i])/P[i])+2);t++) {
if ((B[i]+t*P[i])>=0 && (B[i]+t*P[i])<N) {
if ((mv+(ll)abs(t))<val[(B[i]+t*P[i])]) {
val[(B[i]+t*P[i])]=mv+(ll)abs(t);
PQ.push({-val[(B[i]+t*P[i])],(B[i]+t*P[i])});
}
}
}
} else {
val2[P[i]][B[i]]=mv;
PQ2.push({-mv,B[i],P[i]});
}
}
}
found[ctr]=1;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
444 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
400 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
444 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
856 KB |
Output is correct |
35 |
Correct |
7 ms |
1880 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
6 ms |
1680 KB |
Output is correct |
38 |
Correct |
9 ms |
2276 KB |
Output is correct |
39 |
Correct |
8 ms |
1884 KB |
Output is correct |
40 |
Correct |
8 ms |
2140 KB |
Output is correct |
41 |
Correct |
9 ms |
2140 KB |
Output is correct |
42 |
Correct |
7 ms |
1752 KB |
Output is correct |
43 |
Correct |
6 ms |
1752 KB |
Output is correct |
44 |
Correct |
121 ms |
1828 KB |
Output is correct |
45 |
Correct |
8 ms |
2140 KB |
Output is correct |
46 |
Correct |
8 ms |
2044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
8 ms |
592 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
616 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
372 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
860 KB |
Output is correct |
35 |
Correct |
8 ms |
2048 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
1520 KB |
Output is correct |
38 |
Correct |
9 ms |
2140 KB |
Output is correct |
39 |
Correct |
8 ms |
1884 KB |
Output is correct |
40 |
Correct |
10 ms |
2256 KB |
Output is correct |
41 |
Correct |
9 ms |
2140 KB |
Output is correct |
42 |
Correct |
7 ms |
1608 KB |
Output is correct |
43 |
Correct |
7 ms |
1836 KB |
Output is correct |
44 |
Correct |
121 ms |
1752 KB |
Output is correct |
45 |
Correct |
8 ms |
2144 KB |
Output is correct |
46 |
Correct |
8 ms |
2140 KB |
Output is correct |
47 |
Correct |
9 ms |
3260 KB |
Output is correct |
48 |
Correct |
7 ms |
2396 KB |
Output is correct |
49 |
Correct |
7 ms |
2472 KB |
Output is correct |
50 |
Correct |
5 ms |
2396 KB |
Output is correct |
51 |
Correct |
16 ms |
5436 KB |
Output is correct |
52 |
Correct |
21 ms |
4308 KB |
Output is correct |
53 |
Correct |
11 ms |
3676 KB |
Output is correct |
54 |
Correct |
2 ms |
2008 KB |
Output is correct |
55 |
Correct |
2 ms |
2004 KB |
Output is correct |
56 |
Execution timed out |
1058 ms |
3800 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |