B - Sports Festival
Time limit : 2sec / Memory limit : 256MB
Score : 700 points
Problem Statement
Takahashi is hosting an sports meet. There are N people who will participate. These people are conveniently numbered 1 through N. Also, there are M options of sports for this event. These sports are numbered 1 through M. Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.
Takahashi knows that Person i's j-th favorite sport is Sport Aij. Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.
Takahashi is worried that one of the sports will attract too many people. Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized. Find the minimum possible number of the participants in the sport with the largest number of participants.
Constraints
- 1≤N≤300
- 1≤M≤300
- Ai1 , Ai2 , … , AiM is a permutation of the integers from 1 to M.
Input
Input is given from Standard Input in the following format:
N M A11 A12 … A1M A21 A22 … A2M : AN1 AN2 … ANM
Output
Print the minimum possible number of the participants in the sport with the largest number of participants.
Sample Input 1
4 5 5 1 3 4 2 2 5 3 1 4 2 3 1 4 5 2 5 4 3 1
Sample Output 1
2
Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport4. Here, the sport with the largest number of participants is Sport 3, with two participants. There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.
Sample Input 2
3 3 2 1 3 2 1 3 2 1 3
Sample Output 2
3
Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected. Therefore, the answer is 3.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<sstream>
#include<queue>
#include<set>
#include<cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int mp[300+5][300+56];
int n,m;
int idx[305];
int cnt[305];
int mp2[300+5][300+5];
int pos[305][305];
int check(int x){
memset(cnt,0,sizeof(cnt));
for(int i = 0;i<n;i++){
cnt[mp2[i][idx[i]]]++;
}
int mx = -1;
int res ;
for(int i = 1;i<=m;i++){
if(mx<cnt[i]){
mx = max(mx,cnt[i]);
res = i;
}
}
if(mx>x)
return -res;
return res;
}
void print()
{
for(int i = 0 ;i<n;i++){
for(int j = 0;j<m;j++){
printf("%d%c",mp2[i][j],j==m-1?'\n':' ');
}
}
}
int ok(int x){
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
mp2[i][j] = mp[i][j];
}
}
memset(idx,0,sizeof(idx));
for(int i = 0;i<m;i++)
{
int res = check(x);
//print();
if(res>0)
return 1;
for(int j = 0;j<n;j++){
mp2[j][pos[j][-res]] = 0;
}
for(int j = 0;j<n;j++){
while(mp2[j][idx[j]]==0&&idx[j]<m){
idx[j]++;
}
if(idx[j]>=m)
return 0;
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
scanf("%d",&mp[i][j]);
pos[i][mp[i][j]] = j;
}
}
int l = 0;
int r = n+1;
while(r-l>1){
int mid = (l+r)>>1;
if(ok(mid)){
r = mid;
}
else
l =mid;
}
cout<<r<<endl;
}
}