Cutting Out CodeForces - 1077D

题目:

You are given an array s consisting of n integers.

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

Cutting out the copy of t means that for each element ti of array t you have to find ti in ss and remove it from s. If for some ti you cannot find such element in ss, 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 tt you can use the elements [1,2,3,2,4,3,1](use the highlighted elements). After cutting out the first copy of tt the array ss can look like [1,3,2,4].
  • To cut out the second copy of tt you can use the elements [1,3,2,4]. After cutting out the second copy of tt the array ss will be [4].

Your task is to find such array t that you can cut out the copy of t from ss 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 nn and k (1≤k≤n≤2⋅10^5) — the number of elements in ss 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⋅10^5).

Output

Print kk 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⋅10^5.

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 

Note

The first example is described in the problem statement.

In the second example the only answer is [7,3,1,3]and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 22.

In the third example the array tt can be cut out 55 times.

 

题意:

给出n个数,让你找出长度为k且出现次数最多的子序列(在这k个数中一个数可以重复出现多次)。

 

思路:

输入时标记每个数出现的次数;

把每个出现过的数放进优先队列(按照  出现过的次数/要使用的次数  从大到小排);

用一个数组记录每一次排在队列前面的数,使用过一次后pop掉,把使用次数+1,再放进队列;

最后输出;

 

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int book[200861],a[200861],b[200861];
struct node
{
    int x,c,w;
    bool operator < (const node &p)const
    {
        return p.x/p.c>x/c;
    }
}e[200861];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        book[a[i]]++;
    }
    priority_queue<node>q;
    for(int i=1;i<=200000;i++)
    {
        if(book[i]!=0)
        {
            struct node t;
            t.x=book[i];
            t.c=1;
            t.w=i;
            q.push(t);
        }
    }
    int m=0;
    while(m<k)
    {
        struct node t=q.top();
        q.pop();
        b[m++]=t.w;
        t.c++;
        q.push(t);
    }
    for(int i=0;i<k-1;i++)
        printf("%d ",b[i]);
    printf("%d\n",b[k-1]);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值