#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 1e6 + 10;
const ll inf = 1e18;
int n, mn, mx, cnt[maxn][2];
bool mark[maxn][2];
ll Calc(){
ll ans = 0;
for (int i = mn+1; i <= mx; i++){
cnt[i][0] += cnt[i-1][0];
cnt[i][1] += cnt[i-1][1];
//debug(i, cnt[i][0], cnt[i][1]);
if (cnt[i][0] == 0){
cnt[i][0]++;
cnt[i+1][0]--;
mark[i][0] = true;
}
if (cnt[i][1] == 0){
cnt[i][1]++;
cnt[i+1][1]--;
mark[i][1] = true;
}
if (cnt[i][0] < cnt[i][1]){
mark[i][0] = true;
}
if (cnt[i][0] > cnt[i][1]){
mark[i][1] = true;
}
//debug(i, cnt[i][0], cnt[i][1]);
ans += 2 * max(cnt[i][0], cnt[i][1]);
}
debug(ans);
return ans;
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
mn = s, mx = s;
int mxmn = -1, mnmx = n+1;
for (int i = 0; i < n; i++){
if (p[i] == i) continue;
mn = min(mn, i);
if (i <= s) mxmn = max(mxmn, i);
mx = max(mx, i);
if (s <= i) mnmx = min(mnmx, i);
if (p[i] < i){
cnt[p[i]+1][0]++;
cnt[i+1][0]--;
}
else{
cnt[i+1][1]++;
cnt[p[i]+1][1]--;
}
}
//debug(mn, mx);
if (mn == mx) return 0;
//debug(1);
if (mxmn == -1) mxmn = s;
if (mnmx == n+1) mnmx = s;
assert(mxmn != s && mnmx != s);
//debug(mxmn, mnmx);
ll ans = Calc();
int cnt1 = 0, cnt2 = 0;
for (int i = mxmn+1; i <= s; i++){
cnt1 += (!mark[i][0]);
cnt1 += (!mark[i][1]);
}
for (int i = s+1; i <= mnmx; i++){
cnt2 += (!mark[i][0]);
cnt2 += (!mark[i][1]);
}
return ans + 2 * min(s - mxmn, mnmx - s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |