이 제출은 이전 버전의 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)
typedef long long ll;
int N, n, A[1<<17], B[1<<17], inc[17][2], cnt[17][1<<17];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
n = (1<<N);
FOR(i,0,n-1){
int B; cin >> B;
A[B] = i;
}
ll ans = 1e18;
int mini = -1, bits = -1;
FOR(i,0,N-1){
memset(inc,0,sizeof inc);
memset(cnt,0,sizeof cnt);
FOR(j,0,n-1){
A[j] = (A[j]>>1) | ((A[j]&1)<<(N-1));
int B = A[j];
FOR(k,0,N-1){
inc[k][B&1] += cnt[N-k-1][B>>1] - cnt[N-k][B];
cnt[N-k][B]++;
B >>= 1;
}
++cnt[0][0];
}
//FOR(j,0,n-1){ cout << A[j] << ' '; } cout << endl;
//FOR(k,0,N-1){ cout << k _ inc[k][0] _ inc[k][1] << endl; }
ll cur = 0;
int x = 0;
FOR(j,0,N-1){
if (inc[j][0] < inc[j][1]) cur += inc[j][0];
else cur += inc[j][1], x |= (1<<j);
}
if (cur < ans) ans = cur, mini = i, bits = x;
}
cout << ans << '\n';
FOR(i,0,mini) cout << 2;
FOR(i,0,N-1){
cout << 2;
if (bits&(1<<i)) cout << 1;
}
cout << '\n';
}
# | 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... |