Submission #1335211

#TimeUsernameProblemLanguageResultExecution timeMemory
1335211gelastropodCouncil (JOI23_council)C++20
0 / 100
1468 ms591424 KiB
#pragma GCC optimize("O3,inline")
#include <bits/stdc++.h>
#include <bit>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N, M, a, val1 = 0;
	unsigned int _e = 0, f = 0; cin >> N >> M;
	unsigned int fl = (1LL << M) - 1;
	vector<unsigned int> vals(N, 0);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> a;
			if (a) vals[i] += (1LL << j);
		}
	}
	for (int i = 0; i < M; i++) {
		int crnt = 0;
		for (int j = 0; j < N; j++)
			if (vals[j] & (1LL << i)) crnt++;
		if (crnt > N / 2 + 1) val1++;
		else if (crnt == N / 2 + 1) _e += (1LL << i);
		if (crnt >= N / 2 && crnt <= N / 2 + 1) f += (1LL << i);
	}
	vector<vector<pair<pair<int, int>, pair<int, int>>>> minpc((1LL << M), vector<pair<pair<int, int>, pair<int, int>>>(M + 1, { {INT_MIN, -1}, {INT_MIN, -1} }));
	vector<vector<pair<int, int>>> minpc1((1LL << M), vector<pair<int, int>>(M + 1, pair<int, int>(-1, -1)));
	for (int i = 0; i < N; i++) {
		if (minpc1[fl ^ vals[i]][0].first == -1) minpc1[fl ^ vals[i]][0].first = i;
		else if (minpc1[fl ^ vals[i]][0].second == -1) minpc1[fl ^ vals[i]][0].second = i;
	}
	for (int i = (1LL << M) - 1; i >= 0; i--) {
		for (int j = 0; j < M; j++) {
			minpc1[i][j + 1] = minpc1[i][j];
			if (!(i & (1LL << j))) {
				if (minpc1[i][j + 1].first == -1) minpc1[i][j + 1].first = minpc1[i ^ (1LL << j)][j].first;
				if (minpc1[i][j + 1].first != -1 && minpc1[i][j + 1].second == -1 && minpc1[i][j + 1].first != minpc1[i ^ (1LL << j)][j].first) minpc1[i][j + 1].second = minpc1[i ^ (1LL << j)][j].first;
				if (minpc1[i][j + 1].first == -1 && minpc1[i][j + 1].second == -1) minpc1[i][j + 1].second = minpc1[i ^ (1LL << j)][j].second;
			}
		}
	}
	for (int i = 0; i < (1LL << M); i++) {
		int res = popcount((unsigned int)i);
		if (minpc1[i].back().first != -1) minpc[i][0].first = { res, minpc1[i].back().first };
		if (minpc1[i].back().second != -1) minpc[i][0].second = { res, minpc1[i].back().second };
		for (int j = 0; j < M; j++) {
			vector<pair<int, int>> e;
			e.push_back(minpc[i][j].first);
			e.push_back(minpc[i][j].second);
			if (i & (1LL << j)) {
				if (minpc[i ^ (1LL << j)][j].first.second == e[0].second) e[0].first = max(e[0].first, minpc[i ^ (1LL << j)][j].first.first);
				else if (minpc[i ^ (1LL << j)][j].first.second == e[1].second) e[1].first = max(e[1].first, minpc[i ^ (1LL << j)][j].first.first);
				else e.push_back(minpc[i ^ (1LL << j)][j].first);
				if (minpc[i ^ (1LL << j)][j].second.second == e[0].second) e[0].first = max(e[0].first, minpc[i ^ (1LL << j)][j].second.first);
				else if (minpc[i ^ (1LL << j)][j].second.second == e[1].second) e[1].first = max(e[1].first, minpc[i ^ (1LL << j)][j].second.first);
				else e.push_back(minpc[i ^ (1LL << j)][j].second);
			}
			sort(e.begin(), e.end());
			minpc[i][j + 1] = { e.back(), e[e.size() - 2] };
		}
	}
	for (int i = 0; i < N; i++) {
		int crnt = popcount((~vals[i]) & _e), crnt1 = 0;
		unsigned int g = (~(vals[i] ^ _e)) & f;
		int goofy = popcount(g & (~vals[i]));
		if (minpc[g].back().first.second == i) crnt1 = minpc[g].back().second.first;
		else crnt1 = minpc[g].back().first.first;
		cout << val1 + crnt + crnt1 << '\n';
	}
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...