思路:

分类讨论:

当一个数字出现的次数大于等于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. Git - 关联远程仓库以及同时使用Lab和Hub

    更新一下,感觉有更简单的方式 就比如你git config 的 全局的name和email是lab的 那就clone github上的项目然后设置局部的name和email就行了 ********** ...

  2. 文心一言 VS 讯飞星火 VS chatgpt (188)-- 算法导论14.1 5题

    五.用go语言,给定 n 个元素的顺序统计树中的一个元素 x 和一个自然数 i ,如何在O(lgn)的时间内确定工在该树线性序中的第 i 个后继? 文心一言,代码正常运行: 在顺序统计树(也称为平衡二 ...

  3. TienChin-课程管理-配置课程字典

    课程类型 课程适用人群

  4. 设计模式学习-使用go实现备忘录模式

    备忘录模式 定义 优点 缺点 适用范围 代码实现 参考 备忘录模式 定义 备忘录( Memento ):在不破坏封装性的前提下,获取一个对象的内部状态,并在该对象之处保存该状态.这样以后就可将该对象恢 ...

  5. C++ CryptoPP使用RSA加解密

    Crypto++ (CryptoPP) 是一个用于密码学和加密的 C++ 库.它是一个开源项目,提供了大量的密码学算法和功能,包括对称加密.非对称加密.哈希函数.消息认证码 (MAC).数字签名等.C ...

  6. Hadoop超详细讲解之单节点搭建

    1 Hadoop介绍 Hadoop是Apache旗下的一个用java语言实现开源软件框架,是一个开发和运行处理大规模数据的软件平台.允许使用简单的编程模型在大量计算机集群上对大型数据集进行分布式处理. ...

  7. org.apache.hadoop.security.AccessControlException: Queue root.online already has 0 applications, cannot accept submission of application

    org.apache.hadoop.security.AccessControlException: Queue root.online already has 0 applications, can ...

  8. 解决线程不安全的方式(Java)

    一.同步代码块 package com.synchronized1; // 买票示例 // 使用同步代码块解决线程安全问题 public class TicketRunnableImp impleme ...

  9. 苹果M3 Max有两种版本:14+40?还是16+40?

    最近有关苹果M3系列处理器的消息突然多了起来,包括M3.M3 Pro.M3 Max,都将升级为台积电3nm工艺,但规格上比较保守,至少核心数量不会大幅增加. 此前说法称,M3 Max将配备14个CPU ...

  10. HBase Shell将命令执行结果导出到文件

    1.将Hbase shell执行结果输出到文件 echo "scan 'test'" | hbase shell>my.txt 2.查看表的region数 list_regi ...