Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)

题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色.
题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是否合法,然后对数字进行排序,从\([1,k]\)不断循环的去涂颜色,这样的好处是一定能保证相同数字涂的颜色不同,然后再按位置排序输出即可.
代码:
struct misaka{
int val;
int id;
int col;
}e[N]; int n,k;
map<int,int> mp;
int mx; bool cmp1(misaka a,misaka b){
return a.val<b.val;
} bool cmp2(misaka a,misaka b){
return a.id<b.id;
} int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
n=read();
k=read(); for(int i=1;i<=n;++i){
e[i].val=read();
e[i].id=i;
mp[e[i].val]++;
} for(auto w:mp){
mx=max(mx,w.se);
} if(mx>k) puts("NO");
else{
puts("YES");
sort(e+1,e+1+n,cmp1);
int cnt=1;
for(int i=1;i<=n;++i){
e[i].col=cnt;
cnt++;
if(cnt==k+1) cnt=1;
}
sort(e+1,e+1+n,cmp2);
for(int i=1;i<=n;++i){
printf("%d ",e[i].col);
}
} return 0;
}
Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)的更多相关文章
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #510 (Div. 2) C. Array Product
题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #531 (Div. 3)
A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...
- Codeforces Round #616 (Div. 2) B. Array Sharpening
t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...
随机推荐
- upload-labs 1-21关通关记录
0x01: 检查源代码,发现JS前端验证,关闭JS即可连接,或者手动添加.php,或者上传1.jpg,再抓包修改为php 0X02: if (($_FILES['upload_file']['type ...
- .NET探索平台条件编译
前言 今天偶然机会,翻了一下大学期间的书籍<C程序设计>,好吧,当我翻着翻着,翻到了符号常量(#define指令)中,是啊,这是一个预处理器指令,记得在Magicodes.IE中针对平台选 ...
- kubernets之secret资源
一 对于一些保密度比较高的文件,k8s又是如何存储的呢? 针对那些保密度比较高的配置文件,例如证书以及一些认证配置不能直接存储在configmap中,而是需要存储在另外一种资源中,需要对存储在里面的 ...
- 检查Mysql主从状态
.检查MySQL主从同步状态 #!/bin/bash USER=bak PASSWD=123456 IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e show s ...
- postgresql插件安装
postgresql安装包自带插件安装: 1.编译安装插件 # root用户 # postgresql安装过程省略 # 进入解压包的contrib目录 cd postgresql-10.6/contr ...
- 使用memory_profiler异常
在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误: C:\Users\Chen\Desktop\python_doc\第四模块课件>python ...
- playwright自动化项目搭建
这是关于playwright系列介绍的最后一篇.搭建基于 playwright 的自动化项目. GitHub地址: https://github.com/defnngj/playwright-pro ...
- 09--Docker 安装tomcat9
1.在hub.docker.com中获取tomcat拉取地址 docker pull tomcat:9.0.41-jdk8-corretto 2.查看Dockerfile 中WORKDIR 为/use ...
- 前端知识(二)03-Webpack-谷粒学院
目录 一.什么是Webpack 二.Webpack安装 1.全局安装 2.安装后查看版本号 三.创建项目 1.初始化项目 2.创建src文件夹 3.src下创建common.js 4.src下创建ut ...
- Jmeter非GUI界面对阿里云服务器压测
一.Jmeter非GUI界面 参数讲解 讲解:非GUI界面,压测参数讲解 -h 帮助 -n 非GUI模式 -t 指定要运行的 JMe ...