#include "books.h"
#include <bits/stdc++.h>
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define m_p make_pair
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
//#define pragma optimize("unroll-loops")
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=1e6+3;
struct dsu{
int p[N],sz[N];
void make(int v){
p[v]=v;
sz[v]=1;
}
void init(){
for(int i=0;i<N;i++)
make(i);
}
int get(int v){
return p[v]=(p[v]==v?v:get(p[v]));
}
bool mg(int a,int b){
a=get(a),b=get(b);
if(a==b) return 0;
if(sz[a]>sz[b]) swap(a,b);
p[a]=b;sz[b]+=sz[a];
return 1;
}
}ds;
long long minimum_walk(std::vector<int> p, int s) {
ds.init();
int n=sz(p);
vec<int>used(n,0);
vec<int>rgt(n,0);
ll ans=0;
vec<pii> arr;
for(int i=0;i<n;i++){
if(used[i]) continue;
int j=i;
vec<int>vc;
while(!used[j]){
used[j]=1;
vc.pb(j);
ans+=abs(p[j]-j);
j=p[j];
}
int mx=*max_element(all(vc));
for(auto &z : vc){
rgt[z]=mx;
}
if(sz(vc)==1) continue;
arr.pb({i,mx});
// cout<<"HEY "<<endl;
//// for(auto &z : vc)
// cout<<z<<' ';
// cout<<endl;
}
vec<pii> narr;
for(auto &z : arr){
if(!sz(narr) || narr.back().s<z.f) narr.pb(z);
else umax(narr.back().s,z.s);
}
swap(arr,narr);
ds.init();
vec<array<int,3>> e;
for(int i=0;i<sz(arr);i++){
e.pb({2*min(abs(s-arr[i].f),abs(s-arr[i].s)),i,n});
}
for(int i=1;i<sz(arr);i++){
e.pb({2*abs(arr[i].f-arr[i-1].s),i,i-1});
}
sort(all(e));
for(auto &z : e){
if(ds.mg(z[1],z[2]))
ans+=z[0];
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8008 KB |
Output is correct |
2 |
Correct |
5 ms |
8024 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
5 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
6 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8112 KB |
Output is correct |
8 |
Correct |
5 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
8 ms |
8100 KB |
Output is correct |
11 |
Correct |
5 ms |
8012 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
5 ms |
8012 KB |
Output is correct |
14 |
Correct |
5 ms |
8012 KB |
Output is correct |
15 |
Correct |
5 ms |
8012 KB |
Output is correct |
16 |
Correct |
5 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
5 ms |
8012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8008 KB |
Output is correct |
2 |
Correct |
5 ms |
8024 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
5 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
6 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8112 KB |
Output is correct |
8 |
Correct |
5 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
8 ms |
8100 KB |
Output is correct |
11 |
Correct |
5 ms |
8012 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
5 ms |
8012 KB |
Output is correct |
14 |
Correct |
5 ms |
8012 KB |
Output is correct |
15 |
Correct |
5 ms |
8012 KB |
Output is correct |
16 |
Correct |
5 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
5 ms |
8012 KB |
Output is correct |
19 |
Correct |
6 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8012 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
8 ms |
8092 KB |
Output is correct |
25 |
Correct |
6 ms |
8108 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8008 KB |
Output is correct |
2 |
Correct |
5 ms |
8024 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
5 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
6 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8112 KB |
Output is correct |
8 |
Correct |
5 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
8 ms |
8100 KB |
Output is correct |
11 |
Correct |
5 ms |
8012 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
5 ms |
8012 KB |
Output is correct |
14 |
Correct |
5 ms |
8012 KB |
Output is correct |
15 |
Correct |
5 ms |
8012 KB |
Output is correct |
16 |
Correct |
5 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
5 ms |
8012 KB |
Output is correct |
19 |
Correct |
6 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8012 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
8 ms |
8092 KB |
Output is correct |
25 |
Correct |
6 ms |
8108 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8108 KB |
Output is correct |
30 |
Correct |
180 ms |
28248 KB |
Output is correct |
31 |
Correct |
174 ms |
26168 KB |
Output is correct |
32 |
Correct |
123 ms |
23968 KB |
Output is correct |
33 |
Correct |
187 ms |
34612 KB |
Output is correct |
34 |
Correct |
172 ms |
34668 KB |
Output is correct |
35 |
Correct |
187 ms |
34852 KB |
Output is correct |
36 |
Correct |
140 ms |
30772 KB |
Output is correct |
37 |
Correct |
111 ms |
24644 KB |
Output is correct |
38 |
Correct |
109 ms |
24044 KB |
Output is correct |
39 |
Correct |
108 ms |
24040 KB |
Output is correct |
40 |
Correct |
117 ms |
24360 KB |
Output is correct |
41 |
Correct |
128 ms |
27048 KB |
Output is correct |
42 |
Correct |
123 ms |
25580 KB |
Output is correct |
43 |
Correct |
130 ms |
26200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
8108 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8008 KB |
Output is correct |
2 |
Correct |
5 ms |
8024 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
5 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
6 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8112 KB |
Output is correct |
8 |
Correct |
5 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
8 ms |
8100 KB |
Output is correct |
11 |
Correct |
5 ms |
8012 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
5 ms |
8012 KB |
Output is correct |
14 |
Correct |
5 ms |
8012 KB |
Output is correct |
15 |
Correct |
5 ms |
8012 KB |
Output is correct |
16 |
Correct |
5 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
5 ms |
8012 KB |
Output is correct |
19 |
Correct |
6 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8012 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
8 ms |
8092 KB |
Output is correct |
25 |
Correct |
6 ms |
8108 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8108 KB |
Output is correct |
30 |
Correct |
180 ms |
28248 KB |
Output is correct |
31 |
Correct |
174 ms |
26168 KB |
Output is correct |
32 |
Correct |
123 ms |
23968 KB |
Output is correct |
33 |
Correct |
187 ms |
34612 KB |
Output is correct |
34 |
Correct |
172 ms |
34668 KB |
Output is correct |
35 |
Correct |
187 ms |
34852 KB |
Output is correct |
36 |
Correct |
140 ms |
30772 KB |
Output is correct |
37 |
Correct |
111 ms |
24644 KB |
Output is correct |
38 |
Correct |
109 ms |
24044 KB |
Output is correct |
39 |
Correct |
108 ms |
24040 KB |
Output is correct |
40 |
Correct |
117 ms |
24360 KB |
Output is correct |
41 |
Correct |
128 ms |
27048 KB |
Output is correct |
42 |
Correct |
123 ms |
25580 KB |
Output is correct |
43 |
Correct |
130 ms |
26200 KB |
Output is correct |
44 |
Incorrect |
5 ms |
8108 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |