// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = (b) - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)x.size()
#define pb push_back
#define debug(x) cout<<#x<<" = "<<x<<endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
const ll INF = 1'000'000'007;
ll n;
long long minimum_walk(std::vector<int> p, int s) {
n=sz(p);
ll cycles=0;
ll ans=0;
vll vis(n,0);
vll mxvals(n,-1);
rep(i,0,n) {
if(p[i]==i) continue;
ans+=abs(i-p[i]);
if(vis[i]) continue;
++cycles;
ll j=i;
ll mx=j;
while(!vis[j]) {
vis[j]=1;
j=p[j];
mx=max(mx,j);
}
mxvals[i]=mx;
}
ll mx=0;
//cout<<ans<<endl;
rep(i,0,n) {
//cout<<"i = "<<i<<" mxvals = "<<mxvals[i]<<endl;
if(mxvals[i]!=-1) {
//cout<<"i = "<<i<<" mx = "<<mx<<endl;
if(i) ans+=2*max(0LL,i-mx);
//cout<<123<<endl;
mx=max(mx,mxvals[i]);
}
}
//cout<<ans<<endl;
//cout<<cycles<<endl;
return ans;
}
/**
#include <cstdio>
#include <vector>
#include <cassert>
// BEGIN SECRET
#include <string>
// END SECRET7
x
using namespace std;
int main() {
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
/**/
Compilation message
books.cpp:106:1: warning: "/*" within comment [-Wcomment]
106 | /**/
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
440 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
440 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
0 ms |
436 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
440 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
0 ms |
436 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
440 KB |
Output is correct |
30 |
Correct |
99 ms |
30548 KB |
Output is correct |
31 |
Correct |
103 ms |
30432 KB |
Output is correct |
32 |
Correct |
73 ms |
30432 KB |
Output is correct |
33 |
Correct |
83 ms |
30556 KB |
Output is correct |
34 |
Correct |
82 ms |
30432 KB |
Output is correct |
35 |
Correct |
85 ms |
30444 KB |
Output is correct |
36 |
Correct |
76 ms |
30628 KB |
Output is correct |
37 |
Correct |
73 ms |
30560 KB |
Output is correct |
38 |
Correct |
88 ms |
30552 KB |
Output is correct |
39 |
Correct |
80 ms |
30576 KB |
Output is correct |
40 |
Correct |
99 ms |
30544 KB |
Output is correct |
41 |
Correct |
93 ms |
30544 KB |
Output is correct |
42 |
Correct |
102 ms |
30804 KB |
Output is correct |
43 |
Correct |
107 ms |
30800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
440 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
0 ms |
436 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
440 KB |
Output is correct |
30 |
Correct |
99 ms |
30548 KB |
Output is correct |
31 |
Correct |
103 ms |
30432 KB |
Output is correct |
32 |
Correct |
73 ms |
30432 KB |
Output is correct |
33 |
Correct |
83 ms |
30556 KB |
Output is correct |
34 |
Correct |
82 ms |
30432 KB |
Output is correct |
35 |
Correct |
85 ms |
30444 KB |
Output is correct |
36 |
Correct |
76 ms |
30628 KB |
Output is correct |
37 |
Correct |
73 ms |
30560 KB |
Output is correct |
38 |
Correct |
88 ms |
30552 KB |
Output is correct |
39 |
Correct |
80 ms |
30576 KB |
Output is correct |
40 |
Correct |
99 ms |
30544 KB |
Output is correct |
41 |
Correct |
93 ms |
30544 KB |
Output is correct |
42 |
Correct |
102 ms |
30804 KB |
Output is correct |
43 |
Correct |
107 ms |
30800 KB |
Output is correct |
44 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |