题目链接: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. 【processing】小代码3

    鼠标响应: mouseX, mouseY 鼠标的坐标 ---------------------------------------------- void setup() { size(,); sm ...

  2. Android随笔:属性

    android:padding表示给控件的周围加上补白,这样不至于让文本内容会紧靠在边缘上. android:singleLine 设置为true 表示让这个TextView 只能单行显示. andr ...

  3. 数据库IO简介

    IO有四种类型:连续读,随机读,随机写和连续写,连续读写的IO size通常比较大(128KB-1MB),主要衡量吞吐量,而随机读写的IO size比较小(小于8KB),主要衡量IOPS和响应时间.数 ...

  4. JavaScript的内置对象和浏览器对象

    在javascript中对象通常包括两种类型:内置对象和浏览器对象,此外,用户还可以自定义对象. 对象包含两个要素:1.用来描述对象特性的一组数据,也就是若干变量,通常称为属性.2.用来操作对象特性的 ...

  5. Android Programming: Pushing the Limits -- Chapter 4: Android User Experience and Interface Design

    User Stories Android UI Design 附加资源 User Stories: @.通过写故事来设计应用. @.每个故事只关注一件事. @.不同的故事可能使用相同的组件,因此尽早地 ...

  6. DedeCMS Error: (PHP 5.3 and above) Please set request_order

    部分使用PHP 5.3的主机可能会有下面的提示: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and ...

  7. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

  8. Java简明教程

    Java与C++比较概况 C++ Java class Foo { // 声明 Foo 类 public: int x; // 成员变量 Foo(): x() { // Foo 的构造函数Constr ...

  9. C++ 基础 构造函数的使用

  10. HDU 1227 Fast Food

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...