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. .NET 通过entity framework报数据库连接错误:ORA-01017: invalid username/password; logon denied

    如题,答案为:[ORA-01017].NET程序连接数据库失败 转载文章内容如下: 遇到问题 使用 C#/.NET 开发的一个客户端程序,需要连接 ORACLE 数据库,使用 Oracle.Manag ...

  2. 【原创】三招搞死你的IE11,可重现代码下载(IE Crash keyframes iframe)!

    前言 很多人都知道我们在做FineUI控件库,而且我们也做了超过 9 年的时间,在和浏览器无数次的交往中,也发现了多个浏览器自身的BUG,并公开出来方便大家查阅: 分享IE7一个神奇的BUG(不是封闭 ...

  3. jquery tooltip

    这是个加了点淡入淡出效果的顶部tooltip控件,会自动消失 用法: <head> <title></title> <link href="base ...

  4. 从.Net框架Bug的提交到修复代码成功合并到.NET CoreFX主线

    从发现.NET Framework中SmtpClient的Bug并拿出解决方案,然后给微软开发者社区提交Bug开始,总共耗时一个多月,对Bug修复的代码最终被采纳,现已合并到.NET Core Lib ...

  5. Linux Namespace : UTS

    UTS namespace 用来隔离系统的 hostname 以及 NIS domain name.UTS 据称是 UNIX Time-sharing System 的缩写. hostname 与 N ...

  6. tkinter python(图形开发界面)

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macinto ...

  7. 朱晔和你聊Spring系列S1E2:SpringBoot并不神秘

    朱晔和你聊Spring系列S1E2:SpringBoot并不神秘 [编辑器丢失了所有代码的高亮,建议查看PDF格式文档] 文本我们会一步一步做一个例子来看看SpringBoot的自动配置是如何实现的, ...

  8. 《程序猿闭门造车》之NBPM工作流引擎 - 项目整体架构

    前言: 又是一年一度的圣诞节,可这关我什么事呢 :( ,好不容易周末了,还是说说NBPM吧,前不久我发布了一篇关于工作流的文章:<程序猿闭门造车>之NBPM工作流引擎 - 开篇,很多爱好工 ...

  9. Mac无法清倒废纸篓,终极解决方案

    打开终端 输入 sudo -s rm -rf 你的文件路径 回车即可成功删除

  10. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 即时消息提醒功能改进

    可以给自己的信息管理系统增加一些即时消息提醒功能,简单方便,一般是一行代码就可以发送提醒信息了,方便二次开发,个性化改进. 1:可以用简拼,快速查找内部员工. 2:双击直接可以发QQ消息. 3:双击直 ...