CF 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

贪心,每种颜色尽可能放



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,k;
int a[MAXN];
int f[MAXN][MAXN];
int main()
{
//	freopen("Painting.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>n>>k;
	For(i,n) {cin>>a[i];f[i][0]=a[i];}
		
	int mi=a[1],ma=a[1];
	For(i,n) ma=max(ma,a[i]),mi=min(mi,a[i]);
	
	if (ma-mi>k) 
	{
		cout<<"NO"<<endl;
		return 0;
	}
	
	cout<<"YES"<<endl;
	For(i,n)
	{
		printf("1");
		Fork(j,2,a[i])
		{
			if (j<=mi) printf(" 1");
			else printf(" %d",j-mi);
		}
		printf("\n");
	}
	
	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lodash是一个JavaScript工具库,提供了很多常用的工具函数,可以方便地进行数据处理、函数式编程等操作。下面是一些常用的Lodash方法及其示例: 1. _.chunk(array, [size]): 把一个数组拆分成多个数组,每个数组包含指定数量的元素。 ``` _.chunk(['a', 'b', 'c', 'd'], 2); // => [['a', 'b'], ['c', 'd']] _.chunk(['a', 'b', 'c', 'd'], 3); // => [['a', 'b', 'c'], ['d']] ``` 2. _.compact(array): 去掉数组中的假值(false、null、0、""、undefined 和 NaN)。 ``` _.compact([0, 1, false, 2, '', 3]); // => [1, 2, 3] ``` 3. _.concat(array, [values]): 连接数组与其他值(可以是值或数组),生成一个新的数组。 ``` var array = [1]; var other = _.concat(array, 2, [3], [[4]]); console.log(other); // => [1, 2, 3, [4]] console.log(array); // => [1] ``` 4. _.difference(array, [values]): 返回在第一个数组中但不在其他数组中的元素。 ``` _.difference([2, 1], [2, 3]); // => [1] ``` 5. _.drop(array, [n=1]): 去掉数组前面的n个元素,返回剩余的数组。 ``` _.drop([1, 2, 3]); // => [2, 3] _.drop([1, 2, 3], 2); // => [3] _.drop([1, 2, 3], 5); // => [] _.drop([1, 2, 3], 0); // => [1, 2, 3] ``` 6. _.find(array, [predicate=_.identity], [fromIndex=0]): 遍历数组,找到第一个符合条件的元素并返回。 ``` var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.find(users, function(o) { return o.age < 40; }); // => { 'user': 'barney', 'age': 36, 'active': true } _.find(users, { 'age': 1, 'active': true }); // => { 'user': 'pebbles', 'age': 1, 'active': true } _.find(users, ['active', false]); // => { 'user': 'fred', 'age': 40, 'active': false } _.find(users, 'active'); // => { 'user': 'barney', 'age': 36, 'active': true } ``` 7. _.flatten(array): 把一个多维数组展开成一维数组。 ``` _.flatten([1, [2, [3, [4]], 5]]); // => [1, 2, [3, [4]], 5] _.flattenDeep([1, [2, [3, [4]], 5]]); // => [1, 2, 3, 4, 5] ``` 8. _.groupBy(array, [iteratee=_.identity]): 把一个数组按照指定的条件进行分组,返回一个对象,对象的键为分组的条件,值为分组后的数组。 ``` var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.groupBy(users, 'active'); // => { 'true': [{ 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'pebbles', 'age': 1, 'active': true }], 'false': [{ 'user': 'fred', 'age': 40, 'active': false }] } _.groupBy(users, function(o) { return o.age; }); // => { '1': [{ 'user': 'pebbles', 'age': 1, 'active': true }], '36': [{ 'user': 'barney', 'age': 36, 'active': true }], '40': [{ 'user': 'fred', 'age': 40, 'active': false }] } ``` 9. _.intersection([arrays]): 返回多个数组中都包含的元素。 ``` _.intersection([2, 1], [2, 3]); // => [2] _.intersection([2, 1], [2, 3], [1, 2]); // => [2] ``` 10. _.join(array, [separator=',']): 把数组中的所有元素用指定的分隔符连接成一个字符串。 ``` _.join(['a', 'b', 'c'], '~'); // => 'a~b~c' ``` 11. _.reverse(array): 把一个数组倒序排列。 ``` _.reverse([1, 2, 3]); // => [3, 2, 1] ``` 12. _.slice(array, [start=0], [end=array.length]): 返回一个从start开始、到end结束(不包含end)的子数组。 ``` _.slice([1, 2, 3], 0, 2); // => [1, 2] _.slice([1, 2, 3], 1); // => [2, 3] _.slice([1, 2, 3], -2); // => [2, 3] ``` 以上是一些常用的Lodash方法及其示例,Lodash还有很多其他的方法,可以查看官方文档来了解更多。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值