# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
674544 | QwertyPi | Cheerleaders (info1cup20_cheerleaders) | C++14 | 162 ms | 20844 KiB |
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>
#define int long long
using namespace std;
vector<int> f1(vector<int> v){
vector<int> nv;
int k = v.size() >> 1;
for(int i = k; i < k * 2; i++){
nv.push_back(v[i]);
}
for(int i = 0; i < k; i++){
nv.push_back(v[i]);
}
return nv;
}
vector<int> f2(vector<int> v){
vector<int> nv;
int k = v.size() >> 1;
for(int i = 0; i < k; i++){
nv.push_back(v[i * 2]);
}
for(int i = 0; i < k; i++){
nv.push_back(v[i * 2 + 1]);
}
return nv;
}
void pr(vector<int> v){
for(auto i : v) cout << i << ' '; cout << endl;
}
void g(vector<int>& a, string s){
vector<int> v(a.begin(), a.end());
for(auto i : s){
if(i == '1'){
v = f1(v);
}else if(i == '2'){
v = f2(v);
}
}
cout << s << ": "; pr(v);
}
int dp[1 << 17];
int b[1 << 17];
int inv(vector<int>& a, int l, int m, int r){
int x = l, y = m;
int id = l;
int cnt = 0;
while(x != m || y != r){
if(y == r || x != m && a[x] < a[y]) b[id++] = a[x++];
else b[id++] = a[y++], cnt += x - l;
}
for(int i = l; i < r; i++) a[i] = b[i];
return cnt;
}
int test(int n, vector<int> a){
int ans = 0;
for(int j = 0; j < n; j++){
int c = 0;
for(int l = 0; l < (1 << n); l += (1 << j + 1)){
c += inv(a, l, l + (1 << j), l + (1 << j + 1));
}
ans += min(c, (1 << n + j - 1) - c);
}
return ans;
}
int mcost[17];
int32_t main(){
vector<int> a[17];
int n; cin >> n;
if(n == 0) { cout << "0\n11\n"; return 0; }
for(int i = 0; i < (1 << n); i++){
int v; cin >> v; a[0].push_back(v);
}
for(int i = 1; i < n; i++){
a[i] = f2(a[i - 1]);
}
for(int i = 0; i < n; i++){
mcost[i] = test(n, a[i]);
}
cout << *min_element(mcost, mcost + n) << "\n11\n"; return 0;
vector<int> cur(1 << n);
for(int i = 0; i < (1 << n); i++){
cur[i] = a[0][i];
}
string ans = "11";
while(abs(cur[1] - cur[0]) != 1)
ans.push_back('2'), cur = f2(cur);
for(int i = 0; i < n; i++){
if(cur[0] > cur[1 << i]){
for(int j = 0; j < i + 1; j++) ans.push_back('2'), cur = f2(cur);
ans.push_back('1'), cur = f1(cur);
for(int j = i + 1; j < n; j++) ans.push_back('2'), cur = f2(cur);
}
}
cout << 0 << endl;
cout << ans << endl;
}
/*
: 1 2 3 4
1: 3 4 1 2
212: 2 1 4 3
1212: 4 3 2 1
*/
Compilation message (stderr)
# | 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... |