zoukankan      html  css  js  c++  java
  • codeforces1102/B(思维题)

    B. Array K-Coloring
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an array aa consisting of nn integer numbers.

    You have to color this array in kk colors in such a way that:

    • Each element of the array should be colored in some color;
    • For each ii from 11 to kk there should be at least one element colored in the ii-th color in the array;
    • For each ii from 11 to kk all elements colored in the ii-th color should be distinct.

    Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c1,c2,cnc1,c2,…cn, where 1cik1≤ci≤k and cici is the color of the ii-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.

    Input

    The first line of the input contains two integers nn and kk (1≤k≤n≤5000) — the length of the array aa and the number of colors, respectively.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1≤ai≤5000) — elements of the array aa.

    Output

    If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c1,c2,cnc1,c2,…cn, where 1≤ci≤k and cici is the color of the ii-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.

    Examples

    Input
    4 2
    1 2 2 3
    
    Output
    YES
    1 1 2 2
    
    Input
    5 2
    3 2 1 2 3
    
    Output
    YES
    2 1 1 2 1
    
    Input
    5 2
    2 1 1 2 1
    
    Output
    NO
    

    Note

    In the first example the answer 2 1 2 1 is also acceptable.

    In the second example the answer 1 1 1 2 2 is also acceptable.

    There exist other acceptable answers for both examples.

    题目链接:http://codeforces.com/problemset/problem/1102/B

    题解:题目中要求是k种颜色都要用上,且n个元素都要涂色,但是n个元素中相同元素不可涂相同的颜色(例如第一个样例中的2不可两个都涂1或都涂2)。

    由要求n个元素中相同元素不可涂相同的颜色可在输入时先计算每个相同元素个数,判断是否有大于k的,如若有,则直接输出NO,进行下一组数的测试。如若没有则是输出YES。
    至于涂色可用二维数组来标记这个元素是否使用过这个颜色,没用则用,但是题目还有一个要求全部颜色都要使用,所以可先将所有颜色用过一遍再让其按这元素是否可以使用该颜色来上色。每个元素上的颜色可直接输出,也可用数组存下再输出。

     1 #include<iostream>
     2 #include<string.h>
     3 using namespace std;
     4 int vis6[5005][5005];//标记该元素是否使用了该颜色
     5 bool vis2[5005];//标记该颜色是否被使用过
     6 int cnt[5005];//计算相同元素的个数
     7 void cl()//数组初始化
     8 {
     9     memset(cnt,0,sizeof(cnt));
    10     memset(vis6,0,sizeof(vis6));
    11     memset(vis2,0,sizeof(vis2));
    12 }
    13 int main()
    14 {
    15     int n,k;
    16     while(cin>>n>>k)
    17     {
    18         cl();
    19         int flag=0;
    20         int ans[5005],a[5005];//ans是储存要输出的每个元素颜色答案
    21         for(int i=0;i<n;i++)
    22         {
    23             cin>>a[i];
    24             cnt[a[i]]++;//计算每种相同元素的和
    25             if(cnt[a[i]]>k)若有相同元素的个数大于颜色数则无法完成涂色
    26             flag=1;//标记一下,在输入完后就输出NO,并跳出本次测试
    27         }
    28         if(flag)
    29         {
    30             cout<<"NO"<<endl;
    31             continue;
    32         }
    33         int use=0;//计算使用过的颜色数
    34         for(int i=0;i<n;i++)
    35         {
    36             for(int j=1;j<=k;j++)
    37             {
    38                 if(!vis6[a[i]][j]&&!vis2[j]&&use<k)//如果每种颜色还未都使用过,则进入此使用该元素未使用的且从未使用的颜色
    39                 {
    40                     use++;
    41                     vis6[a[i]][j]=1;//标记该元素使用了该颜色
    42                     vis2[j]=1;//标记该颜色被使用过了
    43                     ans[i]=j;
    44                     break;//找到了可用的颜色,跳出寻找
    45                 }
    46                 if(use==k&&!vis6[a[i]][j])//如果颜色都被用过一次了,则进入此使用该元素未使用的颜色
    47                 {
    48                     vis6[a[i]][j]=1;
    49                     ans[i]=j;
    50                     break;
    51                 }
    52             }
    53         }
    54         cout<<"YES"<<endl;
    55         cout<<ans[0];
    56         for(int i=1;i<n;i++)//输出**注意格式
    57         cout<<" "<<ans[i];
    58         cout<<endl;
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    Linux账户密码安全策略设置 /etc/login.defs:
    GPS网络时间服务器安装注意事项
    ntp时间校准服务器的调试方法
    北斗网络时钟服务器的特点
    sntp时间服务器的介绍
    网络校时服务器
    综合时间码分配器介绍
    子母钟系统介绍
    智能计数器简介
    怎样选择通用计数器?
  • 原文地址:https://www.cnblogs.com/liuzuolin/p/10295520.html
Copyright © 2011-2022 走看看