제출 #784947

#제출 시각아이디문제언어결과실행 시간메모리
784947oscar1fCouncil (JOI23_council)C++17
48 / 100
4088 ms64848 KiB
#include<bits/stdc++.h>
using namespace std;

const int MAX_PERS=300*1000+5,MAX_PROP=22,MAX_VAL=(1<<20);

int nbPers,nbChoix,rep,val,gratuit,r1,r2;
int choix[MAX_PERS][MAX_PROP];
int somme[MAX_PROP];
vector<int> nbOccu[MAX_VAL];
int val1[MAX_VAL];
int memo[MAX_VAL];
int rest1[MAX_VAL],rest2[MAX_VAL];

int compte1(int nb) {
	int nb1=0;
	while (nb>0) {
		nb1+=nb%2;
		nb/=2;
	}
	return nb1;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>nbPers>>nbChoix;
	for (int i=0;i<nbPers;i++) {
		for (int j=0;j<nbChoix;j++) {
			cin>>choix[i][j];
			somme[j]+=choix[i][j];
		}
	}
	for (int j=0;j<nbChoix;j++) {
		if (somme[j]-2>=nbPers/2) {
			gratuit++;
		}
		else if (somme[j]-1==nbPers/2) {
			for (int i=0;i<nbPers;i++) {
				rest2[i]+=(1<<j)*(1-choix[i][j]);
			}
		}
		else if (somme[j]==nbPers/2) {
			for (int i=0;i<nbPers;i++) {
				rest1[i]+=(1<<j)*(1-choix[i][j]);
			}
		}
	}
	for (int i=0;i<(1<<nbChoix);i++) {
		val1[i]=compte1(i);
		memo[i]=-1;
	}
	for (int i=0;i<nbPers;i++) {
		val=0;
		for (int j=0;j<nbChoix;j++) {
			val+=(1<<j)*choix[i][j];
		}
		nbOccu[val].push_back(i);
	}
	for (int i=0;i<nbPers;i++) {
		rep=0;
		val=0;
		for (int j=0;j<nbChoix;j++) {
			val+=(1<<j)*choix[i][j];
		}
		if (memo[val]!=-1) {
			cout<<memo[val]<<"\n";
		}
		else {
			for (int j=0;j<(1<<nbChoix);j++) {
				if ((nbOccu[j].size()>0 and j!=val) or nbOccu[j].size()>1) {
					r1=(rest1[i]&rest1[nbOccu[j].back()]);
					r2=(rest2[i]|rest2[nbOccu[j].back()]);
					//cout<<i<<" "<<j<<" : "<<r1<<" "<<r2<<endl;
					//cout<<r1<<" "<<r2<<endl;
					rep=max(rep,gratuit+val1[r1]+val1[r2]);
				}
			}
			memo[val]=rep;
			cout<<rep<<"\n";
		}
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...