CodeForces 509B Painting Pebbles

本文探讨了给定数量石子堆与颜色种类时,如何为每堆石子分配颜色,确保任意两堆中相同颜色石子的数量之差不超过1。通过分析最大堆与最小堆之间的关系,提供了一种简便的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Painting Pebbles
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one.

In other words, let's say that bi, c is the number of pebbles of color c in the i-th pile. Then for any 1 ≤ c ≤ k1 ≤ i, j ≤ n the following condition must be satisfied |bi, c - bj, c| ≤ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi, c is considered to be zero.

Input

The first line of the input contains positive integers n and k (1 ≤ n, k ≤ 100), separated by a space — the number of piles and the number of colors respectively.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) denoting number of pebbles in each of the piles.

Output

If there is no way to paint the pebbles satisfying the given condition, output "NO" (without quotes) .

Otherwise in the first line output "YES" (without quotes). Then n lines should follow, the i-th of them should contain ai space-separated integers. j-th (1 ≤ j ≤ ai) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them.

Sample Input

Input
4 4
1 2 3 4
Output
YES
1
1 4
1 2 4
1 2 3 4
Input
5 2
3 2 4 1 3
Output
NO
Input
5 4
3 2 4 3 5
Output
YES
1 2 3
1 3
1 2 3 4
1 3 4
1 1 2 3 4


题意: 有n堆石子, 每堆有a[i] 个石子, 有k 种颜色,给每堆石子涂色, 要求任意两堆石子中相同的颜色的差 <= 1; 找到满足条件的涂色方法后,输出其中的任一种;

解题思路: 先看一下不满足的条件, 可以用最大的一堆和最小的一堆来看一下,它们满足的话,其余的肯定都满足。 我们可以用商和余数的方法, 

maxn = m1 * k + r1;

minn = m2 * k + r2;

分为三种情况:1: 当 m1  == m2 时, 可以找到    2: 当m1  - m2 == 1 时, {   (1)  r1 -  r2  > 0  找不到       3: 当 m1 - m2 > 1 时, 找不到

                                                                                                                                         {  (2)  其余 :可以找到

然后:可以找到的就输出  YES  , 再对于每一堆石子循环输出所给的 k种颜色,  找不到就直接输出 NO 

   代码附上::大笑


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
using namespace std;
int a[120];

int main(){
	int n, k, i;
	while(~scanf("%d%d", &n, &k)){
		int minn = 999, maxn = 0;
		for(int i = 0; i < n; ++i){
			scanf("%d", &a[i]);
			minn = min(minn, a[i]);
			maxn = max(maxn, a[i]);
		}
		int m1, m2, r1, r2;
		m1 = maxn / k;
		r1 = maxn % k;
		m2 = minn / k;
		r2 = minn % k;
		if(m1 - m2 > 1){
			cout << "NO" << endl;
		}
		else if (m1 - m2 == 1 && r1 - r2 >= 1){
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
			for(int i = 0; i < n; ++i){
				for(int j = 0; j < a[i]; ++j){
					cout << j % k + 1 << " " ;
				}
				cout << endl;
			}
		}
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值