Submission #428738

# Submission time Handle Problem Language Result Execution time Memory
428738 2021-06-15T14:09:02 Z pavement Cheerleaders (info1cup20_cheerleaders) C++17
51 / 100
2000 ms 652028 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int N, ans = INT_MAX, ft[200005];
string out, curstr;
vector<int> curr;
set<vector<int> > seen;

vector<int> op1() {
	vector<int> ret(N);
	for (int i = 0; i < N; i++)
		if (i < N / 2) ret[i + N / 2] = curr[i];
		else ret[i - N / 2] = curr[i];
	return ret;
}

vector<int> op2() {
	vector<int> ret(N);
	int oddcnt = 0, evencnt = 0;
	for (int i = 0; i < N; i++)
		if ((i + 1) & 1) ret[oddcnt++] = curr[i];
		else ret[evencnt++ + N / 2] = curr[i];
	return ret;
}

inline int ls(int x) { return x & -x; }

int qry(int p) {
	int r = 0;
	for (; p; p -= ls(p)) r += ft[p];
	return r;
}

void upd(int p, int v) {
	for (; p <= N; p += ls(p)) ft[p] += v;
}

bool setans() {
	int tmp = 0;
	for (int i = 1; i <= N; i++) ft[i] = 0;
	for (int i = N - 1; i >= 0; i--) {
		tmp += qry(curr[i]);
		upd(curr[i], 1);
	}
	if (ans > tmp) {
		ans = tmp;
		return 1;
	}
	return 0;
}

void dfs() {
	if (seen.count(curr)) return;
	if (setans()) out = curstr;
	seen.insert(curr);
	vector<int> tmp1 = op1(), tmp2 = op2();
	curr = tmp1;
	curstr.pb('1');
	dfs();
	curstr.ppb();
	curstr.pb('2');
	curr = tmp2;
	dfs();
	curstr.ppb();
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N;
	N = 1 << N;
	for (int i = 1, x; i <= N; i++) {
		cin >> x;
		x++;
		curr.pb(x);
	}
	dfs();
	cout << ans << '\n';
	if (out == "") cout << "11\n";
	else cout << out << '\n';
}

Compilation message

cheerleaders.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   91 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct!
2 Correct 1 ms 332 KB Correct!
3 Correct 1 ms 348 KB Correct!
4 Correct 1 ms 324 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Correct!
2 Correct 5 ms 1612 KB Correct!
3 Correct 5 ms 1612 KB Correct!
4 Correct 5 ms 1664 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 1751 ms 498552 KB Correct!
2 Correct 1787 ms 498476 KB Correct!
3 Correct 1904 ms 498528 KB Correct!
4 Correct 1779 ms 498484 KB Correct!
# Verdict Execution time Memory Grader output
1 Execution timed out 2101 ms 652028 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2100 ms 524632 KB Time limit exceeded
2 Halted 0 ms 0 KB -