codeforces 723C : Polycarp at the Radio
Description
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.
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.
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.
4 2
1 2 3 2
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
2 1
1 3 3 2 2 2 1
4 4
1000000000 100 7 1000000000
1 4
1 2 3 4
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.
正解:贪心
解题报告:
最优的次数显然,方案的话,每次暴力找到一个多余的,并把多出来的部分给别的缺少的即可。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
int n,m,ans,zong;
int a[MAXN],cnt[MAXN];
bool pd[MAXN]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void work(){
n=getint(); m=getint(); for(int i=;i<=n;i++) a[i]=getint();
for(int i=;i<=n;i++) if(a[i]<=m) cnt[a[i]]++;
ans=n/m; printf("%d ",ans); bool flag=false;
for(int i=;i<=n;i++) {
if(a[i]<=m && cnt[a[i]]>ans) {
flag=false;
for(int j=;j<=m;j++) {
if(cnt[j]<ans) {
flag=true;
cnt[j]++; cnt[a[i]]--;a[i]=j; break;
}
}
if(flag) zong++;
}
else if(a[i]>m) {
flag=false;
for(int j=;j<=m;j++) {
if(cnt[j]<ans) {
flag=true;
cnt[j]++; a[i]=j;
break;
}
}
if(flag) zong++;
}
}
printf("%d\n",zong);
for(int i=;i<=n;i++) printf("%d ",a[i]);
} int main()
{
work();
return ;
}
codeforces 723C : Polycarp at the Radio的更多相关文章
- Codeforces 723C. Polycarp at the Radio 模拟
C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- CodeForces 723C Polycarp at the Radio (题意题+暴力)
题意:给定 n 个数,让把某一些变成 1-m之间的数,要改变最少,使得1-m中每个数中出现次数最少的尽量大. 析:这个题差不多读了一个小时吧,实在看不懂什么意思,其实并不难,直接暴力就好,n m不大. ...
- Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心
C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- 【23.48%】【codeforces 723C】Polycarp at the Radio
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 723C】Polycarp at the Radio 贪心
n个数,用最少的次数来改变数字,使得1到m出现的次数的最小值最大.输出最小值和改变次数以及改变后的数组. 最小值最大一定是n/m,然后把可以改变的位置上的数变为需要的数. http://codefor ...
- Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心
http://codeforces.com/contest/723/problem/C 题目是给出一个序列 a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲 ...
- cf723c Polycarp at the Radio
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be re ...
- codeforces 727F. Polycarp's problems
题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...
随机推荐
- Flex ActionScript版本的Map类型
ActionScript中没有Map类型,因为Object就相当于Map了.Object的属性相当于key,值相当于value. 也就是说,没有必要有Map类型.但是,这样做,也会带来一些问题,造成不 ...
- 添加JSON Data到已经存在的JSON文件中
早上在学习<Post model至Web Api创建或是保存数据>http://www.cnblogs.com/insus/p/4343833.html ,如果你第二添加时,json文件得 ...
- 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参
转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...
- 向jboss写入服务器日志
实际开发中,记录日志是常用的功能,jboss默认情况下已经记录了很多运行日志,如果开发人员要手动在server.log中写入日志,可以参考下面的方法: package utils; import ja ...
- 802.11 对于multicast 和 broadcast的处理
ethernet内部会有broadcast 和 multicast.这两种包都是一个STA向多个STA发包. 当没有wifi存在的时候,LAN口之间的broadcast 和 multicast是可靠转 ...
- rsync实现负载均衡集群文件同步,搭建线上测试部署环境
闲来无事,搭建一个负载均衡集群,至于负载均衡集群搭建过程,找时间写下.这次主要写集群之间的文件同步,以及线上测试环境的搭建. 笔者看过很多公司都没有线上测试环境,真是崩溃了,不造怎么确保线上线下环境一 ...
- FileShare枚举的使用(文件读写锁)
开发过程中,我们往往需要大量与文件交互,但往往会出现很多令人措手不及的意外,所以对普通的C#文件操作做了一次总结,问题大部分如下: 1:写入一些内容到某个文件中,在另一个进程/线程/后续操作中要读取文 ...
- Social Emotional Computing -情感模式与价值变化
情感模式与价值变化 第七节 情感模式与价值变化 情感与价值的关系是主观与客观的关系,人的情感不管多么飘忽不定,都可以找到它的价值对应物,情感的任何变化都可以从价值关系的变动中找到它的客观动因,情感的不 ...
- javascript 函数声明与函数表达式的区别
先看一段代码 var f = function g() { return 1; }; if (false) { f = function g(){ return 2; }; } alert(g()); ...
- <button>属性,居然才发现
今天学习了一个表单验证的程序,发现点了一个<botton>之后,表单里面的所有输入框的内容,统统都消失了,后来一查看源代码,我发现居然是<botton>里面的属性如下: < ...