题目链接: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. Divide and conquer:Drying(POJ 3104)

    烘干衣服 题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服? ...

  2. 【python】入门学习(十)

    #入门学习系列的内容均是在学习<Python编程入门(第3版)>时的学习笔记 统计一个文本文档的信息,并输出出现频率最高的10个单词 #text.py #保留的字符 keep = {'a' ...

  3. 让view 覆盖导航栏

    当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...

  4. .Net SqlDbHelper

    using System.Configuration; using System.Data.SqlClient; using System.Data; namespace ExamDAL { clas ...

  5. 使用Ajax上传图片到服务器(不刷新页面)

    有时候我们需要上传图片时不刷新页面,那么Ajax就是很好的东西哦.之前在网上找了很多的资料都不对,不是这里就是那里错,这是本人亲自测试了的哈,是没有问题的,若有不足之处希望指正.我用的.net,对了这 ...

  6. objective-c可变数组

     1 #pragma mark ---------------可变数组-----------------  2 //        可以在数组里面进行增删改的操作  3 //  4 //        ...

  7. 108. Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. idea 用maven骨架生成项目速度慢的问题

    使用mvn archetype:generate命令时,加上-DarchetypeCatalog=local archetypeCatalog=local

  9. Android 天气曲线

    参考:http://blog.csdn.net/qy274770068/article/details/51560148

  10. 6个原因说服你选择PostgreSQL9.6

    PostgreSQL9.6在前些日子发布了, 社区为该版本的重大更新付诸良多, 发布日志一如既往的长,我挑选了6个重要的更新, 这些或许能够帮助你更好的使用PostgreSQL. 并行: 并行应该是这 ...