Painting Pebbles

CodeForces - 509B

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 cin 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.

Examples

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 sol:构造题真好van♂
容易发现,当一个ai和aj的差大于K时,就无法构造出一个方案使得ai和aj满足条件;
证明:容易发现(XJB乱玩)每个堆中染得尽量平均一定是最优的,所以就有上面的东西了
这样就可以判断无解了,同理构造的时候也只要平均分配一轮轮K循环直到涂满就可以了
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,K;
struct Record
{
int Size,Id;
int Ans[N];
}a[N];
inline bool cmp_Size(Record p,Record q)
{
return p.Size<q.Size;
}
inline bool cmp_Id(Record p,Record q)
{
return p.Id<q.Id;
}
int main()
{
int i,j,Now=;
R(n); R(K);
for(i=;i<=n;i++) R(a[a[i].Id=i].Size);
sort(a+,a+n+,cmp_Size);
if(a[n].Size-a[].Size>K) return *puts("NO");
puts("YES");
for(i=;i<=n;i++)
{
for(j=;j<=a[i].Size;j++) a[i].Ans[j]=j%K+;
}
sort(a+,a+n+,cmp_Id);
for(i=;i<=n;i++)
{
sort(a[i].Ans+,a[i].Ans+a[i].Size+);
for(j=;j<=a[i].Size;j++) W(a[i].Ans[j]);
puts("");
}
return ;
}
/*
input
4 4
1 2 3 4
output
YES input
5 2
3 2 4 1 3
output
NO input
5 4
3 2 4 3 5
output
YES input
4 3
5 6 7 8
output
YES
*/
 

codeforces509B的更多相关文章

随机推荐

  1. day96

    在服务器上部署上线项目 Linux数据库处理 首先我们需要在mysql中创建bbs库,并导入数据库SQL脚本(就是原本运行在我们项目中的数据库) 前提:需要进入mysql中 mysql> cre ...

  2. UVA10838 The Pawn Chess

    UVA好题没人写系列,感觉可以稍稍练习一下面向对象编程的形式(大雾) 题意很简单,在国际象棋的棋盘中有一些兵,走到对方底线即为胜利,问最优决策下谁能获胜.并输出最小步数. 首先这里的棋盘都只有\(4\ ...

  3. 【愚人节快乐】拥抱Bootstrap,FineUI新版效果超炫!

    鉴于 Bootstrap 备受欢迎,我们决定用 Bootstrap 完全替换掉 jQuery,得到了更加漂亮的界面,先睹为快: 注:网页背景图片来自百度网盘,版权归百度所有! 详情:http://fi ...

  4. Python从菜鸟到高手(8):print函数、赋值与代码块

    1.神奇的print函数   print函数相信读者一定对它不陌生,因为在前面的章节,几乎每个例子都使用了print函数,这个函数的功能就是在控制台输出文本.不过print在输出文本时还可以进行一些设 ...

  5. .net core实践系列之短信服务-目录

    前言 经过两周多的业余时间,终于把该系列的文章写完了.第一次写系列,可能部分关键点并没有覆盖到,如果有疑问的朋友可以随时反馈给我.另外也感谢在我发布文章时给予我方案建议与反馈源码BUG的朋友们.下面是 ...

  6. Python股票分析系列——基础股票数据操作(二).p4

    该系列视频已经搬运至bilibili: 点击查看 欢迎来到Python for Finance教程系列的第4部分.在本教程中,我们将基于Adj Close列创建烛台/ OHLC图,这将允许我介绍重新采 ...

  7. 朱晔的互联网架构实践心得S2E5:浅谈四种API设计风格(RPC、REST、GraphQL、服务端驱动)

    Web API设计其实是一个挺重要的设计话题,许多公司都会有公司层面的Web API设计规范,几乎所有的项目在详细设计阶段都会进行API设计,项目开发后都会有一份API文档供测试和联调.本文尝试根据自 ...

  8. OSGI嵌入jetty应用服务器

    1.搭建osgi基础环境,参考:https://www.cnblogs.com/dyh004/p/10642383.html 2.引入jetty相关的依赖包 修改jetty启动端口 3.com.ksz ...

  9. new、getInstance()、newInstance()、Class.forName()

    1.对象使用之前通过getinstance()得到而不需要自己定义,用完之后不需要delete: 2.new 一定要生成一个新对象,分配内存:getInstance() 则不一定要再次创建,它可以把一 ...

  10. set和multiset的用法

    set #include<iostream> #include<algorithm> #include<cstdio> #include<string.h&g ...