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;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
const int maxn=(1<<20)+5;
int arr[maxn];
int cnt[25];
vector<int>sub[maxn];
pi2 f(pi2 a, int b, int c){
if(b>=a.first.first){
if(a.first.second==c){
a.first={b,c};
}
else{
a.second=a.first;
a.first={b,c};
}
}
else if(b>=a.second.first){
if(a.first.second!=c) a.second={b,c};
}
return a;
}
pi2 memo[25][maxn];
bool vis[25][maxn];
void out(int x){
for(int y=0;y<m;y++){
cout << ((x&(1<<y))>0);
}
cout << endl;
}
pi2 dp(int index, int mask){
if(index>=m){
pi2 cur={{0,0},{0,0}};
int num=__builtin_popcount(mask);
if(sub[mask].size()==1){
cur.first={num,sub[mask][0]};
}
else if(sub[mask].size()>1){
cur.first={num,sub[mask][0]};
cur.second={num,sub[mask][1]};
}
//if(cur.first.first>=4){
//show2(a,cur.first.first,b,cur.first.second);
//show2(c,cur.second.first,d,cur.second.second);
//cout << "mask ";
//out(mask);
//}
return cur;
}
if(vis[index][mask]) return memo[index][mask];
pi2 ans=dp(index+1,mask);
if(mask&(1<<index)){
pi2 hold=dp(index+1,mask^(1<<index));
ans=f(ans,hold.first.first,hold.first.second);
ans=f(ans,hold.second.first,hold.second.second);
}
vis[index][mask]=true;
return memo[index][mask]=ans;
}
void dfs(int index, int mask){
if(mask==0) return;
if(sub[mask].size()>=2) return;
if(sub[mask].size()>0&&sub[mask][0]==index) return;
if(sub[mask].size()>1&&sub[mask][1]==index) return;
sub[mask].push_back(index);
for(int x=0;x<m;x++){
if(mask&(1<<x)){
dfs(index,mask^(1<<x));
}
}
}
void solve(){
cin >> n >> m;
int temp;
for(int x=0;x<n;x++){
int inv=0;
for(int y=0;y<m;y++){
cin >> temp;
arr[x]+=temp*(1<<y);
cnt[y]+=temp;
inv+=(1-temp)*(1<<y);
}
dfs(x,inv);
}
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
if(arr[x]&(1<<y)) cnt[y]--;
}
int crit=n/2;
int safe=0;
int hold=0;
for(int y=0;y<m;y++){
if(cnt[y]>crit) safe++;
if(cnt[y]==crit) hold+=(1<<y);
}
//show2(x,x,safe,safe);
//out(hold);
pi2 take=dp(0,hold);
//show2(aa,take.first.first,bb,take.first.second);
//show2(cc,take.second.first,dd,take.second.second);
if(take.first.second==x){
safe+=take.second.first;
}
else safe+=take.first.first;
for(int y=0;y<m;y++){
if(arr[x]&(1<<y)) cnt[y]++;
}
cout << safe << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | 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... |