前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces 723C】Polycarp at the Radio 贪心

【Codeforces 723C】Polycarp at the Radio 贪心

作者头像
饶文津
发布2020-06-02 10:58:05
3100
发布2020-06-02 10:58:05
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

n个数,用最少的次数来改变数字,使得1到m出现的次数的最小值最大。输出最小值和改变次数以及改变后的数组。

最小值最大一定是n/m,然后把可以改变的位置上的数变为需要的数。

http://codeforces.com/problemset/problem/723/C

Examples

input

代码语言:javascript
复制
4 2
1 2 3 2

output

代码语言:javascript
复制
2 1
1 2 1 2 

input

代码语言:javascript
复制
7 3
1 3 2 2 2 2 1

output

代码语言:javascript
复制
2 1
1 3 3 2 2 2 1 

input

代码语言:javascript
复制
4 4
1000000000 100 7 1000000000

output

代码语言:javascript
复制
1 4
1 2 3 4
代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
int n,m,a[2005];
int pos[2005],cnt;
int cha[2005];
int ans,ans2;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    ans=n/m;
    printf("%d ",ans);

    for(int i=1;i<=m;i++){
        int num=0;
        int j;
        for(j=1;j<=n;j++){
            if(a[j]==i){
                num++;
                pos[j]=1;//代表j位置不能改变
            }
            if(num==ans)break;
        }
        if(num<ans){cha[i]=ans-num;ans2+=cha[i];}
    }
    int j=1;
    for(int i=1;i<=m;i++){
        for(int k=0;k<cha[i];k++){
            while(pos[j])j++;
            a[j++]=i;
        }
    }
    printf("%d\n",ans2);
    for(int i=1;i<=n;i++)
        printf("%d ",a[i]);
}
  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档