任意门:http://codeforces.com/contest/1077/problem/D

D. Cutting Out
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array ss consisting of nn integers.

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

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

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

  • To cut out the first copy of tt you can use the elements [1,2––,3,2,4,3––,1––][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][1,3,2,4].
  • To cut out the second copy of tt you can use the elements [1––,3––,2––,4][1_,3_,2_,4]. After cutting out the second copy of tt the array ss will be [4][4].

Your task is to find such array tt that you can cut out the copy of tt 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 kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of elements in ss and the desired number of elements in tt, respectively.

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

Output

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

Examples
input

Copy
7 3
1 2 3 2 4 3 1
output

Copy
1 2 3
input

Copy
10 4
1 3 1 3 10 3 7 7 12 3
output

Copy
7 3 1 3
input

Copy
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
output

Copy
1 1
Note

The first example is described in the problem statement.

In the second example the only answer is [7,3,1,3][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个元素的子序列,满足这个子序列在序列中最多(子序列 不要求有序,连续);

解题思路:

一开始的思想:记录每种数出现的次数,按照降序排序,高出现次数的补低出现次数的,贪心最大化最小值。(wa,代码实现不好)

二分+排序(其实看到要最大化最小值就应该想到了的。。。)

排序还是记录原N序列的每种数的出现次数,按照降序排序;

二分最低出现的次数,判断条件是以这个最低标准是否能填充满 子序列 K。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const int MAXN = 1e6+;
LL sum, mx, x;
int K, N, ct, cnt;
int ans[MAXN], ans0, an[MAXN];
int l = , r;
int p[MAXN]; struct date
{
int num, t;
}a[MAXN]; bool cmp(date a, date b)
{
return a.t > b.t;
} bool judge(int tms)
{
cnt = ;
for(int i = ; i <= ct; i++){
int tmp = a[i].t;
if(tmp < tms) break; //因为已经排序过了
while(tmp >= tms){
tmp-=tms;
ans[++cnt] = a[i].num;
}
}
return cnt>=K;
} int main()
{
scanf("%d%d", &N, &K);
r = N;
for(int i = ; i <= N; i++){
scanf("%d", &x);
if(mx < x) mx = x;
p[x]++;
}
for(int i = ; i <= mx; i++){
if(p[i]){
a[++ct].num = i;
a[ct].t = p[i];
}
}
sort(a+, a++ct, cmp);
int mid;
while(l<=r){
mid = (l+r)>>;
if(judge(mid)){
an[] = ;
for(int i = ; i <= cnt; i++){
an[++an[]] = ans[i];
l = mid+;
}
}
else r = mid-;
}
for(int i = ; i <= K; i++){
printf("%d ", an[i]);
}
puts("");
return ;
}

Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】的更多相关文章

  1. CodeForces Round #521 (Div.3) D. Cutting Out

    http://codeforces.com/contest/1077/problem/D You are given an array ss consisting of nn integers. Yo ...

  2. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  3. CodeForces Round #521 (Div.3) E. Thematic Contests

    http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...

  4. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  5. CodeForces Round #521 (Div.3) B. Disturbed People

    http://codeforces.com/contest/1077/problem/B There is a house with nn flats situated on the main str ...

  6. CodeForces Round #521 (Div.3) A. Frog Jumping

    http://codeforces.com/contest/1077/problem/A A frog is currently at the point 00 on a coordinate axi ...

  7. Codeforces Round #521 (Div.3)题解

    A过水,不讲 题解 CF1077B [Disturbed People] 这题就是个显而易见的贪心可是我考场上差点没想出来 显然把一户被打扰的人家的右边人家的灯关掉肯定比把左边的灯关掉 从左到右扫一遍 ...

  8. Codeforces Round #521 (Div. 3)

    B 题过的有些牵强,浪费了很多时间,这种题一定想好思路和边界条件再打,争取一发过.  D 题最开始读错题,后面最后发现可以重复感觉就没法做了,现在想来,数据量大,但是数据范围小枚举不行,二分还是可以的 ...

  9. Codeforces Round #521 Div. 3 玩耍记

    A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...

随机推荐

  1. vue-watch deep 和 immediate

    watch 是一个对象,对象就有键,有值. 值可以是函数:就是当你监控的家伙变化时,需要执行的函数,这个函数有两个形参,第一个是变化后的值,第二个是变化前的值. 值也可以是函数名:不过这个函数名要用单 ...

  2. Java基础24-文档注释

    格式: /** .......*/ /** 此类是对数组进行取最大值 @author 深海溺心 @version 1.0 */ public class Compare{ private Compar ...

  3. (Frontend Newbie)JavaScript基础之常见数据类型

    JavaScript中的数据类型分为两种,一种是简单数据类型,包括Undefined.Null.Boolean.Number和String,另一种是复杂数据类型,即Object,也可称作为引用类型. ...

  4. 《Python编程从入门到实践》_第二章_变量和简单数据类型

    什么是变量 举例: >>> message = "Hello,Python!" >>> print (message) Hello,Python ...

  5. 深入理解JavaScript系列(27):设计模式之建造者模式

    介绍 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...

  6. JavaScript各类型变量和对象

    一.javascript支持的数据类型: var x=1 数字 var x=0.1   小数 var x=true/false bool var x="abc"      字符串 ...

  7. vbScript: 编号成生不夠位數前面加零

    '编号成生Geovin Du '不夠位數前面加零 塗聚文 Function getStringlen(str,lenint) so="" itop=0 slen=Len(str) ...

  8. 洛谷P1966 火柴排队(逆序对)

    题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...

  9. JavaScript(Two)

    innerHtml xx.innerHtml 读取元素内的所有Html内容 xx.innerHtml = 新的值 替换元素内的所有Html内容 JS中不予许出现"-"; font- ...

  10. 自适应布局下echarts引起页面跳帧

    项目上突然遇到一个问题,鼠标快速滑动有echarts画的饼图时,页面出现了跳帧.布局的高度突然发生变化然后恢复正常.高度怎么会变化呢?都是按百分比来的啊? 经过一番仔细观察,在跳帧的时候页面底部闪过了 ...