This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define tern(cond, a, b) (cond ? a : b)
typedef long long lint;
typedef pair<lint,lint> ii;
vector<int> original;
vector<int> arr[140005];
vector<int> nxt;
vector<int> temp;
lint ans = 123123231232132LL;
lint res = 0;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int N = (1<<n);
original.resize(N);
for(int i = 0;i < N;i++) cin >> original[i];
if(n == 0){
cout << 0;
return 0;
}
for(int iter = 0;iter < n;iter++){
for(int i = 0;i < N;i++) arr[i].clear();
for(int i = 0;i < N;i++) arr[i].push_back(original[i]);
res = 0;
for(lint gap = 1;gap < N;gap *= 2){
lint invcnt = 0;
lint total = 0;
for(int s = 0;s < N;s += 2*gap){
int m = s+gap, e = s+2*gap;
lint l = 0;
for(int r : arr[m]){
while(l < sz(arr[s])){
if(arr[s][l] > r) break;
else l++;
}
invcnt += l;
}
total += gap*gap;
swap(temp, arr[s]);
arr[s].resize(sz(temp) + sz(arr[m]));
merge(all(temp), all(arr[m]), arr[s].begin());
temp.clear();
}
if(total-invcnt < invcnt){
res += total-invcnt;
}
else{
res += invcnt;
}
}
//show(res);
ans = min(ans, res);
//for(int i = 0;i < N;i++) cerr << original[i] << " "; cerr << endl;
//for(int i = 0;i < N;i++) cerr << original[i] << " "; cerr << endl;
nxt.clear();
for(int i = 0;i < N;i += 2) nxt.push_back(original[i]);
for(int i = 1;i < N;i += 2) nxt.push_back(original[i]);
swap(nxt, original);
}
cout << ans;
}
Compilation message (stderr)
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:44:20: warning: unused variable 'e' [-Wunused-variable]
44 | int m = s+gap, e = s+2*gap;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |