Submission #675449

#TimeUsernameProblemLanguageResultExecution timeMemory
675449hotboy2703Cheerleaders (info1cup20_cheerleaders)C++14
41 / 100
1050 ms3388 KiB
#include<bits/stdc++.h> using namespace std; #define BIT(mask,i) (((mask) >> (i)) & 1LL) #define MASK(i) (1LL<<(i)) long long h[200100]; long long n; long long a[200100]; long long BIT[(1<<17) + 5]; void update(long long x,long long val){ for (;x<=(1<<17);x+=x&-x)BIT[x]+=val; } long long get(long long x){ long long res = 0; for (;x > 0;x -= x &-x)res += BIT[x]; return res; } pair <long long,long long> solve(){ long long res = 0; long long id = 0; for (long long i = n - 1;i >= 0;i --){ long long inversion = 0; for (long long j = 0;j < (1<<(n-1-i));j ++){ for (long long k = 0;k < (1<<i);k ++){ update(a[(j<<(i+1)) + (1<<i) + k] + 1,1); } for (long long k = 0;k < (1<<i);k ++){ inversion += get(a[(j<<(i+1)) + k]); } for (long long k = 0;k < (1<<i);k ++){ update(a[(j<<(i+1)) + (1<<i) + k] + 1,-1); } } if ((1<<(n-1+i)) - inversion < inversion){ id ^= (1<<i); } res += min((1<<(n-1+i)) - inversion , inversion); } return {res,id}; } int main(){ ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr); cin>>n; for (long long i = 0;i < (1<<n);i ++)cin>>h[i]; if (n == 0){ cout<<0<<'\n'; return 0; } long long ans = LLONG_MAX; pair <long long, long long> trace; for (long long i = n - 1;i >= 0;i --){ for (long long mask = 0;mask < MASK(n);mask++){ long long cur = i; long long real_mask = 0; for (long long k = n - 1;k >= 0;k --){ real_mask ^= (BIT(mask,k)<<cur); cur--; if (cur < 0)cur = n - 1; } a[mask] = h[real_mask]; } pair <long long,long long> tmp = solve(); if (tmp.first < ans){ ans = tmp.first; trace.first = i; trace.second = tmp.second; } } cout<<ans<<'\n'; for (long long i = 0;i < (trace.first - n + 1 + n) % n;i++)cout<<2; for (long long i = n - 1;i >= 0;i--){ if (BIT(trace.second,i))cout<<1; cout<<2; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...