CF-1102-B

思路

从1开始对元素染色,如果某个元素当前颜色已经染过了,那么寻找一个没有染过的颜色即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
@description: cf-1102-B
@author: universal42@163.com
@solution: greedy
*/
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LOCAL
#define cls ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define Mod 998244353
#define ll long long int
#define mset(a,b) memset(a,b,sizeof(a))
#define INF 1e9
const int maxn=5000+5;
string yes="YES\n";
string no="NO\n";
int a[maxn];
bool v[maxn][maxn];//元素a[i],颜色j是否出现过
int num[maxn];
int ans[maxn];
int main() {
cls;
#ifdef LOCAL
freopen("in.in","r",stdin);
#endif // LOCAL
int n,k;
cin>>n>>k;
bool flag=0;
for(int i=0;i<n;i++) {
cin>>a[i];
num[a[i]]++;
if(num[a[i]]>k) flag=true;
}
if(flag){
cout<<no;
return 0;
}
int color=1;
for(int i=0;i<n;i++){
int j=0;
while(v[a[i]][color]&&j<k) {
color=color%k+1;
j++;
}
v[a[i]][color]=true;
ans[i]=color;
color=color%k+1;
j=0;
}
cout<<yes;
for(int i=0;i<n;i++){
if(i) cout<<" ";
cout<<ans[i];
}
cout<<endl;
return 0;
}
Author: universal42
Link: https://universal4s.github.io/2019/02/17/cf-1102-b/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.