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;
int n,a,b,c,mini=2147000000;
string res,resak;
vector<int>v;
map<int,int>mp;
pair<string,long long> solve(vector<int>v2){
string odp="";
long long odpint=0;
for(int i=0;i<n;i++){
int licz[1<<i+1];
for(int j=0;j<1<<i+1;j++)
licz[j]=0;
int wyn=0,wyn2=0;
for(int j=0;j<(int)v2.size();j++){
int smth = v2[j]>>n-i-1;
if(smth%2==0)
wyn+=licz[smth+1];
if(smth%2==1)
wyn2+=licz[smth-1];
licz[smth]++;
}
//cout<<wyn<<" "<<wyn2<<" ";
if(wyn>wyn2){
odp.push_back('1');
}
odpint+=min(wyn,wyn2);
for(int j=1;j<n;j++)
odp.push_back('2');
}
// v2={0,1,2,3};
//cout<<" ";
//if(v==v2)
// return {"21",37};
// return {"2137",2137};
return {odp,odpint};
}
int main(){
cin>>n;
a=1<<n;
for(int i=0;i<a;i++)
v.push_back(0);
for(int i=0;i<a;i++){
cin>>b;
v[b]=i;
}
for(int i=0;i<n+1;i++){
resak.clear();
for(int j=0;j<i;j++)
resak.push_back('2');
pair<string,long long>ak=solve(v);
//cout<<ak.first<<" ";
if(ak.second<mini){
res=resak;
res+=ak.first;
mini=ak.second;
}
// for(int j=0;j<(int)v.size();j++){
//cout<<v[j]<<" ";
// }
for(int j=0;j<(int)v.size();j++){
bool smth=v[j]%2;
v[j]=v[j]>>1;
v[j]+=smth<<n-1;
}
//cout<<"\n";
}
cout<<mini<<"\n"<<res;
}
Compilation message (stderr)
cheerleaders.cpp: In function 'std::pair<std::__cxx11::basic_string<char>, long long int> solve(std::vector<int>)':
cheerleaders.cpp:12:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
12 | int licz[1<<i+1];
| ~^~
cheerleaders.cpp:13:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
13 | for(int j=0;j<1<<i+1;j++)
| ~^~
cheerleaders.cpp:17:34: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
17 | int smth = v2[j]>>n-i-1;
| ~~~^~
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:66:26: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
66 | v[j]+=smth<<n-1;
| ~^~
# | 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... |