B2. Wonderful Coloring - 2<734.div3>

简介: B2. Wonderful Coloring - 2<734.div3>

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:


  1. each element of the sequence is either painted in one of kk colors or isn't painted;
  2. 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);
  3. let's calculate for each of kk colors the number of elements painted in the color — all calculated numbers must be equal;
  4. 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.


题目大意



给定一个长度为 nn 的数组 aa,取出 kk 个相离的大小相同的下标集合,集合中每个下标对应的数字需不同,最大化集合的大小,并输出方案。


题目分析



首先统计每个数字出现的次数,如果大于等于 kk 那么显然每个集合分一个,否则待定。贪心地,最后将待定的数字依次 kk 个一组分配给每个集合即可,这里可以用 vector 实现。


#include <bits/stdc++.h>
using namespace std;
int T, n, k, x, ans[200001];
vector<int>num[200001], vec;
int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    cin >> k;
    for ( int i = 1; i <= n; ++i) {
      num[i].clear();
      ans[i] = 0;
    }
    vec.clear();
    for ( int i = 1; i <= n; ++i) {
      cin >> x;
      num[x].push_back(i);
    }
    for ( int i = 1; i <= n; ++i) {
      if (num[i].size() > k)
        for ( int j = 0; j < k; ++j)
          ans[num[i][j]] = j + 1;
      else {
        for ( int j = 0, up = num[i].size(); j < up; ++j)
          vec.push_back(num[i][j]);
      }
    }
    for ( int i = 0, up = vec.size(); i + k - 1 < up; i += k) {
      for ( int j = i; j < i + k; ++j)
        ans[vec[j]] = j - i + 1;
    }
    for ( int i = 1; i <= n; ++i)
      printf("%d ", ans[i]);
    puts("");
  }
  return 0;
}
相关文章
|
运维 Linux
Linux系统调优详解(二)——CPU负载查看相关命令
Linux系统调优详解(二)——CPU负载查看相关命令
309 10
|
人工智能 机器学习/深度学习 Android开发
三年首次大合集:阿里技术免费电子书一键下载!
12本阿里技术官方出版电子书开放下载啦!还在一个个地搜吗?不如来收藏本合辑吧!
6101 0
|
3月前
|
XML 安全 测试技术
【干货满满】分享什么是API接口测试
API接口测试是验证应用程序编程接口功能、性能、安全性及兼容性的关键环节,通过模拟请求并验证响应结果,确保接口能正确处理各种输入和场景。测试内容涵盖功能验证、性能评估、安全防护、兼容性验证及系统可靠性。相比UI测试,API测试无需界面依赖,支持数据驱动与自动化,适用于持续集成流程。常见接口类型包括RESTful、SOAP和GraphQL API,广泛应用于电商、金融及社交平台,保障系统间数据交互的安全与高效。
|
5月前
|
存储 大数据 数据处理
【HarmonyOS 5】鸿蒙中的UIAbility详解(三)
本文是鸿蒙中的UIAbility详解系列的最终章。主要针对UIAbility的冷启动和热启动,对于want数据的处理。UIAbility的备份恢复,UIAbility的接续等高级功能的概念和使用讲解。
267 0
|
12月前
|
JavaScript 前端开发 API
从Vue 2到Vue 3的演进
从Vue 2到Vue 3的演进
234 17
|
SQL 安全 JavaScript
深入剖析Blazor应用的安全性:从常见Web攻击谈起与传统Web应用的对比与防护策略
【8月更文挑战第31天】本文探讨了Blazor应用的安全策略,通过与传统Web应用的安全措施进行比较,详细分析了如何防范常见的网络攻击。首先介绍了Blazor框架如何通过其基于组件的模型和服务器端DOM控制来减少跨站脚本攻击的风险;接着讨论了使用ORM工具如Entity Framework来预防SQL注入攻击的方法;最后分析了跨站请求伪造攻击,并说明了在Blazor应用中如何利用AntiforgeryToken增强安全性。尽管Blazor提供了许多内置的安全防护,但开发者仍需结合良好的编程习惯来全面保护应用。
259 0
|
前端开发 JavaScript 数据管理
什么是单向数据流
什么是单向数据流
403 1
|
数据采集 数据安全/隐私保护 Python
使用代理技术实现数据采集同步获取和保存
在网络爬虫中,使用代理技术可以有效地提高采集数据的效率和稳定性。本文将介绍如何在爬虫中同步获取和保存数据,并结合代理技术,以提高爬取效率。
330 2
使用代理技术实现数据采集同步获取和保存
|
敏捷开发 JavaScript 前端开发
【开题报告】基于SpringBoot的校园周边攻略平台的设计与实现
【开题报告】基于SpringBoot的校园周边攻略平台的设计与实现
300 0
|
机器学习/深度学习 数据挖掘 算法框架/工具
数据挖掘-二手车价格预测 Task01:赛题理解
赛题以预测二手车的交易价格为任务,数据集来自某交易平台的二手车交易记录,总数据量超过40w,包含31列变量信息,其中15列为匿名变量。为了保证比赛的公平性,将会从中抽取15万条作为训练集,5万条作为测试集A,5万条作为测试集B,同时会对name、model、brand和regionCode等信息进行脱敏。
673 0
数据挖掘-二手车价格预测 Task01:赛题理解