Array K-Coloring

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
Input
4 2
1 2 2 3
Output
YES
1 1 2 2
Input
5 2
3 2 1 2 3
Output
YES
2 1 1 2 1
Input
5 2
2 1 1 2 1
Output
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.
思路:先把所有的k种颜色都涂抹上去,再来判断那些相同的已经被涂抹,在这里我用的是二维数组visit[i][arr[i]] 第i种颜色涂抹在了arr[i]位置上
我错误原因是没有把k种颜色利用完,只想着相同数字填不同颜色,visit[j+1][arr[i]] = 1;填完之后没有做标记
代码:

#include<iostream>
using namespace std;
int arr[6000];
int sum[6000];
int b[6000];
int visit[6000][6000];
int main()
{
    int n,k;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
    {
        cin >> arr[i];
        sum[arr[i]]++;
    }
    for(int i = 0; i < n; i++)
    {
        if(sum[arr[i]]>k)
        {
            cout << "NO";
            return 0;
        }
    }
    if(n < k)
    {
        cout << "NO";
        return 0;
    }
    
    for(int i = 0; i < n; i ++)
    {
        if(i < k)
        {
            b[i] = i+1;
            visit[i+1][arr[i]] = 1;
        }
        else
        {
            for(int j = 0; j < k; j++)
            {
                if(visit[j+1][arr[i]] == 0)
                {
                    b[i] = j+1;
                    visit[j+1][arr[i]] = 1;
                    break;
                }
            }
        }
        
    }
    cout << "YES" << endl;
    for(int i = 0; i < n; i++)
    {
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值