CodeForces 509B Painting Pebbles

B. Painting Pebbles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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

大致题意是给定颜色的种类,问是否能用这些颜色涂石子

如果是,输出满足条件的一种涂色方案

使任意两堆石子中涂有任意颜色的石子数的差值至多为一个


有一点很容易发现:当这些石堆中有两堆的石子数差值的绝对值大于给的颜色数时,这样的方案显然是不存在的

                                而对于其它情况,方案似乎是存在的(其实我还没想出严格的证明方法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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值