CodeForces 723C-Polycarp at the Radio(模拟 贪心 vector乱搞)

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.

题意:有N首编号为1~N的歌,有M个编号为1~M的合法演奏者,第二行N个ai,代表第i首歌由第ai位演奏者演奏,ai不一定就是1~M中的某一位。
我们需要使所有的歌曲演奏者均合法,并使得每一位演奏者演奏歌曲数量的最小值最大。
输出这个值和更改后的演奏者序列。

思路:很明显合法演奏者演奏歌曲数量最小值的最大值是N/M,创建一个合法演奏者演奏歌曲编号的动态数组和一个存储非法演奏者演奏歌曲序号的动态数组以及一个存储非法演奏者编号的动态数组。
遍历每一个合法演奏者,如果他演奏的歌曲数量不足,则优先从非法演奏者动态数组中抽调,如果抽调后数量仍然不足,则扫描非自己的合法演奏者,数量多的调到自己集合。(当然这一步可以另建数组存储数据,然后二分查找优化时间复杂度,但是鉴于数据那么小,就偷懒没写。)

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn=2005;
int A[maxn];//第i首歌曲由第A[i]个人演奏
vector<int>Less[maxn];//第i个人演奏的歌曲集合Less[i]
vector<int>more_point;//演奏序号超过M的歌曲编号集合
vector<int>more_num;//演奏序号超过M的序号
int main()
{
    int N,M;
    scanf("%d%d",&N,&M);
    for(int i=1; i<=N; i++)
    {
        int flag;
        scanf("%d",&flag);
        if(flag<=M)
            Less[flag].push_back(i);
        else
        {
            more_point.push_back(i);
            more_num.push_back(flag);
        }

    }
    int average=N/M;
    int num=0;
    for(int i=1; i<=M; i++)
    {
        if((int)Less[i].size()<average)
        {
            if((int)more_point.size()>0)
            {
                while((int)Less[i].size()!=average&&(int)more_point.size()!=0)
                {
                    Less[i].push_back(more_point.back());
                    more_point.pop_back();
                    more_num.pop_back();
                    num++;
                }
            }
            for(int j=1; j<=M&&(int)Less[i].size()<average; j++)
            {
                if(i!=j&&(int)Less[j].size()>average)
                {
                    while((int)Less[i].size()<average&&(int)Less[j].size()>average)
                    {
                        Less[i].push_back(Less[j].back());
                        Less[j].pop_back();
                        num++;
                    }
                }
            }
        }
    }
    for(int i=1; i<=M; i++)
    {
        for(int j=0; j<(int)Less[i].size(); j++)
        {
            A[Less[i][j]]=i;
        }
    }
    for(int i=0;i<(int)more_point.size();i++)//这一点不能漏掉
        A[more_point[i]]=more_num[i];
    printf("%d %d\n",average,num);
    for(int i=1; i<=N; i++)
        printf("%d ",A[i]);
    printf("\n");
    return 0;
}
这是一道 CodeForces 上的题目,题目编号为 749C,题目名称为 Voting。 题目描述: 有 $n$ 个人参加选举,选出一位领导人。每个人都会投票,你知道了每个人选择谁,并且可以知道选票中作废票和弃权票的数量。如果有一个人获得了半数以上的有效选票(即除去作废票和弃权票的票数),那么他将成为领导人。如果没有任何一个人获得半数以上的有效选票,则选举无效。 现在你可以修改任意数量的作废票或弃权票,使选举有效,并使你支持的候选人成为领导人。你需要最少的修改次数。 输入格式: 第一行包含三个整数 $n, a, b$,分别表示选民的数量,作废票的数量和弃权票的数量。 接下来 $n$ 行,每行包含一个字符串,表示每个人的投票情况。如果该字符串为 “YES” 或 “NO”,表示该选民对应的是弃权票;如果该字符串为 “POLL”, 表示该选民对应的是作废票;如果该字符串为其他字符串,则表示该选民对应的是有效选票,该字符串为该选民的投票对象。 输出格式: 如果无法通过修改使选举有效,则输出 -1;否则输出最少的修改次数,使得选举有效,并且你支持的候选人成为领导人。 数据范围: $1 \leq n \leq 2000, 0 \leq a, b \leq n$ 输入样例 #1: ``` 7 2 1 YES NO POLL YES YES YES NO ``` 输出样例 #1: ``` 1 ``` 样例 #1 解释: 总共有7个选民,其中弃权票有3个,作废票有2个,有效选票有2个。由于选民投票意见分散,无法确定一位领导人,因此选举无效。 我们可以将2张作废票修改为支持你所支持的候选人,这样你所支持的候选人将获得3张有效选票,超过半数,成为领导人。因此修改次数为1。 输入样例 #2: ``` 3 1 0 YES NO YES ``` 输出样例 #2: ``` 0 ``` 样例 #2 解释: 总共有3个选民,其中弃权票有1个,作废票有0个,有效选票有2个。你所支持的候选人获得了2张有效选票,超过半数,成为领导人。由于选民投票意见不分散,选举有效,无需修改任何票。因此修改次数为0。 算法1: (模拟) $O(n)$ 首先计算出除作废票和弃权票之外的票数,如果有一人的得票率超过50%,则选举有效,直接输出0。 否则,需要计算出至少需要修改多少张作废票或弃权票,才能使选举有效,并且支持你所支持的候选人成为领导人。 具体来说,我们可以考虑枚举需要修改的作废票和弃权票的数量,假设需要修改 $i$ 张作废票和 $j$ 张弃权票,使得选举有效。那么,你所支持的候选人需要得到至少 $\lceil \frac{n}{2} \rceil$ 张有效选票。我们可以通过统计当前你所支持的候选人得到的有效选票数 $cnt$,以及当前已经修改的作废票和弃权票的数量 $a'$ 和 $b'$,来判断是否存在一组解 $(i, j)$,使得选举有效。 具体来说,如果当前得票数 $cnt + i \ge \lceil \frac{n}{2} \rceil$,那么选举有效,输出 $i+j$ 即可。如果 $(\lceil \frac{n}{2} \rceil - cnt) \le a' + i \le n-b'-j$,那么也存在一组解 $(i,j)$,使得选举有效,输出 $i+j$ 即可。 如果枚举完所有情况,都无法使选举有效,那么输出 -1。 时间复杂度:$O(n^2)$ C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值