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. 【转】MySQL中的共享锁与排他锁

    在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大大减少数据库操作的冲突.行级锁分为共享锁和排他锁两种,本文将详细介绍共享锁及排他锁的概念.使用方式及 ...

  2. 【转】强化学习(一)Deep Q-Network

    原文地址:https://www.hhyz.me/2018/08/05/2018-08-05-RL/ 1. 前言 虽然将深度学习和增强学习结合的想法在几年前就有人尝试,但真正成功的开端就是DeepMi ...

  3. CAP理论与分布式事务解决方案

    微服务系统所设计的系统是分布式系统.分布式系统有一个著名的CAP理论,即同时满足"一致性""可用性"和"分区容错"是一件不可能的事.CAP理 ...

  4. Android开发常用权限设置

    加在AndroidManifest.xml 文件中manifest标签以内,application以外 例如:<!--网络权限 --> <uses-permission androi ...

  5. js、jquery实现放大镜效果

    在一些电商网站的商品详情页面,都会有放大镜效果,实现起来并不是很困难,今天用了两个小时,写了一个放大镜效果的实例,来分享给大家! 实现的效果大概是这个样子的 预览 先来看一下效果吧,点击下面的链接预览 ...

  6. UVA 10791 -唯一分解定理的应用

    #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> ...

  7. 出题人的女装(牛客练习赛38题B) (概率+分式运算)

    链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  8. Javascript模板引擎handlebars使用

    源地址:http://rfyiamcool.blog.51cto.com/1030776/1278620 代码示例: <!DOCTYPE html> <html> <he ...

  9. iOS开发之线程组解决请求多个接口数据,完成后,再刷新界面

    1.多任务请求接口,完成后,在刷新数据,常用方法 2018年07月18日 16:34:38 hbblzjy 阅读数:1382 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  10. NET操作RabbitMQ组件EasyNetQ

    NET操作RabbitMQ组件EasyNetQ使用中文简版文档. 本文出自EasyNetQ官方文档,内容为自己理解加翻译.文档地址:https://github.com/EasyNetQ/EasyNe ...