• 题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色.

  • 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是否合法,然后对数字进行排序,从\([1,k]\)不断循环的去涂颜色,这样的好处是一定能保证相同数字涂的颜色不同,然后再按位置排序输出即可.

  • 代码:

    struct misaka{
    int val;
    int id;
    int col;
    }e[N]; int n,k;
    map<int,int> mp;
    int mx; bool cmp1(misaka a,misaka b){
    return a.val<b.val;
    } bool cmp2(misaka a,misaka b){
    return a.id<b.id;
    } int main() {
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    n=read();
    k=read(); for(int i=1;i<=n;++i){
    e[i].val=read();
    e[i].id=i;
    mp[e[i].val]++;
    } for(auto w:mp){
    mx=max(mx,w.se);
    } if(mx>k) puts("NO");
    else{
    puts("YES");
    sort(e+1,e+1+n,cmp1);
    int cnt=1;
    for(int i=1;i<=n;++i){
    e[i].col=cnt;
    cnt++;
    if(cnt==k+1) cnt=1;
    }
    sort(e+1,e+1+n,cmp2);
    for(int i=1;i<=n;++i){
    printf("%d ",e[i].col);
    }
    } return 0;
    }

Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)的更多相关文章

  1. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  2. Codeforces Round #510 (Div. 2) C. Array Product

    题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...

  3. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  5. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配

    题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...

  6. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  7. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  8. Codeforces Round #531 (Div. 3)

    A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...

  9. Codeforces Round #616 (Div. 2) B. Array Sharpening

    t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...

随机推荐

  1. 安装Tomcat 9

    文章目录 访问Tomcat官网 选择下载所需的软件包 安装Tomcat 测试安装 访问Tomcat官网 Tomcat官方的下载地址为:https://tomcat.apache.org/downloa ...

  2. yum -y install gnuplot

    [root@test~]# yum -y install gnuplotLoaded plugins: fastestmirrorLoading mirror speeds from cached h ...

  3. 【Linux】shell脚本实现多并发

    情景 shell脚本的执行效率虽高,但当任务量巨大时仍然需要较长的时间,尤其是需要执行一大批的命令时.因为默认情况下,shell脚本中的命令是串行执行的.如果这些命令相互之间是独立的,则可以使用&qu ...

  4. P1341 无序字母对(欧拉回路)

    题目链接: https://www.luogu.org/problemnew/show/P1341 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一 ...

  5. 检查Mysql主从状态

    .检查MySQL主从同步状态 #!/bin/bash USER=bak PASSWD=123456 IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e  show s ...

  6. Entity与Entity之间的相互转化

    一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...

  7. MySQL下载与安装教程

    一,下载篇 1,首先访问MySQL官网下载页,https://dev.mysql.com/downloads/mysql/ 如果是MAC系统,操作系统请选择macOS,Windows则选择Window ...

  8. Windows环境下搭建FTP服务器

    Windows主机建立FTP服务器 第一步:启用对应的Windows功能 控制面板 选择启用或关闭Windows功能 勾选FTP服务器和Web管理工具 可能出现的问题 系统提示无法安装IIS和FTP服 ...

  9. Samba共享工具安装

    Samba 是一种在局域网上共享文件的一种通信协议,它为局域网内的不同计算机之间提供文件的共享服务. (1)下载并安装 Samba 工具. 确定 Ubuntu 已连接到互联网, 执行如下命令下载 Sa ...

  10. Failed to start LSB: starts php-fpm

    跟nginx一样都是进程占用,记录下 [root@localhost pazzn]# systemctl status php-fpm.service ● php-fpm-72.service - L ...