题目链接:http://codeforces.com/contest/723/problem/C

题意:给定长度为n的一个序列。还有一个m。现在可以改变序列的一些数。使得序列里面数字[1,m]出现次数最小的次数尽可能大。输出出现次数最小的次数。需要改变序列多少次。改变后的序列。

思路:题意有点难懂。可以发现出现次数最小的次数最大一定是(n/m).所以枚举数字[1,m]如果数字i(1<=i<=m)出现次数小于n/m,则要把序列某个数替换成i,怎么选择用哪些数来替换。应该选序列值大于m的或者序列值在[1,m]范围但是出现次数大于cnt的来替换。 注意题目没有要求一定要把大于m的数都变成[1,m]。

比如数据

4 3

1 2 3 4

输出结果是

1 0

1 2 3 4

因为不管把4替换成[1,3]的哪个都不会使出现次数最小变大。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<map>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int INF = 0x3f3f3f3f;
const int MAXN = + ;
int n, m, a[MAXN];
map<int, int>mp;
int main(){
while (~scanf("%d%d", &n, &m)){
mp.clear();
for (int i = ; i <= n; i++){
scanf("%d", &a[i]); mp[a[i]]++;
}
int changecnt = ; int cnt = n / m;
for (int i = ; i <= m ; (mp[i]>=cnt?i++:i)){ //枚举[1,m]
if (mp[i] < cnt){ //出现次数小于n/m
int maxcnt = , maxid = ; //找到一个出现次数大于n/m或者值大于m的来替换
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++){
if (it->first <= m){
if (it->second>cnt&&it->second > maxcnt){
maxcnt = it->second; maxid = it->first;
}
}
else{
if (it->second > maxcnt){
maxcnt = it->second; maxid = it->first;
}
}
}
for (int j = ; j <= n; j++){
if (a[j] == maxid){
a[j] = i; break;
}
}
mp[i]++; mp[maxid]--; changecnt++;
}
}
printf("%d %d\n", n / m, changecnt);
for (int i = ; i <= n; i++){
printf("%d", a[i]);
printf(i == n ? "\n" : " ");
}
}
return ;
}

Codeforces Round #375 (Div. 2) - C的更多相关文章

  1. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

  2. Codeforces Round #375 (Div. 2) - B

    题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...

  3. Codeforces Round #375 (Div. 2) - A

    题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...

  4. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  5. Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径

    E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...

  6. Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心

    D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...

  7. Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟

    B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...

  8. Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题

    A. The New Year: Meeting Friends 题目连接: http://codeforces.com/contest/723/problem/A Description There ...

  9. 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个人演唱歌曲 ...

随机推荐

  1. Mathematics:Raising Modulo Numbers(POJ 1995)

    阶乘总和 题目大意:要你算一堆阶乘对m的模... 大水题,对指数二分就可以了... #include <iostream> #include <functional> #inc ...

  2. Mathematics:DNA Sorting(POJ 1007)

    DNA排序 题目大意:给定多个ACGT序列,按照字母顺序算出逆序数,按逆序数从小到大排列 这题其实很简单,我们只要用一个归并排序算逆序数,然后快排就可以了(插入排序也可以,数据量不大),但是要注意的是 ...

  3. C# 对象深度拷贝

    转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...

  4. Android 6.0的运行时权限

    原文  http://droidyue.com/blog/2016/01/17/understanding-marshmallow-runtime-permission/ 主题 安卓开发   Andr ...

  5. Yahoo!网站性能最佳体验的34条黄金守则(转载)

    1.       尽量减少HTTP请求次数  终端用户响应的时间中,有80%用于下载各项内容.这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通过减少页面中的元素可以减少HTTP请求的次数 ...

  6. 关于jQuery新的事件绑定机制on()的使用技巧

    关于jQuery新的事件绑定机制on()的使用技巧 http://www.jb51.net/article/36064.htm 本篇文章介绍了,关于jQuery新的事件绑定机制on()的使用技巧.需要 ...

  7. Git命令之从GitHub上下载开源项目

    1,先在本地创建一个目录,作为本地仓库,如: 2,使用Git init 初始化仓库,git初始化完成后,会生成一个隐藏的git文件如: 3,clone Git项目,如: 4,这个项目就是合Github ...

  8. TIME_WAIT连接过多解决办法

    问题起因: 自己开发了一个服务器和客户端,通过短连接的方式来进行通讯,由于过于频繁的创建连接,导致系统连接数量被占用,不能及时释放.看了一下18888,当时吓到了. 现象: 1.外部机器不能正常连接S ...

  9. Mysql获取时间

    select now() 当前时间;   SELECT curdate() 当天日期; select date_sub(curdate(),interval 1 day) 前一天日期; select ...

  10. Clr Via C#读书笔记---计算限制的异步操作

    线程池基础 1,线程的创建和销毁是一个昂贵的操作,线程调度以及上下文切换耗费时间和内存资源. 2,线程池是一个线程集合,供应你的用程序使用. 3,每个CLR有一个自己的线程池,线程池由CLR控制的所有 ...