【codeforces 723C】 + 思维

72 篇文章 1 订阅
2 篇文章 0 订阅

C. Polycarp at the Radio
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Examples
input
4 2
1 2 3 2
output
2 1
1 2 1 2

input
7 3
1 3 2 2 2 2 1
output
2 1
1 3 3 2 2 2 1

input
4 4
1000000000 100 7 1000000000
output
1 4
1 2 3 4

Note
In the first sample, after Polycarp’s changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.

In the second sample, after Polycarp’s changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.

前两题在40分钟内全部一A了,第三题一个小时的时候看懂了题意,用自己的方法尝试了一下发现行不通,然后就一头雾水,于是和一很好的朋友讨论了一下,跟他说明题意后,在最后50分钟的时候他想到一种思路,我们迅速达成一致,但开始的时候想到的不全面,接下他边完善我边敲,终于在最后5分钟的时候解决了所有顾虑~~但却在第 7 个案例上编译错误了~~~比赛刚结束就被我一眼发现了,在最后一个循环中加了个判断,在提交就AC了~~坑爹啊~~

题意 : 给出一组数组的长度N和符合数字的最大值M,下面是一组数组

思路 : 出现次数最少肯定就是 pl = N / M 了,把1~M 的数出现次数打个表,如果这M个数中有出现次数小于pl的,先把数组中有大于N的数变成该数,如果还有小于pl的在吧1~M中次数大于pl的变成成该数即可;

下面是AC代码 :

#include<cstdio>
int pa[2011],ma[2011],ka[2011];
int main()
{
    int N,M,i,j,pl,kl,sum = 0;
    scanf("%d%d",&N,&M);
    for(i = 1 ; i <= N ; i++){
        scanf("%d",&ma[i]);
        if(ma[i] <= M)
            pa[ma[i]]++;
    }
    pl = N / M; // 最小解
    for(i = 1 ; i <= N ; i++){
        if(ma[i] > M){
            for(j = 1 ; j <= M ; j++){
                if(pa[j] < pl){
                    pa[j]++;
                    ma[i] = j;//改变该值为 j 
                    sum++; // 记录改变次数
                    break;
                }
            }
        }
    }
    kl = 0;
    for(i = 1 ; i <= N ; i++){
            kl = ma[i]; 
        if(kl > M) // 判断是否还有大于M的数,防止数组越界
            continue;
        if(pa[kl] > pl){
            for(j = 1 ; j <= M ; j++)
                if(pa[j] < pl){ //如果有未满足的数则改之
                    pa[j]++;
                    pa[kl]--;
                    ma[i] = j;
                    sum++;
                    break;
            }
        }
    }
    printf("%d %d\n",pl,sum);
    for(i = 1 ; i < N ; i++)
        printf("%d ",ma[i]);
    printf("%d\n",ma[N]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值