题目地址:​​这里​

题意:有一个歌单,歌单上面有n首歌,你喜欢1,2,3….m号乐队唱的歌。你希望改变这个歌单,使得你喜欢的乐队们唱歌唱得最少的尽量大。问你最少修改多少次,且输出方案。

解法: 显然是每个乐队演唱n/m首歌,然后我们就维护一下这个就好了。贪心的for两次。

//CF 723C

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int n, m, a[maxn], cnt[maxn], vis[maxn];
int ans1, ans2 = 0;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
int ans1 = n/m;
for(int i = 1; i <= n; i++){
if(a[i] > m) continue;
if(cnt[a[i]] >= ans1) continue;
cnt[a[i]]++;
vis[i] = 1;
}
for(int i = 1; i <= n; i++){
if(vis[i]) continue;
int Ans1 = 1e9, Ans2 = 1e9;
for(int j = 1; j <= m; j++){
if(cnt[j] < Ans2){
Ans2 = cnt[j], Ans1 = j;
}
}
if(Ans2 == ans1) continue;
ans2++;
cnt[Ans1]++, a[i] = Ans1;
}
cout << ans1 << " " << ans2 << endl;
for(int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
}