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 ≤ 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.
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
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
题意: 有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;
}