#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
const ll INF = 1e18;
//#include "books.h"
ll minimum_walk(vector<int> p, int s) {
ll N = p.size();
ll flux[N];
for (ll i=0;i<N;i++) {
flux[i]=0;
}
for (ll i=0;i<N;i++) {
if (p[i]>i) {
flux[i]++;
flux[p[i]]--;
}
}
ll pf[N+1];
pf[0]=0;
ll ans = 0;
for (ll i=0;i<N;i++) {
pf[i+1]=pf[i]+flux[i];
//cout << "x="<<(i+1)<<", pf[x]="<<pf[i+1]<<"\n";
if (pf[i+1]>1) {
ans += 2*(pf[i+1]-1);
}
}
ll maxl[N+1]; ll minl[N+1];
for (ll i=0;i<=N;i++) {
maxl[i]=-INF;
minl[i]=INF;
}
for (ll i=1;i<=N;i++) {
maxl[pf[i]]=max(maxl[pf[i]],i);
minl[pf[i]]=min(minl[pf[i]],i-1);
}
for (ll i=N;i>=1;i--) {
maxl[i-1]=max(maxl[i],maxl[i-1]);
minl[i-1]=min(minl[i],minl[i-1]);
}
if (minl[1]!=INF) { //below valid if s=0
ans += 2*(maxl[1]-minl[1]);
}
if (minl[1]!=INF) { //below valid if s=0
if (s==0) {
ans += 2*(minl[1]);
}
}
if (s != 0) {
bool found[N];
ll grp[N];
bool grpf[N];
vector<vector<ll>> gelem;
bool foundg[N];
bool found2[N];
for (ll i=0;i<N;i++) {
found[i]=0;
grp[i]=-1;
grpf[i]=0;
foundg[i]=0;
found2[i]=0;
}
for (ll i=0;i<N;i++) {
if (!found[i]) {
vector<ll> enew;
//cout << "f1\n";
ll j = i;
ll T = 0;
ll lp=INF; ll rp=-INF;
do {
T++;
lp = min(lp,j);
rp = max(rp,j);
found[j]=1;
enew.push_back(j);
j=p[j];
} while (j != i);
for (ll x: enew) {
grp[x]=gelem.size();
//cout << "x="<<x<<", grp[x]="<<grp[x]<<"\n";
}
gelem.push_back(enew);
}
}
priority_queue<pii> pq; //{-time,position}
pq.push({0,s});
ll Tmin = INF;
while (!pq.empty()) {
auto A = pq.top(); pq.pop();
ll t = -A.first; ll x = A.second;
if (found2[x]) {
continue;
}
found2[x]=1;
if (x==maxl[1] || x==minl[1]) {
Tmin = min(Tmin,t);
}
ll g = grp[x];
if (!foundg[g]) {
foundg[g]=1;
for (ll y: gelem[g]) {
if (y != x) {
pq.push({-t,y});
}
}
}
if (x>0) {
pq.push({-t-1,x-1});
}
if (x<(N-1)) {
pq.push({-t-1,x+1});
}
}
ans += 2*Tmin;
}
//cout << "minl[1],maxl[1]="<<minl[1]<<","<<maxl[1]<<"\n";
return ans;
}
/*int main() {
vector<int> p1 = {0,1,4,3,2,5,6};
cout << minimum_walk(p1,3);
}*/
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:54:8: warning: variable 'grpf' set but not used [-Wunused-but-set-variable]
54 | bool grpf[N];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
432 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
440 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
432 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
440 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
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 |
432 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
440 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
93 ms |
46184 KB |
Output is correct |
31 |
Correct |
103 ms |
46072 KB |
Output is correct |
32 |
Correct |
99 ms |
46196 KB |
Output is correct |
33 |
Correct |
95 ms |
46064 KB |
Output is correct |
34 |
Correct |
95 ms |
46164 KB |
Output is correct |
35 |
Correct |
96 ms |
46188 KB |
Output is correct |
36 |
Correct |
96 ms |
46160 KB |
Output is correct |
37 |
Correct |
93 ms |
46188 KB |
Output is correct |
38 |
Correct |
106 ms |
46164 KB |
Output is correct |
39 |
Correct |
87 ms |
46160 KB |
Output is correct |
40 |
Correct |
91 ms |
46176 KB |
Output is correct |
41 |
Correct |
103 ms |
46172 KB |
Output is correct |
42 |
Correct |
89 ms |
46060 KB |
Output is correct |
43 |
Correct |
98 ms |
46164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3312' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
432 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
440 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
93 ms |
46184 KB |
Output is correct |
31 |
Correct |
103 ms |
46072 KB |
Output is correct |
32 |
Correct |
99 ms |
46196 KB |
Output is correct |
33 |
Correct |
95 ms |
46064 KB |
Output is correct |
34 |
Correct |
95 ms |
46164 KB |
Output is correct |
35 |
Correct |
96 ms |
46188 KB |
Output is correct |
36 |
Correct |
96 ms |
46160 KB |
Output is correct |
37 |
Correct |
93 ms |
46188 KB |
Output is correct |
38 |
Correct |
106 ms |
46164 KB |
Output is correct |
39 |
Correct |
87 ms |
46160 KB |
Output is correct |
40 |
Correct |
91 ms |
46176 KB |
Output is correct |
41 |
Correct |
103 ms |
46172 KB |
Output is correct |
42 |
Correct |
89 ms |
46060 KB |
Output is correct |
43 |
Correct |
98 ms |
46164 KB |
Output is correct |
44 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3312' |
45 |
Halted |
0 ms |
0 KB |
- |