Submission #339207

#TimeUsernameProblemLanguageResultExecution timeMemory
339207cheissmartCheerleaders (info1cup20_cheerleaders)C++14
72 / 100
2031 ms68204 KiB
// #include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <random>
#include <iostream>
#include <ctime>
#pragma GCC optimize("O3", "unroll-loops", "no-stack-protector")
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 17;

int a[1 << N];
vi who[1 << N][N];

signed main()
{
	IO_OP;

	int n;
	cin >> n;
// n = 17;
	if(n == 0){
		cout << "0\n\n";
		return 0;
	}
	for(int i = 0; i < (1 << n); i++) {
		int x;
		cin >> x;
// x = i;
		a[x] = i;
	}
// mt19937 rng(time(0));
// shuffle(a, a + (1 << n), rng);

	auto shift_right = [&]() {
		putchar('2');
	};
	auto flip_bit = [&](int bit) {
		for(int i = 0; i < bit + 1; i++) shift_right();
		putchar('1');
		for(int i = 0; i < n - bit - 1; i++) shift_right();
	};
	auto xor_val = [&](int val) {
		for(int i = 0; i < n; i++) if(val >> i & 1)
			flip_bit(i);
	};
	ll best_inv = 1e18, best_shift = 0, best_xor = 0;	
	for(int i = 0; i < n; i++)
		for(int j = 0; j < (1 << (n - i)); j++)
			who[j][i].reserve(1 << i);
	for(int shift = 0; shift < n; shift++) {
		for(int i = 0; i < n; i++)
			for(int j = 0; j < (1 << (n - i)); j++)
				who[j][i].clear();
		for(int j = 0; j < (1 << n); j++)
			for(int i = 0; i < n; i++)
				who[a[j] >> i][i].PB(j);
		auto count_inv = [&](vi& a, vi& b) {
			ll ans = 0;
			for(int i = 0, j = 0; i < a.size(); i++) {
				while(j < b.size() && b[j] < a[i]) j++;
				ans += j;
			}
			return ans;
		};
		ll val = 0, inv = 0;
		for(int i = n - 1; i >= 0; i--) {
			ll one = 0, zero = 0;
			for(int prefix = 0; prefix < (1 << (n - i)); prefix += 2) {
				ll cur_zero = count_inv(who[prefix][i], who[prefix^1][i]);
				// ll cur_one = count_inv(who[mask2][i], who[mask1][i]);
				ll cur_one = (1LL << i << i) - cur_zero; // const opt
				one += cur_one, zero += cur_zero;
			}
			if(one < zero) val |= 1 << i;
			inv += min(one, zero);
			if(inv > best_inv) break; // const opt
		}
		if(inv < best_inv) {
			best_inv = inv;
			best_shift = shift;
			best_xor = val;
		}
		for(int i = 0; i < (1 << n); i++)
			a[i] = (a[i] >> 1) | ((a[i] & 1) << (n - 1)); // shift right
	}
	cout << best_inv << '\n';
	for(int i = 0; i < best_shift; i++) shift_right();
	xor_val(best_xor);
	cout << '\n';
}

Compilation message (stderr)

cheerleaders.cpp: In lambda function:
cheerleaders.cpp:74:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |    for(int i = 0, j = 0; i < a.size(); i++) {
      |                          ~~^~~~~~~~~~
cheerleaders.cpp:75:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     while(j < b.size() && b[j] < a[i]) j++;
      |           ~~^~~~~~~~~~
#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...