Submission #430131

# Submission time Handle Problem Language Result Execution time Memory
430131 2021-06-16T11:40:00 Z jamezzz Cheerleaders (info1cup20_cheerleaders) C++17
100 / 100
220 ms 10820 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;

typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements

#define DEBUG

#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));

#define maxn 131075

int n,x,a[maxn],cnt[18][1<<17];
ll inv[17][2];

int main(){
	sf("%d",&n);
	for(int i=0;i<(1<<n);++i){
		sf("%d",&x);
		a[x]=i;
	}
	
	if(n==0)pf("0\n"),exit(0);
	
	ll ans=LINF;int min=-1,bits=-1;
	for(int i=0;i<n;++i){
		//shifted by i
		memset(inv,0,sizeof inv);
		memset(cnt,0,sizeof cnt);
		for(int j=0;j<(1<<n);++j){
			int b=a[j];
			for(int k=0;k<n;++k){
				inv[k][b&1]+=cnt[n-k-1][b>>1]-cnt[n-k][b];
				cnt[n-k][b]++;
				b>>=1;
			}
			++cnt[0][0];
		}
		
		ll cur=0;
		int x=0;
		for(int j=0;j<n;++j){
			if(inv[j][0]<inv[j][1])cur+=inv[j][0];
			else cur+=inv[j][1],x|=(1<<j);
		}
		if(cur<ans)ans=cur,min=i,bits=x;
		
		for(int j=0;j<(1<<n);++j){
			int one=a[j]&1;
			a[j]>>=1;
			a[j]+=(one<<(n-1));
		}
	}
	
	pf("%lld\n",ans);
	for(int i=0;i<min;++i)pf("2");
	for(int i=0;i<n;++i){
		pf("2");
		if(bits&(1<<i))pf("1");
	}
	pf("\n");
}

Compilation message

cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:48:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |  sf("%d",&n);
      |    ^
cheerleaders.cpp:50:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   sf("%d",&x);
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct!
2 Correct 9 ms 9420 KB Correct!
3 Correct 9 ms 9420 KB Correct!
4 Correct 8 ms 9532 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9420 KB Correct!
2 Correct 13 ms 9420 KB Correct!
3 Correct 14 ms 9420 KB Correct!
4 Correct 12 ms 9532 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 17 ms 9548 KB Correct!
2 Correct 18 ms 9548 KB Correct!
3 Correct 20 ms 9548 KB Correct!
4 Correct 20 ms 9548 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 50 ms 9820 KB Correct!
2 Correct 52 ms 9824 KB Correct!
3 Correct 106 ms 10152 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 220 ms 10692 KB Correct!
2 Correct 183 ms 10820 KB Correct!
3 Correct 196 ms 10776 KB Correct!
4 Correct 195 ms 10808 KB Correct!
5 Correct 201 ms 10780 KB Correct!