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;
}