前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces - 1102B Array K-Coloring

CodeForces - 1102B Array K-Coloring

作者头像
风骨散人Chiam
发布2020-10-28 10:12:12
3420
发布2020-10-28 10:12:12
举报
文章被收录于专栏:CSDN旧文

B. Array K-Coloring time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output

describe

You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that:

Each element of the array should be colored in some color; For each i from 1 to k there should be at least one element colored in the i-th color in the array; For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c1,c2,…cn, where 1≤ci≤k and ci is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.

Input

The first line of the input contains two integers n and k (1≤k≤n≤5000) — the length of the array a and the number of colors, respectively.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤5000) — elements of the array a.

Output

If there is no answer, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c1,c2,…cn, where 1≤ci≤k and ci is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.

Examples

4 2 1 2 2 3 YES 1 1 2 2 5 2 3 2 1 2 3 YES 2 1 1 2 1 5 2 2 1 1 2 1 NO Note

In the first example the answer 2 1 2 1 is also acceptable.

In the second example the answer 1 1 1 2 2 is also acceptable.

There exist other acceptable answers for both examples.

这是一道简单的思维题,题意是说有w个数字,n种颜色,刷漆,每种颜色的油漆刷的元素必须不同。我写的应该算得上简单了,也容易理解,因为给的数值范围很小,所以也不用离散化直接用数组代表出现的次数,先用了一个ob[]数组,记录这个数字出现的次数,再用一个ans数组记录它的颜色,这时候你会发现这题好简单,出现1次颜色是1,出现两次颜色是2,出现次数超过给定颜色,不成立输出NO,好了你成功入坑了,这个题要求每个颜色至少使用一次,做了一个小操作,详情看代码!这是你觉得万事大吉了,WA四次终于明白,原来当颜色比数字多的时候,永远可能使每个颜色至少使用一次,现在题意跟坑清晰了,可以敲代码了。 AC code

代码语言:javascript
复制
#include<iostream>
#include<cstring>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
int ob[5050],ans[5050],ao[5050];
int main()
{
    int n,w,x,flag=1,flag2=0;
    mst(ans,0);
    mst(ob,0);
    cin>>n>>w;
    if(n<w) flag=0;
    for(int i=1;i<=n;i++){
        cin>>x;
        ob[x]++;
        ans[i]=ob[x];
        ao[ans[i]]++;
        flag2=max(flag2,ans[i]);
        if(ob[x]>w) flag=0;
    }
    int t=1;
        if(flag){
        while(flag2!=w){
                if(ao[ans[t]]>=2){
                    ao[ans[t]]--;
                    ans[t++]=++flag2;
                }
                else t++;
            }
            cout<<"YES"<<endl;
            for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
            return 0;
        }
        else cout<<"NO";
        return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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