B. Array K-Coloring
给定一个数组
要求对元素进行染色,满足相同元素颜色一定不同,且所有颜色要全部用完
#include<bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%I64d",&x);
#define int long long
vector<int> v[];
int ans[];
signed main()
{
int n,k,a;
sc(n);
sc(k);
int ma=;
for(int i=;i<n;i++){
sc(a);
v[a].push_back(i);
if(v[a].size()>k){
puts("NO");
return ;
}
ma=max(ma,(int)v[a].size());
}
int t=; for(int j=;j<=k;j++)
for(int i=;i<=;i++){
if(v[i].size()>){
ans[v[i][]]=j;
v[i].erase(v[i].begin());
t++;
}
if(n-t==k-j){
break;
}
}
puts("YES"); for(int i=;i<n;i++){
if(ans[i]==){
cout<<(k--)<<' ';
}
else
cout<<ans[i]<<' ';
}
}
B. Array K-Coloring的更多相关文章
- [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 ...
- 文件大小K、M、G、T
function size2mb($size,$digits=2){ //digits,要保留几位小数 $unit= array('','K','M','G','T','P');//单位数组,是必须1 ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- S5中新增的Array方法详细说明
ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...
- ES5中新增的Array方法详细说明
一.前言-索引 ES5中新增的不少东西,了解之对我们写JavaScript会有不少帮助,比如数组这块,我们可能就不需要去有板有眼地for循环了. ES5中新增了写数组方法,如下: forEach (j ...
- O(n)获得中位数及获得第K小(大)的数
首先,中位数问题可以归结为求 K=n/2的 第K小元素,并无明显区别. 第一种方法,用MaxHeap,大小为K的大顶堆,能够求出最小的K的元素,复杂度为O(n*logK). 当K较大时,复杂度会较高. ...
- 【leetcode】363. Max Sum of Rectangle No Larger Than K
题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...
- 第K顺序统计量
1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 查询无序列表中第K小元素
当需要在无需列表中寻找第k小的元素时,一个显然的方法是将所有数据进行排序,然后检索k个元素.这种方法的运行时间为O(n log(n)). 无序列表调用分区函数将自身分解成两个子表,其长度为i和n-i. ...
随机推荐
- PostgreSQL数据库表的内部结构
A page within a table contains three kinds of data described as follows: heap tuple(s) – A heap tupl ...
- Http服务器搭建(CentOS 7)
注意ip地址为: 虚拟机ip设置 TYPE="Ethernet"BOOTPROTO="static"NAME="enp0s3"DEVICE= ...
- C语言 --- 高级指针
1. 指针赋值: C语言允许使用赋值运算进行指针的赋值,前提是两个指针具有相同的类型. int i,*p,*q; p = &i; ...
- 响应式前端框架Bootstrap系列(11)分页
分页功能已经封装成一个独立的js文件,也是用bs完成的,名称为bootstrap-paginator.js. 使用前先导入文件 : <script src="../libs/boots ...
- 81. Search in Rotated Sorted Array II (JAVA)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 十、LaTex数学公式初步
- 【leetcode 461】. Hamming Distance
要求: 给定两个整数x和y,0 ≤ x, y < 231. 求x和y的汉明距离. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 ...
- OneDrive高速下载链接分享
目录 1. 下载帮助 2. 本文地址 3. 资源链接 4. 打赏&支持 5. 关于&联系我 1. 下载帮助 OneDrive下载教程,建议不了解的先看下: https://www.cn ...
- init_module - 初始化一条可加载模块的记录.
总览 #include <linux/module.h> int init_module(const char *name, struct module *image); 描述 init_ ...
- 第五小节之JAVA IO流
文件:文本文件是一种计算机文件,它是一种典型的顺序文件,其文件的逻辑结构又属于流式文件,特别的是指以ASCLL码方式(也称为文本方式)存储的文件,而更确切地说,英文.数字等字符存储的是ASCLL码,而 ...