CodeForces - 1102B:Array K-Coloring(给数字涂颜色)

博客介绍了CodeForces的一道题目,1102B - Array K-Coloring,要求给定数组用k种颜色进行涂色,每种颜色至少有一个元素,且颜色内的元素不重复。文章提供了判断和解决思路,包括判断数字出现次数是否超过颜色种类,以及如何确保所有颜色都被使用到。并给出了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Array K-Coloring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
4 2
1 2 2 3
outputCopy
YES
1 1 2 2
inputCopy
5 2
3 2 1 2 3
outputCopy
YES
2 1 1 2 1
inputCopy
5 2
2 1 1 2 1
outputCopy
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.
题意:给n个数字、k种颜色,要求你给每个数字涂色,但是每种颜色中数字不能相同,并且颜色都要用完,当满足条件时,就输出YES以及涂的颜色,否则输出NO
思路:
1、题目要求每种颜色的数字不能相同,那么我们就可以先判断,如果一个数字出现的次数已经大于颜色的种类,那么必然不能满足条件
2、然后我们再满足条件颜色都要用完这个条件,只要n>k那么上面这个条件就可以满足
3、我们可能需要一个二维数组来记录每种颜色中的数字都有哪些,通过这个数组,我们就可以让接下来n-k个数字完成涂色

代码如下

#include <stdio.h>
#include <string.h>
#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值