#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
/*ifstream fin("test_input.txt");
#define cin fin*/
int t, n, m, k, b[30010], p[30010];
int crd[30010];
int d[30010];
bool v[30010];
vector<pii> g[30010];
bool dog[30010];
void dijkstra(){
multiset<pii> ms;
ms.insert({d[crd[1] ], crd[1]});
while(!ms.empty()){
auto f=*ms.begin(); ms.erase(ms.begin());
int nod=f.sc;
v[nod]=true;
//cout<<nod<<" "<<d[nod]<<"y\n";
if(nod==crd[2]){return;}
for(auto pr:g[nod]){
int u=pr.ft, c=pr.sc;
if(v[u]){continue;}
//cout<<u<<" "<<c<<"x\n";
auto it=ms.find({d[u], u});
if(it!=ms.end()){
if( (d[nod]+c)<d[u] ){
ms.erase(it);
d[u]=d[nod]+c;
ms.insert({d[u], u});}
}
else{
d[u]=d[nod]+c;
ms.insert({d[u], u} );
}
}
}
return;
}
map<pii, int> up, down;
pii pr[30010];
void add_chain1(int b, int p){
int lim=down[{b%p, p}];
for(int i=b-p; (i>=1) && ( (i>=lim) ) ; i-=p){
if(dog[i]){g[b].pb({i, abs(i-b)/p }); }
}
down[{b%p, p}]=max(lim, b);
}
void add_chain2(int b, int p){
if(up[{b%p, p}]==0){ up[{b%p, p }]=n+1; }
int lim=up[{b%p, p}];
for(int i=b+p; (i<=n) && ( (i<=lim) ) ; i+=p){
if(dog[i]){g[b].pb({i, abs(i-b)/p }); }
}
up[{b%p, p} ]=min( lim, b);
}
int32_t main(){
INIT
cin>>n>>m;
for(int i=1; i<=n; i++){d[i]=-1; }
for(int i=1; i<=m; i++){
cin>>b[i]>>p[i]; b[i]++;
crd[i ]=b[i];
dog[b[i] ]=true;
pr[i]={b[i], p[i]};
}
sort(pr+1, pr+1+m);
for(int i=1; i<=m; i++){
add_chain1(pr[i].ft, pr[i].sc);
}
for(int i=m; i>=1; i--){
add_chain2(pr[i].ft, pr[i].sc);
}
d[crd[1] ]=0;
dijkstra();
cout<<d[crd[2] ];
/*for(int i=1; i<=n; i++){
for(auto u:g[i]){
cout<<u.ft<<" ";
}
cout<<"\n";
}*/
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
2 ms |
1132 KB |
Output is correct |
13 |
Correct |
1 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
2 ms |
1132 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
2 ms |
1132 KB |
Output is correct |
13 |
Correct |
1 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
1388 KB |
Output is correct |
16 |
Correct |
2 ms |
1260 KB |
Output is correct |
17 |
Correct |
4 ms |
1516 KB |
Output is correct |
18 |
Correct |
2 ms |
1260 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1132 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
4 ms |
1644 KB |
Output is correct |
25 |
Correct |
3 ms |
1516 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
2 ms |
1132 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
2 ms |
1132 KB |
Output is correct |
30 |
Correct |
2 ms |
1132 KB |
Output is correct |
31 |
Correct |
2 ms |
1132 KB |
Output is correct |
32 |
Correct |
1 ms |
1132 KB |
Output is correct |
33 |
Correct |
3 ms |
1388 KB |
Output is correct |
34 |
Correct |
3 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
2 ms |
1132 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1132 KB |
Output is correct |
13 |
Correct |
2 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
1388 KB |
Output is correct |
16 |
Correct |
2 ms |
1260 KB |
Output is correct |
17 |
Correct |
4 ms |
1516 KB |
Output is correct |
18 |
Correct |
2 ms |
1260 KB |
Output is correct |
19 |
Correct |
2 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1132 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
4 ms |
1516 KB |
Output is correct |
25 |
Correct |
3 ms |
1516 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
2 ms |
1132 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
2 ms |
1260 KB |
Output is correct |
30 |
Correct |
1 ms |
1132 KB |
Output is correct |
31 |
Correct |
1 ms |
1132 KB |
Output is correct |
32 |
Correct |
1 ms |
1132 KB |
Output is correct |
33 |
Correct |
3 ms |
1388 KB |
Output is correct |
34 |
Correct |
3 ms |
1388 KB |
Output is correct |
35 |
Correct |
38 ms |
5740 KB |
Output is correct |
36 |
Correct |
7 ms |
2028 KB |
Output is correct |
37 |
Correct |
40 ms |
5868 KB |
Output is correct |
38 |
Correct |
47 ms |
7148 KB |
Output is correct |
39 |
Correct |
43 ms |
7020 KB |
Output is correct |
40 |
Correct |
43 ms |
7020 KB |
Output is correct |
41 |
Correct |
45 ms |
7148 KB |
Output is correct |
42 |
Correct |
8 ms |
1644 KB |
Output is correct |
43 |
Correct |
11 ms |
1644 KB |
Output is correct |
44 |
Correct |
7 ms |
1772 KB |
Output is correct |
45 |
Correct |
31 ms |
5740 KB |
Output is correct |
46 |
Correct |
32 ms |
5740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1132 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
2 ms |
1132 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1132 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
1536 KB |
Output is correct |
16 |
Correct |
2 ms |
1260 KB |
Output is correct |
17 |
Correct |
4 ms |
1516 KB |
Output is correct |
18 |
Correct |
2 ms |
1260 KB |
Output is correct |
19 |
Correct |
2 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1132 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
4 ms |
1516 KB |
Output is correct |
25 |
Correct |
3 ms |
1516 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
2 ms |
1132 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
2 ms |
1132 KB |
Output is correct |
30 |
Correct |
1 ms |
1132 KB |
Output is correct |
31 |
Correct |
2 ms |
1132 KB |
Output is correct |
32 |
Correct |
1 ms |
1132 KB |
Output is correct |
33 |
Correct |
3 ms |
1388 KB |
Output is correct |
34 |
Correct |
3 ms |
1388 KB |
Output is correct |
35 |
Correct |
37 ms |
5740 KB |
Output is correct |
36 |
Correct |
7 ms |
2028 KB |
Output is correct |
37 |
Correct |
39 ms |
5868 KB |
Output is correct |
38 |
Correct |
47 ms |
7148 KB |
Output is correct |
39 |
Correct |
43 ms |
7020 KB |
Output is correct |
40 |
Correct |
43 ms |
7020 KB |
Output is correct |
41 |
Correct |
45 ms |
7168 KB |
Output is correct |
42 |
Correct |
10 ms |
1644 KB |
Output is correct |
43 |
Correct |
9 ms |
1644 KB |
Output is correct |
44 |
Correct |
7 ms |
1772 KB |
Output is correct |
45 |
Correct |
30 ms |
5740 KB |
Output is correct |
46 |
Correct |
33 ms |
5760 KB |
Output is correct |
47 |
Correct |
96 ms |
12524 KB |
Output is correct |
48 |
Correct |
34 ms |
5996 KB |
Output is correct |
49 |
Correct |
30 ms |
5100 KB |
Output is correct |
50 |
Correct |
21 ms |
4076 KB |
Output is correct |
51 |
Correct |
77 ms |
8556 KB |
Output is correct |
52 |
Correct |
79 ms |
8684 KB |
Output is correct |
53 |
Correct |
40 ms |
6504 KB |
Output is correct |
54 |
Correct |
1 ms |
1132 KB |
Output is correct |
55 |
Correct |
1 ms |
1132 KB |
Output is correct |
56 |
Correct |
12 ms |
2796 KB |
Output is correct |
57 |
Correct |
7 ms |
2176 KB |
Output is correct |
58 |
Correct |
7 ms |
1772 KB |
Output is correct |
59 |
Correct |
8 ms |
1772 KB |
Output is correct |
60 |
Correct |
10 ms |
1772 KB |
Output is correct |
61 |
Correct |
12 ms |
2284 KB |
Output is correct |
62 |
Correct |
46 ms |
10604 KB |
Output is correct |
63 |
Correct |
32 ms |
5740 KB |
Output is correct |
64 |
Correct |
32 ms |
5740 KB |
Output is correct |
65 |
Correct |
33 ms |
5740 KB |
Output is correct |
66 |
Correct |
38 ms |
5868 KB |
Output is correct |
67 |
Correct |
36 ms |
5868 KB |
Output is correct |