// ~ Be Name Khoda ~ //
#include "books.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 1e6 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int cmp[maxn5], lef[maxn5], rig[maxn5], num = 0;
bool mark[maxn5];
vector <int> per;
void expa(int &l, int &r){
int lq = l, rq = r;
while(true){
lq = min({lq, lef[cmp[l]], lef[cmp[r]]});
rq = max({rq, rig[cmp[l]], rig[cmp[r]]});
//cout << "haaaa " << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
if(l > lq){
l--;
continue;
}
if(r < rq){
r++;
continue;
}
break;
}
}
int solve(int lq, int rq, int s){
int l = s, r = s;
expa(l, r);
int ans = 0;
//cout << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
while(l > lq || r < rq){
int c1 = 0, c2 = 0;
int l1 = l, r1 = r, l2 = l, r2 = r;
while(r1 <= r && l1 > lq){
l1--;
c1++;
expa(l1, r1);
}
while(l2 >= l && r2 < rq){
r2++;
//cout << r2 << ' ' << cmp[r2] << ' ' << rig[cmp[r2]] << endl;
c2++;
expa(l2, r2);
//cout << "hoy " << l2 << ' ' << r2 << ' ' << rig[cmp[r2]] << ' ' << cmp[r2] << endl;
}
ans += min(c1, c2) * 2;
l = min(l1, l2);
r = max(r1, r2);
if(l == lq && r == rq)
ans += max(c1, c2) * 2;
//cout << l << ' ' << r << ' ' << ans << ' ' << c1 << ' ' << c2 << endl;
}
return ans;
}
void dfs(int v){
mark[v] = true;
cmp[v] = num;
lef[num] = min(lef[num], v);
rig[num] = max(rig[num], v);
if(!mark[per[v]])
dfs(per[v]);
}
long long minimum_walk(std::vector<int> PER, int s) {
per = PER;
int n = per.size();
int r = s, l = s;
ll ans = 0;
for(int i = 0; i < n; i++){
if(!mark[i]){
lef[num] = rig[num] = i;
dfs(i);
num++;
}
//cout << i << ' ' << per[i] << ' ' << cmp[i] << endl;
ans += abs(per[i] - i);
if(i != per[i]){
l = min(l, i);
r = max(r, i);
}
}
if(l == r)
return 0;
//cout << "ha? " << ans << endl;
return solve(l, r, s) + ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
240 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
240 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
144 ms |
23648 KB |
Output is correct |
31 |
Correct |
108 ms |
23628 KB |
Output is correct |
32 |
Correct |
97 ms |
31460 KB |
Output is correct |
33 |
Correct |
102 ms |
29004 KB |
Output is correct |
34 |
Correct |
111 ms |
28988 KB |
Output is correct |
35 |
Correct |
110 ms |
27972 KB |
Output is correct |
36 |
Correct |
96 ms |
25584 KB |
Output is correct |
37 |
Correct |
107 ms |
23908 KB |
Output is correct |
38 |
Correct |
90 ms |
23648 KB |
Output is correct |
39 |
Correct |
96 ms |
23656 KB |
Output is correct |
40 |
Correct |
103 ms |
23556 KB |
Output is correct |
41 |
Correct |
104 ms |
23652 KB |
Output is correct |
42 |
Correct |
97 ms |
23656 KB |
Output is correct |
43 |
Correct |
99 ms |
29408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4066' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
240 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
144 ms |
23648 KB |
Output is correct |
31 |
Correct |
108 ms |
23628 KB |
Output is correct |
32 |
Correct |
97 ms |
31460 KB |
Output is correct |
33 |
Correct |
102 ms |
29004 KB |
Output is correct |
34 |
Correct |
111 ms |
28988 KB |
Output is correct |
35 |
Correct |
110 ms |
27972 KB |
Output is correct |
36 |
Correct |
96 ms |
25584 KB |
Output is correct |
37 |
Correct |
107 ms |
23908 KB |
Output is correct |
38 |
Correct |
90 ms |
23648 KB |
Output is correct |
39 |
Correct |
96 ms |
23656 KB |
Output is correct |
40 |
Correct |
103 ms |
23556 KB |
Output is correct |
41 |
Correct |
104 ms |
23652 KB |
Output is correct |
42 |
Correct |
97 ms |
23656 KB |
Output is correct |
43 |
Correct |
99 ms |
29408 KB |
Output is correct |
44 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4066' |
45 |
Halted |
0 ms |
0 KB |
- |