标签:
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 ≤ 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.
Sample Input
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
/* 把个数从少到多进行排序,用来判断前后是否可能,只要统计循环的次数以及循环到哪里了就可以轻松判断,如果循环次数差的大于1并且差的位数也大于1那么肯定是不行的,for example: m = 2 a[1] = 2, a[2] = 5 1 2 b[1] = 1 c[1] = 0; 1 2 1 2 1 b[1] = 2 c[2] = 1 如果循环次数差的大于2也肯定不行这个显然 所以先判是否可以如果可以直接就根据循环和位输出当前值 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct edge{ int a, b, c; }G[110]; bool cmp(edge i, edge j){ return i.a < j.a; } int main() { int n, m; int a[110]; int b[110]; int c[110]; while(~scanf("%d%d", &n, &m)){ for(int i = 1; i <= n ; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++){ int k = a[i] / m; int t = a[i] % m ; b[i] = k; c[i] = t; } for(int i = 1; i <= n ;i++){ G[i].a = a[i]; G[i].b = b[i]; G[i].c = c[i]; } sort(G + 1, G + n + 1, cmp); /* for(int i = 1; i <= n ;i++){ printf("%d %d %d", G[i].a, G[i].b, G[i].c); puts(""); } */ int flag = 0; for(int i = 1; i <= n ; i++){ for(int j = i + 1; j <= n ; j++){ if(G[j].b > G[i].b && G[j].c > G[i].c || G[j].b - G[i].b >= 2 ){ flag = 1 ; break; } } if(flag == 1) break; } if(flag == 1) {printf("NO\n"); } else { printf("YES\n"); for(int i = 1; i <= n ; i++){ for(int j = 1; j <= b[i]; j++){ for(int k = 1; k <= m; k++) printf("%d ",k); } for(int j = 1; j <= c[i]; j++) printf("%d ", j); puts(""); } } } return 0; }
Codeforces Round #289 (Div. 2, ACM ICPC Rules)——B贪心——Painting Pebbles
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4652404.html