任意门: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. 实现类似tail -f file功能

    python版本py3 tail -f file是打印最后10行,然后跟踪文件追加的内容打印出来. python3 以为本方式打开的话,不能回退(f.seek(-1,1)),所有以'rb'方式打开文件 ...

  2. PHP 编译安装 gd 库

    作者博文地址:https://www.cnblogs.com/liu-shuai/ 安装gd依赖库 freetype wget http://download.savannah.gnu.org/rel ...

  3. oracle 查询及删除表中重复数据

    create table test1( id number, name varchar2(20) ); ,'jack'); ,'jack'); ,'peter'); ,'red'); insert i ...

  4. 8086实时时钟实验(一)——《x86汇编语言:从实模式到保护模式》05

    1.代码清单 ;代码清单9-1 ;文件名:c09_1.asm ;文件说明:用户程序 ;创建日期:2011-4-16 22:03 ;=================================== ...

  5. innosetup的静默安装与卸载

    静默安装,就是减少程序与用户的交互,一站式的安装过程(一气呵成) 1. 静默安装参数 innosetup的静默安装是通过参数来控制的 1.1.  /silent                     ...

  6. pycharm Python解释器的配置--可以指定批处理文件为解释器

    这样就可以很方便的配置一些环境变量了,很方便很有创意的功能,再次Mark一下以防忘记

  7. word 摘要

    word 使用心得 定义快捷键 Tools -> Customize keyboard 自定义快捷键 cmd + L, 左对齐; cmd + R, 右对齐; cmd + E, 居中对齐 cmd ...

  8. bootstrap-table使用记录

    效果如图所示: 1.框架用的flask 目录结构如下: 2.前端代码如下: table-test1.html <!DOCTYPE html> <html> <head&g ...

  9. bzoj 5305: [Haoi2018]苹果树

    Description Solution \(n\) 个点的二叉树的方案数是 \(n!\) 证明十分显然:新加入的点占掉了 \(1\) 个位置,新加了 \(2\) 个位置,那么多出来一个位置,所以第 ...

  10. MVC设置默认页面

    方法1:在RouteConfig.cs文件中配置默认路由 public class RouteConfig { public static void RegisterRoutes(RouteColle ...