• 题意:给你\(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. 修改hosts文件后不生效,该怎么办

    对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...

  2. CSAPP:Lab0 -Docker搭建纯净Linux环境

    1. 安装docker 在mac-os下我们可以利用homebrew很容易的安装docker. brew install docker 当然去官网下载也很容易 Empowering App Devel ...

  3. CMU数据库(15-445)Lab0-环境搭建

    0.写在前面 从这篇文章开始.开一个新坑,记录以下自己做cmu数据库实验的过程,同时会分析一下除了要求我们实现的代码之外的实验自带的一些代码.争取能够对实现一个数据库比较了解.也希望能写进简历.让自己 ...

  4. [Noip模拟题]Seq

    题目描述 由于hyf长得实在是太帅了,英俊潇洒,风流倜傥,人见人爱,花见花开,车见车载.有一群MM排队看hyf.每个MM都有自己独特的风格,由于hyf有着一颗包容的心,所以,什么风格的MM他都喜欢-- ...

  5. guava eventbus 原理+源码分析

    前言: guava提供的eventbus可以很方便的处理一对多的事件问题, 最近正好使用到了,做个小结,使用的demo网上已经很多了,不再赘述,本文主要是源码分析+使用注意点+新老版本eventbus ...

  6. 简单明朗的 RNN 写诗教程

    目录 简单明朗的 RNN 写诗教程 数据集介绍 代码思路 输入 and 输出 训练集构建 生成一首完整的诗 代码实现 读取文件 统计字数 构建word 与 id的映射 转成one-hot代码 随机打乱 ...

  7. spring data JPA 使用EntityentiListeners实现数据审计功能设计

    当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...

  8. jmeter---线程组执行顺序记录

    一.默认未勾选测试计划中独立运行每个线程组时,线程组并行执行,如下,设置三个请求,每个线程组设置5秒启动5个线程. 未勾选独立运行 运行结果如下,三个线程并行执行 勾选后,一个线程组执行完后才执行下一 ...

  9. websocket心跳重连 websocket-heartbeat-js

    初探和实现websocket心跳重连(npm: websocket-heartbeat-js) 心跳重连缘由 websocket是前后端交互的长连接,前后端也都可能因为一些情况导致连接失效并且相互之间 ...

  10. Go Concurrency Patterns: Context At Google, we require that Go programmers pass a Context parameter as the first argument to every function on the call path between incoming and outgoing requests.

    小结: 1. Background is the root of any Context tree; it is never canceled: 2.     https://blog.golang. ...