#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e6+10;
int n;
int ds[maxn], dsz[maxn], dsl[maxn], dsr[maxn];
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(dsz[u] < dsz[v]) swap(u,v);
dsz[u]+= dsz[v];
dsl[u] = min(dsl[u],dsl[v]);
dsr[u] = max(dsr[u],dsr[v]);
ds[v] = u;
}
int L,R;
map<int,int> dp[maxn];
int sol(int l, int r) {
if(dp[l].count(r)) return dp[l][r];
if(l == L && r == R) return dp[l][r] = 0;
dp[l][r] = inf;
if(l > L) dp[l][r] = min(dp[l][r],2+sol(dsl[find(l-1)],max(r,dsr[find(l-1)])));
if(r < R) dp[l][r] = min(dp[l][r],2+sol(min(l,dsl[r+1]),dsr[r+1]));
return dp[l][r];
}
long long minimum_walk(vector<int32_t> p, int32_t s) {
// return 0;
n = p.size();
for(int i = 0; i < n; i++) {
ds[i] = i;
dsz[i] = 1;
dsl[i] = i;
dsr[i] = i;
}
for(int i = 0; i < n; i++) {
join(i,p[i]);
}
int itl = 0;
while(itl != n) {
int itr = itl;
int cur = itl;
while(cur <= itr) {
itr = max(itr,dsr[find(cur)]);
cur++;
}
for(int i = itl; i < itr; i++) {
join(i,i+1);
}
itl = itr+1;
}
L = 0;
R = n-1;
while(L < s && dsz[find(L)] == 1) L++;
while(R > s && dsz[find(R)] == 1) R--;
int ans = 0;
for(int i = 0; i < n; i++) ans+= abs(p[i]-i);
return ans+sol(dsl[find(s)],dsr[find(s)]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
53852 KB |
Output is correct |
2 |
Correct |
11 ms |
53852 KB |
Output is correct |
3 |
Correct |
14 ms |
53956 KB |
Output is correct |
4 |
Correct |
11 ms |
53852 KB |
Output is correct |
5 |
Correct |
11 ms |
53852 KB |
Output is correct |
6 |
Correct |
13 ms |
54364 KB |
Output is correct |
7 |
Correct |
11 ms |
53852 KB |
Output is correct |
8 |
Correct |
12 ms |
53852 KB |
Output is correct |
9 |
Correct |
11 ms |
53848 KB |
Output is correct |
10 |
Correct |
11 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53960 KB |
Output is correct |
12 |
Correct |
12 ms |
53848 KB |
Output is correct |
13 |
Correct |
11 ms |
53852 KB |
Output is correct |
14 |
Correct |
12 ms |
53932 KB |
Output is correct |
15 |
Correct |
12 ms |
53976 KB |
Output is correct |
16 |
Correct |
11 ms |
53852 KB |
Output is correct |
17 |
Correct |
11 ms |
53852 KB |
Output is correct |
18 |
Correct |
11 ms |
53848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
53852 KB |
Output is correct |
2 |
Correct |
11 ms |
53852 KB |
Output is correct |
3 |
Correct |
14 ms |
53956 KB |
Output is correct |
4 |
Correct |
11 ms |
53852 KB |
Output is correct |
5 |
Correct |
11 ms |
53852 KB |
Output is correct |
6 |
Correct |
13 ms |
54364 KB |
Output is correct |
7 |
Correct |
11 ms |
53852 KB |
Output is correct |
8 |
Correct |
12 ms |
53852 KB |
Output is correct |
9 |
Correct |
11 ms |
53848 KB |
Output is correct |
10 |
Correct |
11 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53960 KB |
Output is correct |
12 |
Correct |
12 ms |
53848 KB |
Output is correct |
13 |
Correct |
11 ms |
53852 KB |
Output is correct |
14 |
Correct |
12 ms |
53932 KB |
Output is correct |
15 |
Correct |
12 ms |
53976 KB |
Output is correct |
16 |
Correct |
11 ms |
53852 KB |
Output is correct |
17 |
Correct |
11 ms |
53852 KB |
Output is correct |
18 |
Correct |
11 ms |
53848 KB |
Output is correct |
19 |
Correct |
11 ms |
53852 KB |
Output is correct |
20 |
Correct |
11 ms |
53980 KB |
Output is correct |
21 |
Correct |
13 ms |
53852 KB |
Output is correct |
22 |
Incorrect |
13 ms |
53892 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2092' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
53852 KB |
Output is correct |
2 |
Correct |
11 ms |
53852 KB |
Output is correct |
3 |
Correct |
14 ms |
53956 KB |
Output is correct |
4 |
Correct |
11 ms |
53852 KB |
Output is correct |
5 |
Correct |
11 ms |
53852 KB |
Output is correct |
6 |
Correct |
13 ms |
54364 KB |
Output is correct |
7 |
Correct |
11 ms |
53852 KB |
Output is correct |
8 |
Correct |
12 ms |
53852 KB |
Output is correct |
9 |
Correct |
11 ms |
53848 KB |
Output is correct |
10 |
Correct |
11 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53960 KB |
Output is correct |
12 |
Correct |
12 ms |
53848 KB |
Output is correct |
13 |
Correct |
11 ms |
53852 KB |
Output is correct |
14 |
Correct |
12 ms |
53932 KB |
Output is correct |
15 |
Correct |
12 ms |
53976 KB |
Output is correct |
16 |
Correct |
11 ms |
53852 KB |
Output is correct |
17 |
Correct |
11 ms |
53852 KB |
Output is correct |
18 |
Correct |
11 ms |
53848 KB |
Output is correct |
19 |
Correct |
11 ms |
53852 KB |
Output is correct |
20 |
Correct |
11 ms |
53980 KB |
Output is correct |
21 |
Correct |
13 ms |
53852 KB |
Output is correct |
22 |
Incorrect |
13 ms |
53892 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2092' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
53852 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
53852 KB |
Output is correct |
2 |
Correct |
11 ms |
53852 KB |
Output is correct |
3 |
Correct |
14 ms |
53956 KB |
Output is correct |
4 |
Correct |
11 ms |
53852 KB |
Output is correct |
5 |
Correct |
11 ms |
53852 KB |
Output is correct |
6 |
Correct |
13 ms |
54364 KB |
Output is correct |
7 |
Correct |
11 ms |
53852 KB |
Output is correct |
8 |
Correct |
12 ms |
53852 KB |
Output is correct |
9 |
Correct |
11 ms |
53848 KB |
Output is correct |
10 |
Correct |
11 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53960 KB |
Output is correct |
12 |
Correct |
12 ms |
53848 KB |
Output is correct |
13 |
Correct |
11 ms |
53852 KB |
Output is correct |
14 |
Correct |
12 ms |
53932 KB |
Output is correct |
15 |
Correct |
12 ms |
53976 KB |
Output is correct |
16 |
Correct |
11 ms |
53852 KB |
Output is correct |
17 |
Correct |
11 ms |
53852 KB |
Output is correct |
18 |
Correct |
11 ms |
53848 KB |
Output is correct |
19 |
Correct |
11 ms |
53852 KB |
Output is correct |
20 |
Correct |
11 ms |
53980 KB |
Output is correct |
21 |
Correct |
13 ms |
53852 KB |
Output is correct |
22 |
Incorrect |
13 ms |
53892 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2092' |
23 |
Halted |
0 ms |
0 KB |
- |