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;
typedef pair<pii, pii> ppp;
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];
ppp dp[(1<<MAXM)+10][MAXM+10];
pii operator + (pii p, int q)
{
return {p.first+q, p.second};
}
ppp operator + (ppp p, int q)
{
return {p.first+q, p.second+q};
}
void f(ppp &p, ppp q)
{
vector<pii> V={p.first, p.second, q.first, q.second};
sort(V.begin(), V.end());
p.first=V[0];
for(int i=1; i<4; i++) if(p.first.second!=V[i].second) { p.second=V[i]; break; }
}
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, -1), pii(INF, -1)};
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, i), pii(INF, -1)});
}
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(dp[B[i]][M].first.second!=i) printf("%d\n", M-dp[B[i]][M].first.first);
else printf("%d\n", M-dp[B[i]][M].second.first);
}
}
Compilation message (stderr)
council.cpp: In function 'int main()':
council.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
council.cpp:40:54: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | 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... |