Array K-Coloring【Codeforces Round #531 (Div. 3)B】【构造】

题目链接


  题意:给你N长度的数组,以及K种颜色,要求的是我们能否使用全部K种颜色来填充每个数组元素,其中数组中的每个相同值元素的染色是不能相同的,并且,要用完所有K个颜色,能达到以上要求,则是YES并输出染色,否则,只有NO。

  我WA在了第6组数据,后来没想到,竟然是卡了多组输入的continue;!!!,直接使用单组输入以及return 0;就不会有这样的问题,不改就过了……


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 5005;
int N, K;
int a[maxN], color[maxN], cnt, ans[maxN];
bool used[maxN];
int main()
{
    scanf("%d%d", &N, &K);
    memset(used, false, sizeof(used));  cnt = 0;
    memset(color, 0, sizeof(color));
    memset(ans, 0, sizeof(ans));
    bool flag = true;
    for(int i=1; i<=N; i++)
    {
        scanf("%d", &a[i]);
        if(++color[a[i]] > K) { flag = false; break; }
        cnt = max(cnt, color[a[i]]);
        ans[i] = color[a[i]];
    }
    if(!flag) { printf("NO\n"); return 0; }
    for(int i=1; i<=N; i++)
    {
        if(cnt >= K) break;
        if(!used[ans[i]]) { used[ans[i]] = true; continue; }
        if(color[a[i]] < K)
        {
            ans[i] = ++cnt;
            used[cnt] = true;
            color[a[i]]++;
        }
    }
    if(cnt

 

你可能感兴趣的:(构造)