codeforces509B
Painting Pebbles
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
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 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的更多相关文章
随机推荐
- .net core 简单项目的创建
1.linux 安装net coref https://www.microsoft.com/net/learn/get-started/linuxubuntu 2.创建目录 2.创建控制台项目 第一次 ...
- face detection[Face R-CNN]
face r-cnn是腾讯ai实验室的作品,而且登录过腾讯ai实验室官网,发现果然硕果累累,不得不佩服. 1 引言 人脸检测虽然相对之前有了不小的进步,可是还是因为真实世界中人脸图像的明显变化导致仍然 ...
- glance系列二:glance部署及操作
一 简单架构图示参考 更新中... 二 部署glance yum install memcached python-memcachedsystemctl enable memcached.servic ...
- RPC与Zookeeper注册中心的简单实现
连接上文:https://www.cnblogs.com/wuzhenzhao/p/9962250.html RPC框架的简单实现,基于这个小程序,在我学习完Zookeeper之后如何将注册中心与RP ...
- Linux Namespace : Network
Network namespace 在逻辑上是网络堆栈的一个副本,它有自己的路由.防火墙规则和网络设备.默认情况下,子进程继承其父进程的 network namespace.也就是说,如果不显式创建新 ...
- 苹果 icloud 把我 ipad min 所有照片丢失
苹果 icloud 把我 ipad min 所有照片丢失,大概发生在 '云上贵州' 之后! 发帖纪念--- 求个说法---
- Java 自动装箱与拆箱(Autoboxing and unboxing)
什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象实例的时候,我们会这样: Class a = ...
- JS 深拷贝和浅拷贝概念,以及实现深拷贝的三种方式
一.理解堆栈,基本数据类型与引用数据类型 1.堆栈 栈(stack):系统自动分配的内存空间,内存会由系统自动释放,用来存放函数的参数值,局部变量的值等,特点是先进后出. 堆(heap):系统动态分配 ...
- Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)
E. Selling Souvenirs time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Podfile文件用法详解
https://www.jianshu.com/p/b8b889610b7e 2018.01.09 15:51* 字数 2343 阅读 6263评论 3喜欢 34 前言 iOS开发会经常用到cocoa ...