CodeForces - 1102B Array K-Coloring
B. Array K-Coloring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
describe
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
4 2
1 2 2 3
YES
1 1 2 2
5 2
3 2 1 2 3
YES
2 1 1 2 1
5 2
2 1 1 2 1
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.
这是一道简单的思维题,题意是说有w个数字,n种颜色,刷漆,每种颜色的油漆刷的元素必须不同。我写的应该算得上简单了,也容易理解,因为给的数值范围很小,所以也不用离散化直接用数组代表出现的次数,先用了一个ob[]数组,记录这个数字出现的次数,再用一个ans数组记录它的颜色,这时候你会发现这题好简单,出现1次颜色是1,出现两次颜色是2,出现次数超过给定颜色,不成立输出NO,好了你成功入坑了,这个题要求每个颜色至少使用一次,做了一个小操作,详情看代码!这是你觉得万事大吉了,WA四次终于明白,原来当颜色比数字多的时候,永远可能使每个颜色至少使用一次,现在题意跟坑清晰了,可以敲代码了。
AC code
#include<iostream>
#include<cstring>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
int ob[5050],ans[5050],ao[5050];
int main()
{
int n,w,x,flag=1,flag2=0;
mst(ans,0);
mst(ob,0);
cin>>n>>w;
if(n<w) flag=0;
for(int i=1;i<=n;i++){
cin>>x;
ob[x]++;
ans[i]=ob[x];
ao[ans[i]]++;
flag2=max(flag2,ans[i]);
if(ob[x]>w) flag=0;
}
int t=1;
if(flag){
while(flag2!=w){
if(ao[ans[t]]>=2){
ao[ans[t]]--;
ans[t++]=++flag2;
}
else t++;
}
cout<<"YES"<<endl;
for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
return 0;
}
else cout<<"NO";
return 0;
}
CodeForces - 1102B Array K-Coloring的更多相关文章
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- 网络流(最大流):CodeForces 499E Array and Operations
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m goo ...
- [LeetCode] K Inverse Pairs Array K个翻转对数组
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- codeforces gym 100971 K Palindromization 思路
题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...
- codeforces 1027 E. Inverse coloring (DP)
E. Inverse Coloring time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...
随机推荐
- C语言输出菱形
#include<stdio.h> #include<string.h> int main(){ int data[7][7] = {0}; for( ...
- JAVA中使用Date和SimpleDateFromat类表示时间
转自:https://www.imooc.com/code/2335 仅做个人学习保存之用,侵删! 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Dat ...
- lr事务
事务:transaction(性能里面的定义:客户机对服务器发送请求,服务器做出反应的过程) 用于模拟用户的一个相对完整的业务操作过程:如登录,查询,交易等操作(每次http请求不会用来作为一个事务) ...
- 数据结构和算法(Golang实现)(25)排序算法-快速排序
快速排序 快速排序是一种分治策略的排序算法,是由英国计算机科学家Tony Hoare发明的, 该算法被发布在1961年的Communications of the ACM 国际计算机学会月刊. 注:A ...
- C# 基础知识系列- 10 反射和泛型(二)
0. 前言 这篇文章延续<C# 基础知识系列- 5 反射和泛型>,继续介绍C#在反射所开发的功能和做的努力.上一篇文章大概介绍了一下泛型和反射的一些基本内容,主要是通过获取对象的类型,然后 ...
- php+ajax实现拖动滚动条分批加载请求加载数据
HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- 【three.js第二课】页面自适应
1.在[three.js第一课]的基础上加入以下代码,改变窗口大小时,页面内容会自适应 //加入事件监听器,窗口自适应 window.addEventListener('resize', functi ...
- 提升效率必备!8个超好用的Python内置函数
文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 吃着不想停 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- SQLi —— 逗号,空格,字段名过滤突破
前言 出于上海大学生网络安全大赛的一道easysql,促使我积累这篇文章.因为放了大部分时间在Decade和Babyt5上,easysql一点没看,事后看了WP,发现看不懂怎么回事,于是了解了一番. ...
- IOC 概念
转摘:https://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1.IOC的理论背景 2.什么是IOC 3.IOC也叫 ...