B2. Wonderful Coloring - 2- Codeforces Round #734 (Div. 3)
原题链接Problem - B2 - Codeforces
B2. Wonderful Coloring - 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely.
Recently, Paul and Mary have found a new favorite sequence of integers a1,a2,…,ana1,a2,…,an. They want to paint it using pieces of chalk of kk colors. The coloring of a sequence is called wonderful if the following conditions are met:
- each element of the sequence is either painted in one of kk colors or isn't painted;
- each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color);
- let's calculate for each of kk colors the number of elements painted in the color — all calculated numbers must be equal;
- the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions.
E. g. consider a sequence a=[3,1,1,1,1,10,3,10,10,2]a=[3,1,1,1,1,10,3,10,10,2] and k=3k=3. One of the wonderful colorings of the sequence is shown in the figure.
The example of a wonderful coloring of the sequence a=[3,1,1,1,1,10,3,10,10,2]a=[3,1,1,1,1,10,3,10,10,2] and k=3k=3. Note that one of the elements isn't painted.
Help Paul and Mary to find a wonderful coloring of a given sequence aa.
Input
The first line contains one integer tt (1≤t≤100001≤t≤10000) — the number of test cases. Then tt test cases follow.
Each test case consists of two lines. The first one contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤n1≤k≤n) — the length of a given sequence and the number of colors, respectively. The second one contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n).
It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.
Output
Output tt lines, each of them must contain a description of a wonderful coloring for the corresponding test case.
Each wonderful coloring must be printed as a sequence of nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤k0≤ci≤k) separated by spaces where
- ci=0ci=0, if ii-th element isn't painted;
- ci>0ci>0, if ii-th element is painted in the cici-th color.
Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one.
Example
input
Copy
6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9
output
Copy
1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0
Note
In the first test case, the answer is shown in the figure in the statement. The red color has number 11, the blue color — 22, the green — 33.
--------------------------------------------------------------------------------------------------------------------------------
本来我敲了一个模拟,比较麻烦,也就是排个序,染上之前比对该颜色之前的数字值,不相等再染色,到达k个之后再退回去染色,细节较多。
#include<iostream>
#include<set>
# include<algorithm>
# include<cstring>using namespace std;typedef struct
{int id;int se;int a;int cnt;}dian;
dian s[200000+10];bool cmp(dian a,dian b)
{return a.a<b.a;
}bool cmp1(dian a,dian b)
{return a.id<b.id;
}
int pre[200000+10];int main()
{int t;
cin>>t;
while(t--)
{int n;cin>>n;int k;cin>>k;for(int i=1;i<=n;i++){cin>>s[i].a;s[i].se=0;s[i].cnt=0;s[i].id=i;}sort(s+1,s+1+n,cmp);memset(pre,0,sizeof(pre));int cnt=1;int temp=0;int now=1;for(int i=1;i<=n;i++){if(s[i].a==pre[now])continue;pre[now]=s[i].a;s[i].cnt=cnt;now++;if(now==k+1){now--;for(int j=i;j>=1;j--){if(s[j].cnt==cnt){s[j].se=now;now--;}if(!now)break;}now=1;cnt++;}}sort(s+1,s+1+n,cmp1);for(int i=1;i<=n;i++){cout<<s[i].se<<" ";}cout<<'\n';}return 0;
}
后来想到,我们只需要把出现k次以内的数字塞进数组,然后按照一组k个进行染色就行了
转载一位网友的代码
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;
}
B2. Wonderful Coloring - 2- Codeforces Round #734 (Div. 3)相关推荐
- Codeforces Round #734 (Div. 3) 题解
Hello大家好,今天给大家带来的是 Codeforces Round #734 (Div. 3) 的全题目讲解. 本文链接:https://www.lanqiao.cn/questions/2040 ...
- Codeforces Round #734 (Div. 3) (A-D1)
A 太简单了,写完就删了 B1 #include <bits/stdc++.h> using namespace std; #define int long long signed mai ...
- Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring 构造
传送门 文章目录 题意: 思路: 题意: 思路: 针灸思维不行,数据结构来凑呗. 一开始做的时候想简单了,一直wawawa,后来想到了hackhackhack样例,开始换思路构造,结果死活想不到O(m ...
- Codeforces Round #617 (Div. 3) E2. String Coloring (hard version) 思维 + dp + Dilworth定理
传送门 文章目录 题意: 思路: 题意: 让你给一个串染色,不同颜色且相邻的一对字符可以互换位置,用最少的颜色,使交换后这个字符串字典序最小. 思路: 考虑将字符串分成若干个非递减的子序列,由于其非递 ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...
- Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...
- [CF]Codeforces Round #529 (Div. 3)
[CF]Codeforces Round #529 (Div. 3) C. Powers Of Two Description A positive integer xx is called a po ...
- Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解
Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 文章目录 A. Simply Strange Sort B. ...
最新文章
- php中jwt权限认证,php 后端实现JWT认证方法示例
- 程序员究竟该如何提高效率
- 手机python软件怎么创建项目_pycharm怎么创建项目
- spring入门案例plus
- Name node is in safe mode解决
- mysql 5.5 client 字符集_rhel4 mysql5.5 字符集_character set
- linux命令cp -a,linux命令_ls命令与cp命令详解(一)
- nginx redis mysql_Nginx + Lua + Kafka + Redis + Mysql
- Python-程序模块化
- photoshop保存里没有html,PS中为什么没有存储为web格式?
- 最小二乘法的原理讲解
- LaTeX代码: 下划线与删除线 ← 利用 ulem 宏包
- vue中的混入mix
- Python基础 4 字符串的变形 判断
- 解决Macbook互联网不能共享 因为它是受802.1X保户问题
- Edman降解蛋白测序法的基本步骤解析:耦合和裂解过程
- Nginx学习心得总结第一章
- 宇宙最强IDE vs2019升级
- 华大HC32-(01)-创建工程模板
- 服务器系统连接音响,音响系统入门 从数码串流音源开始