Submission #339752

# Submission time Handle Problem Language Result Execution time Memory
339752 2020-12-26T06:10:42 Z wwdd Cheerleaders (info1cup20_cheerleaders) C++14
100 / 100
1159 ms 32216 KB
//laziness 10^100
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
typedef vector<ll> vl;
const ll INFL = 1e18;
vl w;
vl wa,wi;
void shuf(int n) {
	vl x(w.size(),0);
	for(int i=0;i<w.size();i++) {
		int nx = (i>>1) + ((i&1)<<(n-1));
		x[nx] = w[i];
	}
	w = x;
	wa.assign(n,0);
	wi.assign(n,-1);
}
void proc(int n) {
	vector<vl> wo[2] = {{w.size(),vl()},{w.size(),vl()}};
	int cu = 0;
	for(int i=0;i<w.size();i++) {
		wo[cu][i].push_back(w[i]);
	}
	for(int k=0;k<n;k++) {
		ll res[2] = {0,0};
		int nu = 1-cu;
		for(int i=0;i<w.size();i+=(1<<(k+1))) {
			for(int j=0;j<2;j++) {
				int pt = 0;
				for(int l=0;l<wo[cu][i^(j<<k)].size();l++) {
					while(pt < wo[cu][i^((1^j)<<k)].size() && wo[cu][i^((1^j)<<k)][pt] < wo[cu][i^(j<<k)][l]) {
						pt++;
					}
					res[j] += pt;
				}
			}
			wo[nu][i].resize((1<<(k+1)));
			merge(wo[cu][i].begin(),wo[cu][i].end(),
					wo[cu][i+(1<<k)].begin(),wo[cu][i+(1<<k)].end(),
					wo[nu][i].begin());
		}
		wa[k] = min(res[0],res[1]);
		wi[k] = res[0] > res[1];
		cu = nu;
	}
}
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	ll n;
	cin >> n;
	ll m = 1<<n;
	wa.assign(n,0);
	wi.assign(n,-1);
	for(int i=0;i<m;i++) {
		ll t;
		cin >> t;
		w.push_back(t);
	}
	if(n == 0) {
		cout << 0 << '\n';
		cout << 2 << '\n';
		return 0;
	}
	ll ma = INFL;
	pl st = {0,0};
	for(int i=0;i<n;i++) {
		ll res = 0;
		ll rb = 0;
		proc(n);
		for(int j=0;j<n;j++) {
			res += wa[j];
			rb |= wi[j]<<j;
		}
		if(res < ma) {
			ma = res;
			st = {i,rb};
		}
		shuf(n);
	}
	cout << ma << '\n';
	for(int i=0;i<st.first;i++) {
		cout << "2";
	}
	st.second = (st.second<<1)|((st.second&(1<<(n-1)))>>(n-1));
	for(int i=0;i<n;i++) {
		if((st.second)&(1<<i)) {
			cout << "1";
		}
		cout << "2";
	}
	cout << '\n';
}

Compilation message

cheerleaders.cpp: In function 'void shuf(int)':
cheerleaders.cpp:12:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |  for(int i=0;i<w.size();i++) {
      |              ~^~~~~~~~~
cheerleaders.cpp: In function 'void proc(int)':
cheerleaders.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for(int i=0;i<w.size();i++) {
      |              ~^~~~~~~~~
cheerleaders.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   for(int i=0;i<w.size();i+=(1<<(k+1))) {
      |               ~^~~~~~~~~
cheerleaders.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int l=0;l<wo[cu][i^(j<<k)].size();l++) {
      |                 ~^~~~~~~~~~~~~~~~~~~~~~~~
cheerleaders.cpp:33:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |      while(pt < wo[cu][i^((1^j)<<k)].size() && wo[cu][i^((1^j)<<k)][pt] < wo[cu][i^(j<<k)][l]) {
      |            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct!
2 Correct 1 ms 364 KB Correct!
3 Correct 2 ms 364 KB Correct!
4 Correct 1 ms 364 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct!
2 Correct 2 ms 364 KB Correct!
3 Correct 1 ms 364 KB Correct!
4 Correct 2 ms 364 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 15 ms 748 KB Correct!
2 Correct 15 ms 748 KB Correct!
3 Correct 9 ms 748 KB Correct!
4 Correct 9 ms 748 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 127 ms 7800 KB Correct!
2 Correct 129 ms 7768 KB Correct!
3 Correct 281 ms 15648 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 781 ms 32216 KB Correct!
2 Correct 681 ms 31964 KB Correct!
3 Correct 1159 ms 32084 KB Correct!
4 Correct 1151 ms 32008 KB Correct!
5 Correct 1119 ms 32068 KB Correct!