Codeforces Round #375 (Div. 2) - C
题目链接: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的更多相关文章
- Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...
- Codeforces Round #375 (Div. 2) - B
题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
- Codeforces Round #375 (Div. 2) - A
题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟
B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...
- 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 ...
- 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个人演唱歌曲 ...
随机推荐
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
- 通过configSource提高web.config配置灵活性
很多时候我们会有这样的情况,开发环境和测试环境中的配置文件是不一样的,最明显的就是数据库连接串,这样,每次我们发布一个测试版本,都要手动去修改一下配置文件,是不是很麻烦的说.其实利用web.confi ...
- struts2.0整合json
框架:struts2.0+hibernate2+spring 今天写代码时,需要用到json,我就直接加了两个jar包:json-lib-2.1-jdk15.jar,struts2-json-plug ...
- objective-c数组
1 #pragma mark -----------数组的初始化方式-------------- 2 // insert code here... 3 // NSLo ...
- PCH文件配置路径
"项目" >> Build Settings >> Preflx Header >> $(SRCROOT)/项目名/XXX.pch
- September 21st 2016 Week 39th Wednesday
Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...
- NYOJ题目611练练
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJ1CAIAAACgqiqJAAAgAElEQVR4nO3du27jSp4HYL+Ecj2IYz
- 关于安装Ubuntu后触摸板无法使用的解决方案
安装了Ubuntu后发现触摸板无法使用,以为是修改了安装文件导致(之前拿安装源文件做了小实验),于是重装,之后触摸板仍无法使用,在一个长满小广告的页面上找到了解决方案. 以下是原文章内容: 最近突然发 ...
- postgresql集群方案参考答案
PostgreSQL配置Streaming Replication集群 http://www.cnblogs.com/marsprj/archive/2013/03/04/2943373.html p ...
- web项目没有run on server时..
文章转载至:http://blog.csdn.net/hongchangfirst/article/details/7722703 web项目没有run on server 1.首先确保正确安装Tom ...