DAY4.1 A - Array K-Coloring

A - Array K-Coloring

You are given an array aa consisting of nn integer numbers.

You have to color this array in kk colors in such a way that:

Each element of the array should be colored in some color;
For each ii from 11 to kk there should be at least one element colored in the ii-th color in the array;
For each ii from 11 to kk all elements colored in the ii-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,…cnc1,c2,…cn, where 1≤ci≤k1≤ci≤k and cici is the color of the ii-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 nn and kk (1≤k≤n≤50001≤k≤n≤5000) — the length of the array aa and the number of colors, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤50001≤ai≤5000) — elements of the array aa.

Output
If there is no answer, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c1,c2,…cnc1,c2,…cn, where 1≤ci≤k1≤ci≤k and cici is the color of the ii-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

思路&过程

纯模拟,先用没用过的颜色,都用完了就挑同一种没用过的,如果没有,则无法构造。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int a[5010],c[5010];
	static bool vis[5010][5010];
	memset(vis,true,sizeof(vis));
	int n,k,i,j;
	bool t;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<n;i++)
	{
		t=false;
		for(j=1;j<=k;j++)
		{
			if(vis[j][0])
			{
				vis[j][0]=false;
				vis[j][a[i]]=false;
				t=true;
				c[i]=j;
				break;
			}
		}
		for(j=1;j<=k && !t;j++)
		{
			if(vis[j][a[i]])
			{
				vis[j][a[i]]=false;
				t=true;
				c[i]=j;
				break;
			}
		}
		if(!t)
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	for (i=1;i<=k;i++)
	{
		if(vis[i][0])
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	cout<<"YES"<<endl;
	for(i=0;i<n;i++) cout<<c[i]<<" ";
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值