# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
428834 | jamezzz | Cheerleaders (info1cup20_cheerleaders) | C++17 | 2110 ms | 391736 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>
using namespace std;
#define sf scanf
#define pf printf
#define pb emplace_back
typedef long long ll;
#define maxn 131077
int n,x,ft[maxn];
ll best;
vector<int> v;
string cur,ans;
set<vector<int>> vis;
void up(int x,int nv){
while(x<=n)ft[x]+=nv,x+=x&-x;
}
int qry(int x){
int res=0;
while(x)res+=ft[x],x-=x&-x;
return res;
}
ll inv(){
memset(ft,0,sizeof ft);
ll res=0;
for(int x:v){
res+=qry(x);
up(1,1);up(x+1,-1);
}
return res;
}
void dfs(){
if(vis.count(v))return;
vis.insert(v);
ll tmp=inv();
if(tmp<best){
best=tmp;
ans=cur;
}
vector<int> t[2];
for(int i=n/2;i<n;++i)t[0].pb(v[i]);
for(int i=0;i<n/2;++i)t[0].pb(v[i]);
for(int i=0;i<n;i+=2)t[1].pb(v[i]);
for(int i=1;i<n;i+=2)t[1].pb(v[i]);
cur.push_back('1');
swap(v,t[0]);
dfs();
cur.pop_back();
swap(v,t[0]);
swap(v,t[1]);
cur.push_back('2');
dfs();
cur.pop_back();
swap(v,t[1]);
}
int main(){
sf("%d",&n);n=(1<<n);
for(int i=0;i<n;++i){
sf("%d",&x);v.pb(++x);
}
best=inv();
dfs();
pf("%lld\n",best);
pf("%s\n",ans.c_str());
}
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... |