This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const int K = 20;
const int M = 1 << K;
const ll linf = 1e18;
pair<int, int> mx[M][2], pom[M][2];
vector<int> pos[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int k = n / 2;
vector<int> a(n), cnt(m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x; cin >> x;
if (x == 1) {
cnt[j] += 1;
a[i] += 1 << j;
}
}
}
int www = 0;
int p = 0, q = 0;
for (int i = 0; i < m; i++) {
if (cnt[i] >= k + 2) www++;
if (cnt[i] == k + 1) p += (1 << i);
if (cnt[i] == k) q += (1 << i);
}
for (int i = 0; i < n; i++) a[i] ^= p ^ q;
if (n <= 3000) {
for (int i = 0; i < n; i++) {
int mx = 0;
for (int j = 0; j < n; j++) if (i != j) {
smax(mx, __builtin_popcount(p & (a[i] | a[j])) + __builtin_popcount(q & (a[i] & a[j])));
}
cout << mx + www << en;
}
return 0;
}
for (int i = 0; i < n; i++) {
if (pos[a[i]].size() < 2) pos[a[i]].push_back(i);
}
for (int mask = M - 1; mask >= 0; mask--) {
int o = __builtin_popcount(mask);
pom[mask][0] = pom[mask][1] = {-1, -1};
for (int i = 0; i < pos[mask].size(); i++) pom[mask][i] = {o, pos[mask][i]};
for (int i = 0; i < K; i++) {
int sm = (1 << i) ^ mask;
if (!((1 << i) & mask)) {
for (int j = 0; j < 2; j++) {
if (pom[sm][j].second != -1 && pom[sm][j].second != pom[mask][0].second) {
pom[mask][1] = pom[mask][0];
pom[mask][0] = {o, pom[sm][j].second};
}
else if (pom[sm][j].second != -1 && pom[sm][j].second != pom[mask][0].second) {
pom[mask][1] = {o, pom[sm][j].second};
}
}
}
}
}
for (int mask = 0; mask < M; mask++) {
int o = __builtin_popcount(mask);
mx[mask][0] = pom[mask][0];
mx[mask][1] = pom[mask][1];
for (int i = 0; i < m; i++) {
if (!((1 << i) & mask)) continue;
for (int j = 0; j < 2; j++) {
if (mx[i][j] > mx[mask][0] && mx[i][j].second != mx[mask][0].second) {
mx[mask][1] = mx[mask][0];
mx[mask][0] = mx[i][j];
}
else if (mx[i][j] > mx[mask][1] && mx[i][j].second != mx[mask][0].second) mx[mask][1] = mx[i][j];
}
}
}
for (int i = 0; i < n; i++) {
int x = a[i] & q;
int y = a[i] ^ p;
y &= p;
x |= y;
int sol = www;
sol += __builtin_popcount(a[i] & p);
if (mx[x][0].second == i && mx[x][1].second != -1) sol += mx[x][1].first;
else if (mx[x][0].second != -1) sol += mx[x][0].first;
cout << sol << en;
}
return 0;
}
Compilation message (stderr)
council.cpp: In function 'int main()':
council.cpp:56:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < pos[mask].size(); i++) pom[mask][i] = {o, pos[mask][i]};
| ~~^~~~~~~~~~~~~~~~~~
council.cpp:73:13: warning: unused variable 'o' [-Wunused-variable]
73 | int o = __builtin_popcount(mask);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |