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. docker+nginx 微信支付回调

    制作微信支付发现回调有问题: docker-compose中接口要映射对应地址 然后再进行访问

  2. spring boot 学习(一)——在idea建立第一个spring boot项目

    1.打开idea->点击file->点击new->点击project->点击Spring Initializr->点击default 2.这是springboot的启动类 ...

  3. AD颗粒化密码规则策略

    我们在第一次设定密码规则的时候,通常会在根节点或者默认组策略中设置 如果,我们在后期运维过程中,有一些特殊用户需要设置额外的密码策略,我们要如何操作呢? 可能,有些同学会在这些特殊用户对应的OU下在创 ...

  4. python基础之函数详解

    Python基础之函数详解 目录 Python基础之函数详解 一.函数的定义 二.函数的调用 三.函数返回值 四.函数的参数 4.1 位置参数 4.2 关键字参数 实参:位置实参和关键字参数的混合使用 ...

  5. Gitflow分支管理策略

    Gitflow存在两个记录项目历史的分支 Master分支:存储(官方的,正式的)项目发布历史记录的分支. develop分支:充当功能的集成分支. Develop分支将包含项目的完整历史记录,而ma ...

  6. 一起了解 .Net Foundation 项目 No.24

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Xamarin.Mobil ...

  7. 原创Hbase1.2.1集群安装

    [hadoop@Hmaster install]$ tar -zxvf hbase-1.2.1-bin.tar.gz -C ~ [hadoop@Hmaster install]$vi ~/.bash_ ...

  8. Android | 教你如何在安卓上实现通用卡证识别,一键各种卡绑定

    目录 前言 通用卡证识别的应用场景 如何使用通用卡证识别服务 集成通用卡证识别服务的关键流程 开发实战 1 开发准备 1.1 在项目级gradle里添加华为maven仓 1.2 在应用级的build. ...

  9. 4.加密与token(node+express)

    一. 敏感数据加密1.安装并引入中间件     npm install utility     const utils = require('utility')2.加密方法     function ...

  10. golang实现并发爬虫三(用队列调度器实现)

    欲看此文,必先可先看: golang实现并发爬虫一(单任务版本爬虫功能) gollang实现并发爬虫二(简单调度器) 上文中的用简单的调度器实现了并发爬虫. 并且,也提到了这种并发爬虫的实现可以提高爬 ...