Codeforces Round 734 (Div. 3)B2. Wonderful Coloring - 2(贪心构造实现)
思路:
分类讨论:
当一个数字出现的次数大于等于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(贪心构造实现)的更多相关文章
- 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 ...
- 刷题记录:Codeforces Round #734 (Div. 3)
Codeforces Round #734 (Div. 3) 20210920.网址:https://codeforces.com/contest/1551. 编程细节:下标定义不要一会[1,n]一会 ...
- 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 ...
- Codeforces Round #595 (Div. 3)B2 简单的dfs
原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include &l ...
- 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 ...
- 【Codeforces Round #453 (Div. 2) B】Coloring a Tree
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...
- 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 ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- 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 ...
- 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 ...
随机推荐
- word文档删除空白页
记住两个快捷键 CTRL+Backspace Shift+Backspace 鼠标箭头放在空白的页面 按住键盘上的快捷键 就可以成功删除了不要天天看营销号设置什么磅值,全选删除啥的 效果如下
- 字节码编程,Javassist篇四《通过字节码插桩监控方法采集运行时入参出参和异常信息》
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 字节码编程插桩这种技术常与 Javaagent 技术结合用在系统的非入侵监控中,这样 ...
- Go语言的100个错误使用场景(11-20)|项目组织和数据类型
目录 前言 2. Code and project organization 2.11 没有使用函数式选项模式(#11) 2.12 项目缺乏组织(#12) 2.13 创建公共设施包(#13) 2.14 ...
- 离线解锁 CodeCombat 全关卡教程 使用docker安装实现
前期准备 下载安装docker desktop https://www.123pan.com/s/fmvUVv-HqApH, 这个安装不会的随便搜一个教程,挺多的.我随便找了一个知乎的 Windows ...
- spring声明式事务(@Transactional)开发常犯的几个错误及解决办法
spring声明式事务(@Transactional)开发常犯的几个错误及解决办法 目前JAVA的微服务项目基本都是SSM结构(即:springCloud +springMVC+Mybatis),而其 ...
- ElasticSearch7.3学习(十八)----多索引搜索
1.multi-index 多索引搜索 多索引搜索就是一次性搜索多个index下的数据 /_search:所有索引下的所有数据都搜索出来 /index1/_search:指定一个index,搜索其下所 ...
- Java Calendar 多用,日期 加减
服务需要订购一个月,订购一个月 不等于增加 30天:若是1,3,5的话应该 31天,要善用 Calendar public static void main(String[] args) throws ...
- yapi的安装
1.官方网站:https://hellosean1025.github.io 2.看官方的文档,部署方法: 3. 根据文档 安装环境先: 4. 开始安装yapi 5. 可见需要启动一下 6.重启一下 ...
- CentOS7.5上卸载Oracle19c
最近遇到一个麻烦的事情,由于公司开发的数据库备份容灾系统,对于备份容灾的数据库必须使用LVM(pv.vg.lv),所以之前安装的Oracle19C 必须卸载掉,然后使用空白磁盘通过parted.fdi ...
- CF1853
你谷的加题速度实在太慢了 被 CF 的题目薄纱 A 可以选任意次 \(i\in [1,n]\),使 \(a[1\sim i]++,a[i+1\sim n]--\).求最少操作次数使得原数列变成非从小到 ...