运动会I
题面大意
有 n 个人, m 个项目,每个人对这 m 个项目都有一个优先级,现从中选出若干个项目,人们会选择现有项目中优先级最高的项目,求玩的人数最多的项目玩的人数的最小值
思路
用一个cnt数组保存每个项目选择的人数,每次删除选择人数最多的项目删除,这样,每次都会破坏最大值,在删除的过程中更新答案
#include<bits/stdc++.h>
using namespace std;
int n, m;
int cnt[305], a[305][305], now[305], vis[305];
int main() {
int ans = 100000000;//给答案赋一个初始值
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
//项目全选的情况下的cnt数组,每个人都选最喜欢的项目
for(int i = 1; i <= n; i++)
cnt[a[i][now[i] = 1]]++;
for(int i = 1; i <= m; i++) {
int maxn = 1, pos = -1;
//找到最多人选的那个项目
for(int j = 1; j <= m; j++) {
if(!vis[j] && cnt[j] > maxn) {
maxn = cnt[j];
pos = j;
}
}
//更新答案
ans = min(ans, maxn);
//删除项目
vis[pos] = 1;
for(int j = 1; j <= n; j++) {
if(vis[a[j][now[j]]]) {
while(vis[a[j][now[j]]]) now[j]++; //把第j个人记录到他要参加的项目
//更新cnt数组
cnt[pos]--;
cnt[a[j][now[j]]]++;
}
}
}
cout << ans;
return 0;
}