#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n, m;
cin >> n >> m;
vector<vector<ll> > a(n, vector<ll> (m));
vector<ll> d1(n, 0), d2(n, 0), cnt(m, 0), ans(n, 0);
vector<pi> dp1(1LL << m, mp(0, -1)), dp2(1LL << m, mp(0, -1));
for (ll i=0; i<n; ++i)
{
for (ll j=0; j<m; ++j) cin >> a[i][j];
for (ll j=0; j<m; ++j) if (a[i][j]==0) d1[i] |= (1LL << j);
}
for (ll i=0; i<m; ++i) for (ll j=0; j<n; ++j) cnt[i] += a[j][i];
for (ll i=0; i<n; ++i)
{
for (ll j=0; j<m; ++j)
{
if (cnt[j] - n/2 - a[i][j] > 0) ans[i]++;
if (cnt[j] - n/2 - a[i][j] == 0) d2[i] |= (1LL << j);
}
}
for (ll i=0; i<n; ++i)
{
pi now = mp(1, i);
if (dp1[d1[i]].s == now.s && dp1[d1[i]].f < now.f) dp1[d1[i]].f = now.f;
else if (dp1[d1[i]] < now) dp2[d1[i]] = dp1[d1[i]], dp1[d1[i]] = now;
else if (dp1[d1[i]].s != now.s && dp2[d1[i]] < now) dp2[d1[i]] = now;
}
for (ll i=(1LL << m)-1; i>=0; --i)
{
for (ll j=0; j<m; ++j) if (i & (1LL << j))
{
ll mask = i ^ (1LL << j);
pi now = dp1[i];
if (dp1[mask].s == now.s && dp1[mask].f < now.f) dp1[mask].f = now.f;
else if (dp1[mask] < now) dp2[mask] = dp1[mask], dp1[mask] = now;
else if (dp1[mask].s != now.s && dp2[mask] < now) dp2[mask] = now;
now = dp2[i];
if (dp1[mask].s == now.s && dp1[mask].f < now.f) dp1[mask].f = now.f;
else if (dp1[mask] < now) dp2[mask] = dp1[mask], dp1[mask] = now;
else if (dp1[mask].s != now.s && dp2[mask] < now) dp2[mask] = now;
}
}
for (ll i=0; i<(1LL << m); ++i) dp1[i].f*= __builtin_popcountll(i), dp2[i].f*= __builtin_popcountll(i);
for (ll i=0; i<(1LL << m); ++i)
{
for (ll j=0; j<m; ++j) if (i & (1LL << j))
{
ll mask = i ^ (1LL << j);
pi now = dp1[mask];
if (dp1[i].s == now.s && dp1[i].f < now.f) dp1[i].f = now.f;
else if (dp1[i] < now) dp2[i] = dp1[i], dp1[i] = now;
else if (dp1[i].s != now.s && dp2[i] < now) dp2[i] = now;
now = dp2[mask];
if (dp1[i].s == now.s && dp1[i].f < now.f) dp1[i].f = now.f;
else if (dp1[i] < now) dp2[i] = dp1[i], dp1[i] = now;
else if (dp1[i].s != now.s && dp2[i] < now) dp2[i] = now;
}
}
for (ll i=0; i<n; ++i)
{
if (dp1[d2[i]].s == i) ans[i] += dp2[d2[i]].f;
else ans[i] += dp1[d2[i]].f;
assert(dp1[d2[i]].s!=dp2[d2[i]].s);
cout << ans[i] << endl;
}
return 0;
}
# | 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... |