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 ≤ k, 1 ≤ 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.
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.
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.
4 4 1 2 3 4
YES 1 1 4 1 2 4 1 2 3 4
5 2 3 2 4 1 3
NO
5 4 3 2 4 3 5
YES 1 2 3 1 3 1 2 3 4 1 3 4 1 1 2 3 4
大致题意是给定颜色的种类,问是否能用这些颜色涂石子
如果是,输出满足条件的一种涂色方案
使任意两堆石子中涂有任意颜色的石子数的差值至多为一个
有一点很容易发现:当这些石堆中有两堆的石子数差值的绝对值大于给的颜色数时,这样的方案显然是不存在的
而对于其它情况,方案似乎是存在的(其实我还没想出严格的证明方法o(╯□╰)o)
那么假定对于其它情况这样的方案是存在的
该怎么输出涂色方案捏
为了让答案更清晰
我们有序地对石子进行涂色
对于任一堆石子,从下至上,我们都从颜色1开始涂,一种颜色涂一块石子
例如总共有4块石子,那么从下到上的颜色分别为1,2,3,4
似不似很简单~(≧▽≦)/~
那么问题来了,如果我们现在只有3种或更少的颜色怎么办o(╯□╰)o
很容易想到再从颜色1开始继续按顺序涂
例如总共有5块石子,而颜色总数只有3种,那么从下到上的颜色分别为1,2,3,1,2
由于一堆石块中各石块的涂色顺序与题目是无关的
因此,当我们按照这样的方法涂色时,给出的必定是符合题意的方案(咦,前面刚说没想出证明方法来着怎么就证出来了╮(╯▽╰)╭)
代码如下:
#include<iostream>
using namespace std;
const int maxn = 100+5;
int main() {
int n, k, min_num = maxn, max_num = -maxn;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > max_num) max_num = a[i];
if (a[i] < min_num) min_num = a[i];
}
if (max_num - min_num > k) { cout << "NO" << endl; return 0; }
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
int first = 1;
for (int j = 0; j < a[i]; j++) {
if (first) first = 0;
else cout << " ";
cout << j%k+1;
}
cout << endl;
}
return 0;
}