codeforces 509 B. Painting Pebbles (构造)

题目链接

题意:n堆石子,每堆a[i]个,k种颜色。给每个石子涂色,要求对于每种颜色,任意两堆中该颜色石子的个数最多差一个。问是否有解,有解输出一组方案。

思路:我们发现有解与否只和最大值最小值有关。

设mn为最小值,mx为最大值。

当mx>mn+k时无解,否则有解。

如果有解。。。输出方案就好了。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年10月04日 星期二 02时40分20秒
 4File Name :code/cf/problem/509B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <deque>
15#include <map>
16#include <string>
17#include <cmath>
18#include <cstdlib>
19#include <bitset>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34int n,k;
35int a[105];
36int main()
37{
38	#ifndef  ONLINE_JUDGE 
39	freopen("code/in.txt","r",stdin);
40  #endif
41
42	cin>>n>>k;
43	int mn = 101;
44	int mx = -1;
45	for ( int i = 1 ; i <= n ; i++)
46	{
47	    cin>>a[i];
48	    mn = min(mn,a[i]);
49	    mx = max(mx,a[i]);
50	}
51	if (mx>mn+k)
52	{
53	    puts("NO");
54	}
55	else
56	{
57	    puts("YES");
58	    for ( int i = 1 ; i  <= n ; i++)
59	    {
60		for ( int j = 1 ; j <= a[i] ; j++) printf("%d ",j%k+1);
61		printf("\n");
62	    }
63	}
64
65  #ifndef ONLINE_JUDGE  
66  fclose(stdin);
67  #endif
68    return 0;
69}