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 ...
随机推荐
- 【7】vscode不同的窗口样式和颜色插件peacock、设置打开多个窗口、md文件打开方式和预览以及插入目录
相关文章: [1]VScode中文界面方法-------超简单教程 [2]VScode搭建python和tensorflow环境 [3]VSCode 主题设置推荐,自定义配色方案,修改注释高亮颜色 [ ...
- C/C++ 数据结构与算法笔记
实现顺序表 #include <stdio.h> #include <stdlib.h> #define MaxSize 10 int Insert_Elem(int Arra ...
- Libevent [补档-2023-08-29]
libevent的使用 8-1 安装 自己百度一下,安装它不是特别难,加油!!! 8-2 libevent介绍 它是一个开源库,用于处理网络和定时器等等事件.它提供了跨平台的API,能够在不同 ...
- Advanced Installer添加快捷方式和卸载功能
依次点击左侧"资源 "中的"文件和文件夹"选中"应用程序快捷方式文件夹 ",在右侧空白处右键-新建快捷方式 在弹出的对话框中,选择需要创建快 ...
- 15.6寸不到1kg!LG Gram超轻薄本发布:13代酷睿加持
出道就以轻薄为主要卖点的LG Gram系列笔记本产品,在今年CES 2023上也迎来更新. 隶属于全新LG Gram UltraSlim产品线的15.6寸新品(15Z90RT),机身重量仅998g,厚 ...
- java将集合里面的元素拼接为一条String字符串
java将集合里面的元素拼接为一条String字符串 1️⃣ 随便创建一个list集合,往里面塞入元素 2️⃣ 第一种方式:通过foreach循环实现 但是通过这种方式只能将list集合里面的元素取出 ...
- 20.1 DLL模块的显式加载和符号链接--《Windows核心编程》
一.显式加载DLL模块使用函数 LoadLibrary/LoadLibraryEx HINSTANCE LoadLibrary(PCTSTR pSzDLLPathName); HINSTANCE Lo ...
- TortoiseSVN 官网打不开,去哪下最新的软件和中文包?
官网:https://tortoisesvn.net 能打开最好,但通常打不开,打不开时候去这个网站下: https://sourceforge.net/projects/tortoisesvn/ 这 ...
- 扩展说明: 指令微调 Llama 2
这篇博客是一篇来自 Meta AI,关于指令微调 Llama 2 的扩展说明.旨在聚焦构建指令数据集,有了它,我们则可以使用自己的指令来微调 Llama 2 基础模型. 目标是构建一个能够基于输入内容 ...
- JS Leetcode 213. 打家劫舍 II 题解分析,在动态规划基础上感受分治算法的魅力
壹 ❀ 引 本题来自LeetCode 213. 打家劫舍 II,难度中等,属于前面我们做过的198. 打家劫舍的升级版,难度同样为中等,题目描述如下: 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内 ...