이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
int N, org[1<<17], A[1<<17], tmp[2][1<<16];
int ft[1<<17], n, nn;
void update(int i, int x) {
for (; i <= n; i += i&-i) ft[i] += x;
}
int query(int i) {
int r = 0;
for (; i; i -= i&-i) r += ft[i];
return r;
}
int inv() {
int cnt = 0;
memset(ft,0,sizeof ft);
FOR(i,0,(1<<N)-1){
cnt += i-query(A[i]);
update(A[i]+1,1);
}
return cnt;
}
void sim(int op) {
if (op == 1) {
FOR(i,0,(1<<(N-1))-1){
swap(A[i],A[i+(1<<(N-1))]);
}
} else {
int c[] = {0,0};
FOR(i,0,(1<<N)-1) tmp[i&1][c[i&1]++] = A[i];
FOR(i,0,(1<<(N-1))-1) A[i] = tmp[0][i];
FOR(i,0,(1<<(N-1))-1) A[i+(1<<(N-1))] = tmp[1][i];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
FOR(i,0,(1<<N)-1){
cin >> org[i];
}
n = (1<<N);
nn = n>>1;
int mininv = 1e9;
vector<int> ans;
FOR(s,0,n-1){
FOR(i,0,n-1) A[i] = org[i];
vector<int> ops;
while (true) {
int pos0 = -1;
FOR(i,0,n-1) if (A[i] == s) pos0 = i;
if (pos0 == 0) break;
int op;
if (pos0 < nn) op = 2;
else op = 1;
ops.push_back(op);
sim(op);
}
int cur = inv(), mini = 0;
FOR(i,1,N-1){
sim(2);
int nw = inv();
if (nw < cur) {
cur = nw;
mini = i;
}
}
if (cur < mininv) {
mininv = cur;
FOR(i,1,mini) ops.push_back(2);
ans = ops;
//TRACE(cur _ SZ(ops));
}
if (N > 11) break;
}
cout << mininv << '\n';
for (int& x : ans) {
cout << x;
}
}
# | 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... |