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

#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;
}

CodeForces - 1102B Array K-Coloring的更多相关文章

  1. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  2. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  3. 网络流(最大流):CodeForces 499E Array and Operations

    You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m goo ...

  4. [LeetCode] K Inverse Pairs Array K个翻转对数组

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  5. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  6. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  7. codeforces 1027 E. Inverse coloring (DP)

    E. Inverse Coloring time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  9. 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring

    http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...

随机推荐

  1. Java第十六天,list接口

    List接口 1.三大特点: ① 有序.② 有索引. ③ 允许存在重复元素. 注意: ① 利用list接口的索引执行操作时,要防止索引越界引起的程序错误. 2.基本使用: 针对List接口有索引的特点 ...

  2. Linux 磁盘管理篇,目录管理(一)

    目录:     当我们在linux的ext2档案建立一个目录时,ext2会分配一个inode与至少一块Block给该目录,其中inode记录该目录在相关属性,并指向分配到在那块Block,而block ...

  3. "浮动按钮"组件:<fab> —— 快应用组件库H-UI

        <import name="fab" src="../Common/ui/h-ui/basic/c_fab"></import ...

  4. c++存储区

    全局变量与静态变量区.常量区.局部变量区(栈).动态存储区(堆).自由存储区 1.全局变量与静态变量区->存放全局变量.静态变量,程序运行结束后释放 2.常量区->存放常量 3.局部变量区 ...

  5. AJ学IOS(21)UIApplication设置程序图标右上⾓红⾊数字_联⺴指⽰器等

    AJ分享,必须精品 效果简介 UIApplication的运用,有很多相如:进⾏行⼀一些应⽤用级别的操作等等,打开网页,打开电话拨号和信息等.. 什么是UIApplication ● UIApplic ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  7. GeoGebra动态效果

    1.动态绘出f(x) 使用SlowPlot指令 2.GeoGebra的动态来源于两个:滑动条(Slider)和动点(Point) silder简单使用 动点的使用,右击,trace on,如果需要固定 ...

  8. Salesforce 产品 | 协同办公“大魔王”,Salesforce Quip的使用攻略!

    Salesforce帮助企业渡过疫情难关,支持在线远程办公.7.5亿美金收购的动态文档共享平台Quip,即刻开放给所有Salesforce老客户还有非营利组织免费使用至2020年9月30日. Quip ...

  9. Git敏捷开发--reset和clean

    reset 丢弃本地所有修改,强行和上游分支保持一致 git reset --hard HEAD 若仅丢弃某个文件的改动,利用checkout git checkout your_file clean ...

  10. @SessionAttributes 的使用

    @SessionAttributes 注解只用作用在 类 上,作用是将指定的 Model 的键值对保存在 session 中.可以让其他请求共用 session 中的键值对. 指定保存的属性名 作用是 ...