【23.48%】【codeforces 723C】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/m;
因为平均分才能使得最小的最大。
设k = n/m;
然后就是要维护出现次数的最小值大于等于k了;
具体的办法是把那些原序列出现次数大于k且数字本身小于等于m的数和那些数字本身大于m的数字加入到一个giver队列中。
表示这些数字都能转换成出现次数小于k的那些数字。以此来维护(1..m)权值范围内的数字出现次数的最小值大于等于k;
具体的操作看代码
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 2999;
struct data1
{
int what;
int num;
int idx;
};
int n, m, a[MAXN], step = 0;
int dic[MAXN];
queue <data1> q, g;
bool emp[MAXN] = { 0 };
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] <= m)
dic[a[i]]++;
}
int k1;
k1 = n / m;
for (int i = 1; i <= m; i++)
if (dic[i] < k1)
{
data1 temp;
temp.what = i;
temp.num = dic[i];
q.push(temp);
}
for (int i = 1; i <= n; i++)
if (a[i] <= m)
{
if (dic[a[i]] > k1)
{
data1 temp;
temp.what = a[i];
temp.num = 1;
temp.idx = i;
g.push(temp);
}
}
else
{
data1 temp;
temp.what = a[i];
temp.num = 1;
temp.idx = i;
g.push(temp);
}
while (!q.empty())
{
data1 temp = q.front();
data1 giver = g.front();
if (giver.what <= m && emp[giver.what])//如果giver已经从大于k变成了k则emp[giver]会变成true,表示不能再用它转换了。
{
g.pop();
continue;
}
a[giver.idx] = temp.what;
step++;
if (giver.what <= m)
{
dic[giver.what]--;
if (dic[giver.what] == k1)//从大于k变成等于k则不能在用这个giver了
emp[giver.what] = true;
}
g.pop();
temp.num++;
if (temp.num == k1)
q.pop();
else
q.front().num = temp.num;
}
printf("%d %d\n", k1, step);
for (int i = 1; i <= n - 1; i++)
printf("%d ", a[i]);
printf("%d\n", a[n]);
return 0;
}
【23.48%】【codeforces 723C】Polycarp at the Radio的更多相关文章
- 【Codeforces 723C】Polycarp at the Radio 贪心
n个数,用最少的次数来改变数字,使得1到m出现的次数的最小值最大.输出最小值和改变次数以及改变后的数组. 最小值最大一定是n/m,然后把可以改变的位置上的数变为需要的数. http://codefor ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【23. 合并K个排序链表】【困难】【优先队列/堆排序】
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...
- 【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【16.23%】【codeforces 586C】Gennady the Dentist
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.26%】【codeforces 747D】Winter Is Coming
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- Android中级第九讲--相机调焦
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 相机调焦:原理,使用竖直seekbar,根据用户拖拉来获得距离 ...
- 《Java设计模式》之桥接模式
Bridge模式的概念 Bridge 模式是构造型的设计模式之中的一个.Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任.它的主要特点是把抽象(abst ...
- 01-Jvm 内存区域复习笔记
Java内存区域 1.程序计数器(Program Counter Register) 在虚拟机中一块较小的内存空间.它的作用能够看做是当前线程所运行的字节码的行号指示 ...
- php7 兼容 MySQL 相关函数
php7 兼容 MySQL 相关函数 PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql http://PHP.net/manual/zh/mysqlinfo ...
- 虚拟机上的企业网络管理系统(cisco works 2000安装配置)
虚拟机上的企业网络管理系统 北京 李晨光 相关文章 Cisco Works 2000 网络管理软件安装.配置全过程 http://you.video.sina.com.cn/b/18168631-14 ...
- monkey基础知识(二)
- 洛谷 P1724 东风早谷苗
洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...
- 洛谷 P1755 斐波那契的拆分
P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...
- 让ie6 7 8 9支持原生html5 websocket
让ie6 7 8 9支持原生html5 websocket 从github上的 web-socket-js(socket.io好像也是用这个做的他们的flash替代传输方式)改过来的.不过值得 ...
- 1.一个WEB应用的开发流程
先说项目开发过程中团队人员的分工协作. 一.人员安排 毕业至今的大部分项目都是独立完成,虽然也有和其他同事协作的时候,但自认为对团队协作的了解和认知都还有所欠缺.很清楚团队协作的重要性,但尚未有很好的 ...