이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
using ll = long long;
using namespace std;
ll get_inversion(vector<int>& p) {
int n = p.size();
ll ans = 0;
for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) {
ans += p[i] > p[j];
}
return ans;
}
vector<int> combine_permutations(vector<int> a, vector<int> b) {
int n = a.size();
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
ans[i] = b[a[i]];
}
return ans;
}
vector<int> reverse_permutation(vector<int> a) {
int n = a.size();
vector<int> ans(n);
for (int i = 0; i < n; ++i)
ans[a[i]] = i;
return ans;
}
pair<ll, string> get_ans(vector<int> p) {
int n = p.size();
vector<int> aaa = p;
ll ans = get_inversion(aaa);
int ansx = 0;
//for (int bt = n / 2; bt > 0; bt /= 2) {
//int nx = ansx + bt;
for (int nx = 0; nx < n; ++nx) {
vector<int> op(n);
for (int i = 0; i < n; ++i)
op[i] = i ^ nx;
vector<int> np = combine_permutations(op, p);
ll nans = get_inversion(np);
if (nans < ans) {
ans = nans;
ansx = nx;
}
}
string ansa = "";
for (int bt = n / 2; bt > 0; bt /= 2) {
if (ansx & bt)
ansa.push_back('1');
ansa.push_back('2');
}
//cout << ans << ' ' << ansx << endl;
return {ans, ansa};
}
signed main() {
int N;
cin >> N;
int m = (1 << N);
vector<int> p(m);
for (int i = 0; i < m; ++i)
cin >> p[i];
if (N == 0) {
cout << "0\n";
cout << "1\n";
return 0;
}
vector<int> op1(m);
vector<int> op2(m);
for (int i = 0; i < m; ++i) {
op1[i] = i ^ (m / 2);
op2[i] = (i >> 1) + ((i & 1) << (N - 1));
}
ll ans = 1ll * m * m;;
string ansa;
string curst = "";
for (int t = 0; t < N; ++t) {
auto [nans, nansa] = get_ans(p);
if (nans < ans) {
ans = nans;
ansa = curst + nansa;
}
p = combine_permutations(op2, p);
curst.push_back('2');
}
cout << ans << '\n';
cout << ansa << '\n';
//cout << endl;
}
# | 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... |