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,这样是单节点树形成的森林 ...
随机推荐
- docker 相关操作
docker-compose down //关闭所有容器 docker-compose up //开启所有容器 docker-compose restart //重启所有容器 单独更新某个容器时用脚本 ...
- django->基本操作和新建项目常用配置
一.安装django pip install django==2.1.5 -U #安装django/升级最新版本 二.创建.启动django项目 django-admin startproject m ...
- 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数
* 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...
- Mysql中的分库分表
mysql中的分库分表分库:减少并发问题分表:降低了分布式事务分表 1.垂直分表 把其中的不常用的基础信息提取出来,放到一个表中通过id进行关联.降低表的大小来控制性能,但是这种方式没有解决高数据量带 ...
- CVPR2020|3D-VID:基于LiDar Video信息的3D目标检测框架
作者:蒋天园 Date:2020-04-18 来源:3D-VID:基于LiDar Video信息的3D目标检测框架|CVPR2020 Brief paper地址:https://arxiv.org/p ...
- IE各版本CSS Hack(兼容性处理)语法速查表
为了兼容IE各个版本,需要在CSS中添加额外的代码,比如以前常用的_width.之所以工作,是因为浏览器会忽略不能解析的样式规则,因此举个例子来说,把_width写在width下面,对于非IE浏览器会 ...
- matlab计算LZ复杂度
我这个计算得14通道,每个通道截取3000个数据得复杂度,最后将计算得出得数据存储到本地txt文档中 function LZC(data) % 计算一维信号的复杂度 % data时间序列 % lzc: ...
- Extjs入门——环境配置
Extjs框架作为一个07年就上线的框架,虽然与现在的框架对比,显得十分臃肿.但是在针对企业内部引用系统上,它依旧能发挥出不错的效果.现在我接触到了Extjs,所以我准备写一个入门框架,简单的介绍Ex ...
- HTML+CSS教程(一)简介及其基本标签的使用方法
一.前端 HTML(结构):HyPer TEXT Markup LanguageCSS(样式): 样式就是对于结构的一种美化JavaScript(js: 行为/ 提供了用户和界面的交互方式)jQuer ...
- react typescript jest config (一)
1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...