Codeforces Round #375 (Div. 2) C Polycarp at the Radio

本文介绍了一个关于音乐播放列表调整的问题,旨在通过改变播放列表使指定乐队的歌曲数量尽可能均匀分布,同时最小化所需更改次数。文章提供了一段C++代码实现,通过贪心算法策略解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


                                                                                                            C. Polycarp at the Radio
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.

We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.

Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.

Input

The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.

Output

In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.

In the second line print the changed playlist.

If there are multiple answers, print any of them.



水题,注意贪心顺序;

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=2005;
int vis[maxn],in[maxn],flag[maxn];
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&in[i]);
    }
    int all=n/m;
    for(int i=1;i<=n;i++)
    {
        int now=in[i];
        if(now<=m)
        {
            if(flag[now]<all)
            {
                flag[now]++;
                vis[i]=1;
            }
        }
    }
    int now=1,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            while(flag[now]>=all&&now<=m)
            {
                now++;
            }
            if(now>m)
                break;
            in[i]=now;
            flag[now]++;
            ans++;
        }
      // cout<<now<<endl;
    }
    cout<<all<<" "<<ans<<endl;
    for(int i=1;i<=n;i++)
    {
        printf("%d ",in[i]);
    }
    puts("");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值