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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
const int MAXM = 20;
const int INF = 1e9;
int N, M, A[MAXN+10][MAXM+10];
int P[MAXM+10];
int B[MAXN+10];
pii dp[(1<<MAXM)+10][MAXM+10];
pii operator + (pii p, int q)
{
return {p.first+q, p.second+q};
}
void f(pii &p, pii q)
{
if(p.first<q.first) p={p.first, min(p.second, q.first)};
else p={q.first, min(q.second, p.first)};
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) for(int j=0; j<M; j++) scanf("%d", &A[i][j]);
for(int j=0; j<M; j++)
{
int cnt=0;
for(int i=1; i<=N; i++) cnt+=A[i][j];
if(cnt<N/2) P[j]=0;
else if(cnt>N/2+1) P[j]=3;
else if(cnt==N/2) P[j]=1;
else if(cnt==N/2+1) P[j]=2;
}
for(int j=0; j<=M; j++) for(int i=0; i<(1<<M); i++) dp[i][j]=pii(INF, INF);
for(int i=1; i<=N; i++)
{
for(int j=0; j<M; j++) B[i]|=(A[i][j]<<j);
f(dp[B[i]][0], pii(0, INF));
}
for(int i=0; i<M; i++)
{
for(int j=0; j<(1<<M); j++)
{
if(P[i]==0)
{
f(dp[j][i+1], dp[j][i]+1);
f(dp[j][i+1], dp[j^(1<<i)][i]+1);
}
if(P[i]==3)
{
f(dp[j][i+1], dp[j][i]);
f(dp[j][i+1], dp[j^(1<<i)][i]);
}
if(P[i]==1)
{
if(j&(1<<i))
{
f(dp[j][i+1], dp[j][i]+1);
f(dp[j][i+1], dp[j^(1<<i)][i]+1);
}
else
{
f(dp[j][i+1], dp[j][i]);
f(dp[j][i+1], dp[j^(1<<i)][i]+1);
}
}
if(P[i]==2)
{
if(j&(1<<i))
{
f(dp[j][i+1], dp[j][i]+1);
f(dp[j][i+1], dp[j^(1<<i)][i]);
}
else
{
f(dp[j][i+1], dp[j][i]);
f(dp[j][i+1], dp[j^(1<<i)][i]);
}
}
}
}
for(int i=1; i<=N; i++)
{
if(__builtin_popcount(B[i])!=M-dp[B[i]][M].first) printf("%d\n", M-dp[B[i]][M].first);
else printf("%d\n", M-dp[B[i]][M].second);
}
}
Compilation message (stderr)
council.cpp: In function 'int main()':
council.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
council.cpp:32:54: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | for(int i=1; i<=N; i++) for(int j=0; j<M; j++) scanf("%d", &A[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |