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. 基于 mpvue 框架的小程序选择控件,支持单列,多列,联动

    最近在学着写mpvue小程序,在做选择控件时候遇到了点问题,按照微信小程序方法picker,很不方便! 在网上搜到一个很好用的组件下面给大家分享: 组件说明文档链接:https://go.ctolib ...

  2. C语言atoi函数

    目录 1.包含头文件 2.函数声明 3.功能说明 4.示例 5.其它说明 6.版权声明 C语言提供了一系列函数把字符串转换为整数:atoi.atol.atoll和atoq. 1.包含头文件 #incl ...

  3. go语言goroutine

    Go语言goroutine 在别的语言里想要在一个程序中实现多任务,如python,python实现多任务可以使用多进程.多线程.携程.但多进程占用资源,多线程无法发挥多核的优势(GIL),pytho ...

  4. 2015蓝桥杯五星填数(C++C组)

    题目:五星填数 如[图1.png]的五星图案节点填上数字:1~12,除去7和11.要求每条直线上数字和相等.如图就是恰当的填法.请你利用计算机搜索所有可能的填法有多少种.注意:旋转或镜像后相同的算同一 ...

  5. Python爬虫 ---scrapy框架初探及实战

    目录 Scrapy框架安装 操作环境介绍 安装scrapy框架(linux系统下) 检测安装是否成功 Scrapy框架爬取原理 Scrapy框架的主体结构分为五个部分: 它还有两个可以自定义下载功能的 ...

  6. Python爬虫系列(五):分析HTML结构

    今晚,被烦死了.9点多才下班,就想回来看书学习,结果被唠叨唠叨个小时,我不断喊不要和我聊天了,还反复说.我只想安安静静看看书,学习学习,全世界都不要打扰我 接着上一个讨论,我们今晚要分析HTML结构了 ...

  7. tf.nn.relu6 激活函数

    tf.nn.relu6(features,name=None) 计算校正线性6:min(max(features, 0), 6) 参数: features:一个Tensor,类型为float,doub ...

  8. **********Prometheus(三)******************

    通过centos7.x来部署Prometheus ####同步时间,否则后面监控会有异常!!!!!####### 1. 创建文件夹,上传软件包.解压并将prometheus promtool两个命令复 ...

  9. VCL安装有哪几种方法?

    不是由Borland 提供的组件叫第三方组件: 就目前常见的各种形式的组件的安装方法介绍一下. 1.只有一个DCU文件的组件.DCU文件是编译好的单元文件,这样的组件是作者不想把源码公布.一般来说,作 ...

  10. NCTF2018_easy_audit->coding_breaks

    easy_audit 题目源码 <?php highlight_file(__FILE__); error_reporting(0); if($_REQUEST){ foreach ($_REQ ...