思路:

分类讨论:

当一个数字出现的次数大于等于k,那么最多有k个能被染色,

当一个数字出现的次数小于k,南那么这些数字都可能被染色

还有一个条件就是需要满足每个颜色的数字个数一样多,这里记出现次数小于k的所有数字的出现次数总和为sum,将所有这些数字排序后,前sum-sum%k个数字是都可以被染色的,按照1~k的顺寻依次染色即可。

主要是有点不太好实现。

对于这种我们需要统计每个数字有多少个,同时还需要保留每个数字的下标信息的我们可以开多个vector去维护

对于不需要的直接开一个桶就行。

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
#define x first
#define y second using namespace std; const int N=2e5+10,mod=1e9+7;
int a[N];
int ans[N];
vector<int>tong[N];
bool vis[N]; void solve()
{
int n,k;cin>>n>>k;
rep(i,1,n)
{
vis[i]=a[i]=ans[i]=0;
if(tong[i].size()) tong[i].clear();
}
rep(i,1,n)
{
int x;cin>>x;
a[i]=x;
tong[x].push_back(i);
}
vector<int>b;
rep(i,1,n)
{
if(!vis[a[i]]&&tong[a[i]].size()>=k)
{
rep(j,1,k) ans[tong[a[i]][j-1]]=j;
vis[a[i]]=1;
}
else if(!vis[a[i]]&&tong[a[i]].size()>0)
{
rep(j,0,tong[a[i]].size()-1) b.push_back(tong[a[i]][j]);
vis[a[i]]=1;
}
}
int ss=b.size();
rep(i,0,ss-ss%k-1) ans[b[i]]=(i%k+1);
rep(i,1,n) cout<<ans[i]<<' ';
cout<<endl;
}
signed main()
{
IOS
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}

参考洛谷上大佬的代码写的显然简洁很多。

int main() {
int m,n,k;
cin>>m;
while(m--){
int cnt[200005],ans[200005],inp;//cnt统计26个字母的个数,ans存储染色结果
vector<pair <int,int> > v;
cin>>n>>k;
for(int i=0;i<=n;i++){
ans[i]=0;cnt[i]=0;
}
for(int i=0;i<n;i++){
cin>>inp;
//cout<<"cnt[inp]: "<<cnt[inp]<<" ";
if(cnt[inp]<k){
v.push_back({inp,i});
}
cnt[inp]++;
}
sort(v.begin(),v.end());//排序,目的是为了避免同个数字被染同样色
int groups=v.size()/k;//把能染色的个数分成k组,设一次染色过程为把k种颜色各自用一遍,groups就是能有几次染色过程
for(int i=0;i<groups*k;i++){//gruops*k即为保证用各种颜色次数相等时的最大染色数量
ans[v[i].second]=i%k+1;//染色
}
for(int i=0;i<n;i++) cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}

Codeforces Round 734 (Div. 3)B2. Wonderful Coloring - 2(贪心构造实现)的更多相关文章

  1. Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造

    B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...

  2. 刷题记录:Codeforces Round #734 (Div. 3)

    Codeforces Round #734 (Div. 3) 20210920.网址:https://codeforces.com/contest/1551. 编程细节:下标定义不要一会[1,n]一会 ...

  3. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  4. Codeforces Round #595 (Div. 3)B2 简单的dfs

    原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include &l ...

  5. Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

    链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...

  6. 【Codeforces Round #453 (Div. 2) B】Coloring a Tree

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...

  7. Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)

    This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In a ...

  8. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  9. Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心

    题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...

  10. Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

    B. Anton and Lines   The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...

随机推荐

  1. Ubuntu编译Xilinx的u-boot

    博主这里的是Ubuntu20.04LTS+Vivado2017.4+ZedBoard 注意:本文使用的环境变量导入方法是临时的,只要退出当前终端或者使用其他终端就会失效,出现异常问题,请随时expor ...

  2. 使用svn.externals(外链)提升美术多个svn目录的svn up速度

    svn up多个目录耗时大 svn上的美术资源项目,在打包机上对一个很久没有变化的目录进行svn up也是需要消耗不少时间的,特别打包时需要对多个目录进行svn up,比如空跑54个目录的svn up ...

  3. border多层渐变

    .content { margin-top: 19px; border-top: 1px dashed rgba(113, 183, 248, 0.6) !important; border-left ...

  4. C/C++ 常见数组排序算法

    本文介绍了几种常见的排序算法的实现,包括冒泡排序.选择排序.插入排序.希尔排序.归并排序和快速排序.冒泡排序通过多次遍历数组,比较并交换相邻元素,逐步将较小元素"浮"到数组顶端,时 ...

  5. sed文本处理工具常见用法

    sed的全称是stream editor, 表示它是一个流编译器.可以处理文本内容和终端命令的流标准输出,对文本做查找,替换,插入,删除操作. 它是把文件中的内容逐行copy到缓冲区,然后在缓冲区中进 ...

  6. Visual Studio 2017高级编程(第7版)中文版

    发布一个Visual Studio 2017的编程书籍: 链接:https://pan.baidu.com/s/1-RL9wkNYXwvQOdWrnAsSZQ 提取码:ig0c

  7. 神奇的 SQL ,同时实现小计与合计,阁下该如何应对

    开心一刻 今天,小区有个很漂亮的姑娘出嫁 我对儿子说:你要好好学习,认真写作业,以后才能娶到这么漂亮的老婆 儿子好像听明白了,思考了一会,默默的收起了作业本 然后如释重负的跟我说到:爸,我以后还是不娶 ...

  8. Docker从认识到实践再到底层原理(六-1)|Docker容器基本介绍+命令详解

    前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总 然后就是博主最近最花时间的一 ...

  9. Java - CodeForces - 1230A

    题目: Dawid有了 4 包糖果.第 i 包里面有 Ai 个糖果. Dawid想把这四包糖果送给两个朋友,能否让两个朋友收到相同数量的糖果?注意,不能拆开任何一包糖,不能把糖果留给自己或扔掉,四包糖 ...

  10. centos 安装nacos 并以后台服务形式启动

    一.下载解压nacos tar -xvf nacos-server-1.2.0.tar.gz 二.持久化配置(mysql) 修改nacos/conf/application.properties文件, ...