Codeforces Round #531 (Div. 3) B. Array K-Coloring(思维)(vector map)

本文探讨了Array K-Coloring问题,旨在通过k种颜色对n个整数进行着色,确保每种颜色至少使用一次且同一颜色不重复用于相同数值。文章详细阐述了解决方案的思路与步骤,包括条件判断、颜色分配策略及代码实现。

Array K-Coloring:

题目大意:(文末有原题)

给出n个整数,用k种颜色给这n个整数着色,要求:

1. 每个整数都有一种颜色;

2. 每种颜色至少要涂一个整数;

3. 每种颜色不能为相同的整数着色;

判断能否为每一个整数都着色;

思路:

首先判断是否能够实现;

不能实现的条件:

存在有x个相同整数,并且x > k;(因为k个颜色,每个颜色不能涂相同的整数,如果x>k,代表会有x-k个该整数没法着色)

 

如果能实现就进行下列操作:

因为要每个颜色至少要涂一个整数,则先遍历,让每个颜色都先涂一个整数,并且保存已经涂过的整数的值;

之后从第一个(i = 0)颜色开始遍历,每一次再对剩下还没着色的数遍历,如果这个颜色之前没有涂过这个整数,就用这一种颜色涂它,否则跳过;

直到所有的数都被着色;

代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <map>
using namespace std;

const int maxn = 1e4 + 10;
int a[maxn], ans[maxn];

int main() {
	int n, k, sum = 0, max = -100;
	cin >> n >> k;
	map<int, int> m;
	vector<int> vis[maxn];	
	memset(ans, 0, sizeof(ans));
	
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		m[a[i]]++;	//用来统计一个整数的数量 
		if(m[a[i]] > max) {  
			max = m[a[i]];
		}
	}
	
	if(max > k)
		cout << "NO" << endl;
	}else {
		cout << "YES" << endl;
		for(int i = 0; i < k; i++) {
			vis[i].push_back(a[i]);		//用来记录用颜色i涂过的整数 
			ans[i] = i + 1;
		}
		for(int j = 0; j < k; j++) {
			if(j >= k) j -= k;
			
			for(int i = k; i < n; i++) {
				if(!ans[i]) {
					int d = 0;
					for(int l = 0; l < vis[j].size(); l++) {
						if(a[i] == vis[j][l]) {
							d = 1;
							break;
						}
					}
					if(d) continue;
					ans[i] = j + 1;
					vis[j].push_back(a[i]);
					sum++;
				}
			}
			
			if(sum + k >= n) 
				break;
		}
		for(int i = 0; i < n; i++) {
			if(i == n - 1) cout << ans[i] << endl;
			else cout << ans[i] << " "; 
		}
	}
	
	return 0;
}

原题:

题目:

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.

输入:

The first line of the input contains two integers n and k (1≤k≤n≤5000 ) — the length of the array aa 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 .

输出:

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.

样例:

Input:

4 2
1 2 2 3

Output:

YES
1 1 2 2
Input:

5 2
3 2 1 2 3

Output:

YES
2 1 1 2 1

Input:

5 2
2 1 1 2 1

Output:

NO

### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以根据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值