【codeforces 509B】Painting Pebbles

【题目链接】:http://codeforces.com/contest/509/problem/B

【题意】

给n鹅卵石染色;
有k种颜色可供选择;
问你有没有染色方案;
使得各个堆的鹅卵石里面,第i颜色的鹅卵石的数目的差的绝对值都不大于1

【题解】

把所有的堆
一开始按照石头的数目升序排一遍;
然后顺序处理
对于第一堆石头
1..k,1..k一直重复直到满a[1]个石头;
这以后
每一行直接复制前一行的;
然后开始给多出来的石头染色;
就给他们染成能够染的石子就好;
一开始1..k都还能再染一个;
所以如果∑a[i]-a[i-1]>k的话就无解
否则肯定有解的;
每次比上一层多a[i]-a[i-1]个石子就好(选择可以染的颜色);
模拟

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

struct abc
{
    int id,val;
};

int n,k,ma[N],ans[N][N],rest[N];
abc a[N];

bool cmp1(abc a,abc b)
{
    return a.val<b.val;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    rei(n),rei(k);
    rep1(i,1,n)
        rei(a[i].val),a[i].id = i;
    rep1(i,1,k)
        rest[i] = 1;
    sort(a+1,a+1+n,cmp1);
    int now = 0;
    rep1(i,1,a[1].val)
    {
        now++;
        if (now>k) now = 1;
        ans[a[1].id][i] = now;
    }
    rep1(i,2,n)
    {
        rep1(j,1,a[i-1].val)
            ans[a[i].id][j] = ans[a[i-1].id][j];
        int need = a[i].val-a[i-1].val;
        int now = 1;
        if (need)
            rep1(j,1,k)
                if (rest[j])
                {
                    rest[j]=0;
                    ans[a[i].id][a[i-1].val+now] = j;
                    need--;now++;
                    if (!need) break;
                }
        if (need) return pri("NO"<<endl),0;
    }
    pri("YES"<<endl);
    rep1(i,1,n)
    {
        for (int j = 1;;j++)
        {
            if (ans[i][j]==0) break;
            pri(ans[i][j]<<' ');
        }
        pri(endl);
    }
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(191)  评论(0编辑  收藏  举报