CodeForces 1077 D. Cutting Out

Description:

You are given an array s consisting of n integers.

You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.

Cutting out the copy of t means that for each element ti of array t you have to find ti in s and remove it from s. If for some ti you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.

For example, if s=[1,2,3,2,4,3,1] and k=3 then one of the possible answers is t=[1,2,3]. This array t can be cut out 2 times.

To cut out the first copy of t you can use the elements [1,2––,3,2,4,3––,1––] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1,3,2,4].

To cut out the second copy of t you can use the elements [1––,3––,2––,4]. After cutting out the second copy of t the array s will be [4] .

Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.

Input

The first line of the input contains two integers n and k (1≤k≤n≤2⋅105) — the number of elements in s and the desired number of elements in t, respectively.

The second line of the input contains exactly n
integers s1,s2,…,sn (1≤si≤2⋅105).

Output

Print k integers — the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t1,t2,…,tk) should satisfy the following condition: 1≤ti≤2⋅105.

Examples

Input
7 3
1 2 3 2 4 3 1
Output
1 2 3

Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3

Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1

最近脑子有些混乱,写下来防止忘记,将来好复习。
初见这个题,想找k个数去看最大能出现多少次,但中间经历了一些曲折,最后思维完全混乱,然后看了Achan的博客,豁然开朗,他是去试出现次数,看这个次数能否可以让>=k个数出现这些次,而次数范围是有限的,1~n次,所以又可以二分去找,不失为一种有效的突破口。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = (int)2e5 + 10;
typedef long long ll;

int times[MAXN];//The times of a element(number) appears.
int ans[MAXN];
int k;

bool OK(int t)//whether >=k elements can appear t times
{
    int result = 0;
    for(int i = 1; i < MAXN; i++)
        if(times[i] >= t)
            result += times[i] / t;
    return result >= k;
}

int main()
{
    memset(times, 0, sizeof(times));
    int n;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++)
    {
        int t;
        scanf("%d", &t);
        times[t]++;
    }
    int l = 1, r = n, mid, max_time = 1;
    while(l <= r)
    {
        mid = (l+r) >> 1;
        if(OK(mid)) max_time = mid, l = mid + 1;
        else r = mid - 1;
    }
    int tot = 0;
    for(int i = 1; i < MAXN; i++)
        if(times[i] >= max_time)
            for(int j = 1; j <= times[i]/max_time; j++)
            {
                ans[tot++] = i;
                if(tot == k) goto out;
            }
    out: ;
    for(int i = 0; i < k; i++)
        if(i != k-1) printf("%d ", ans[i]);
        else printf("%d\n", ans[i]);
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值