cf509B 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

题意是给出一种染色方案,使得第i行有a[i]个元素,任意两行中任意两种颜色的元素个数相差不超过1
如果max-min>k,直接输出NO
否则直接每行模拟就好了
就是第i+tk的全染第i种颜色(t>=0)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void write(LL a)
{
    if (a<0){printf("-");a=-a;}
    if (a>=10)write(a/10);
    putchar(a%10+'0');
}
inline void writeln(LL a){write(a);printf("
");}
int a[110];
int s[110];
int n,m,mx,mn=inf;
int main()
{
    n=read();m=read();
    for (int i=1;i<=n;i++)
    {
        a[i]=read();
        mx=max(mx,a[i]);
        mn=min(mn,a[i]);
    }
    if (mx-mn>m)
    {
        printf("NO
");
        return 0;
    }
    printf("YES
");
    for (int i=1;i<=n;i++)
    {
        memset(s,0,sizeof(s));
        int now=1;
        for (int j=1;j<=a[i];j++)
        {
            s[now++]++;
            if (now>m)now=1;
        }
        for (int j=1;j<=m;j++)
            for (int k=1;k<=s[j];k++)
                printf("%d ",j);
        printf("
");
    }
    
}
——by zhber,转载请注明来源
原文地址:https://www.cnblogs.com/zhber/p/4265692.html