zoukankan      html  css  js  c++  java
  • codeforces 723C : Polycarp at the Radio

    Description

    Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.

    We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.

    Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.

    Input

    The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000).

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.

    Output

    In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.

    In the second line print the changed playlist.

    If there are multiple answers, print any of them.

    Examples
    Input
    4 2
    1 2 3 2
    Output
    2 1
    1 2 1 2
    Input
    7 3
    1 3 2 2 2 2 1
    Output
    2 1
    1 3 3 2 2 2 1
    Input
    4 4
    1000000000 100 7 1000000000
    Output
    1 4
    1 2 3 4
    Note

    In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.

    In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.

     

    正解:贪心

    解题报告:

      最优的次数显然,方案的话,每次暴力找到一个多余的,并把多出来的部分给别的缺少的即可。

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 using namespace std;
    14 typedef long long LL;
    15 const int inf = (1<<30);
    16 const int MAXN = 2011;
    17 int n,m,ans,zong;
    18 int a[MAXN],cnt[MAXN];
    19 bool pd[MAXN];
    20 
    21 inline int getint()
    22 {
    23     int w=0,q=0; char c=getchar();
    24     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    25     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    26 }
    27 
    28 inline void work(){
    29     n=getint(); m=getint(); for(int i=1;i<=n;i++) a[i]=getint();
    30     for(int i=1;i<=n;i++) if(a[i]<=m) cnt[a[i]]++;
    31     ans=n/m; printf("%d ",ans); bool flag=false;
    32     for(int i=1;i<=n;i++) {
    33     if(a[i]<=m && cnt[a[i]]>ans) {
    34         flag=false;        
    35         for(int j=1;j<=m;j++) {
    36         if(cnt[j]<ans) {
    37             flag=true;
    38             cnt[j]++; cnt[a[i]]--;a[i]=j; break;
    39         }
    40         }
    41         if(flag) zong++;
    42     }
    43     else if(a[i]>m) {
    44         flag=false;        
    45         for(int j=1;j<=m;j++) {
    46         if(cnt[j]<ans) {
    47             flag=true;
    48             cnt[j]++; a[i]=j;
    49             break;
    50         }
    51         }
    52         if(flag) zong++;
    53     }
    54     }
    55     printf("%d
    ",zong);
    56     for(int i=1;i<=n;i++) printf("%d ",a[i]);
    57 }
    58 
    59 int main()
    60 {
    61     work();
    62     return 0;
    63 }
  • 相关阅读:
    [转] 微服务架构
    vue拦截器实现统一token,并兼容IE9验证
    一句话 道出设计模式
    老话闲说,关于身份证号码的验证
    值得珍藏的HTTP协议详解
    微信内嵌H5网页 解决js倒计时失效
    使用JavaScript重定向URL参数
    Nodejs --我自己的学习笔记
    ONLY三行脚本 SQL数据恢复到指定时间点
    [转] 数据库加锁 sql加锁的
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5933858.html
Copyright © 2011-2022 走看看