[cf509B]Painting Pebbles

题意

给出一种染色方案,使得任意两行中任意两种颜色的元素个数相差不超过1

题解

根据抽屉原理,当k+1个物品放入k个抽屉时,必有两个物品放在同一个抽屉
故当序列最大值-最小值>k时,可以发现无法构造符合题意的方案,必有一种颜色在一个序列中多用了2次及以上
比如1 和 4, 2种颜色,
染色方案只能是
1
1 2 1 1

1
1 2 1 2

对于有解的情况,只要按顺序循环每种颜色填入1个


#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define drep(i,t,s) for(int i=t;i>=s;--i)
#define inf 0x3f3f3f3f
using namespace std;
int n,k,a[101];
int main(){
  scanf("%d%d",&n,&k);
  int maxn=0,minn=inf;
  rep(i,1,n){
    scanf("%d",&a[i]);
    if (a[i]>maxn) maxn=a[i];
    if (a[i]<minn) minn=a[i];
  } 
  if (maxn-minn>k) puts("NO");
  else{
       puts("YES");
       rep(i,1,n){
           int col=1;
           for (int j=1;j<a[i];++j){
               printf("%d ",col);
               ++col;
               if (col>k) col=1;
           }
           printf("%d\n",col);
       }
  }
  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、付费专栏及课程。

余额充值