codeforces 509 B. Painting Pebbles (构造)

题目链接

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

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

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

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

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

/* ***********************************************
Author :111qqz
Created Time :2016年10月04日 星期二 02时40分20秒
File Name :code/cf/problem/509B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <deque>
 9#include <map>
10#include <string>
11#include <cmath>
12#include <cstdlib>
13#include <bitset>
14#define fst first
15#define sec second
16#define lson l,m,rt<<1
17#define rson m+1,r,rt<<1|1
18#define ms(a,x) memset(a,x,sizeof(a))
19typedef long long LL;
20#define pi pair < int ,int >
21#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6int n,k;
 7int a[105];
 8int main()
 9{
10	#ifndef  ONLINE_JUDGE 
11	freopen("code/in.txt","r",stdin);
12  #endif
 1	cin>>n>>k;
 2	int mn = 101;
 3	int mx = -1;
 4	for ( int i = 1 ; i <= n ; i++)
 5	{
 6	    cin>>a[i];
 7	    mn = min(mn,a[i]);
 8	    mx = max(mx,a[i]);
 9	}
10	if (mx>mn+k)
11	{
12	    puts("NO");
13	}
14	else
15	{
16	    puts("YES");
17	    for ( int i = 1 ; i  <= n ; i++)
18	    {
19		for ( int j = 1 ; j <= a[i] ; j++) printf("%d ",j%k+1);
20		printf("\n");
21	    }
22	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}