Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】
任意门:http://codeforces.com/contest/1077/problem/D
3 seconds
256 megabytes
standard input
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.
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).
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.
7 3
1 2 3 2 4 3 1
1 2 3
10 4
1 3 1 3 10 3 7 7 12 3
7 3 1 3
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
1 1
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 【二分+排序】的更多相关文章
- 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 ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- CodeForces Round #521 (Div.3) E. Thematic Contests
http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #521 (Div.3)题解
A过水,不讲 题解 CF1077B [Disturbed People] 这题就是个显而易见的贪心可是我考场上差点没想出来 显然把一户被打扰的人家的右边人家的灯关掉肯定比把左边的灯关掉 从左到右扫一遍 ...
- Codeforces Round #521 (Div. 3)
B 题过的有些牵强,浪费了很多时间,这种题一定想好思路和边界条件再打,争取一发过. D 题最开始读错题,后面最后发现可以重复感觉就没法做了,现在想来,数据量大,但是数据范围小枚举不行,二分还是可以的 ...
- Codeforces Round #521 Div. 3 玩耍记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
随机推荐
- linux 输出 之 nl 命令
1.命令格式:nl [选项]... [文件]... 2.命令参数: -b :指定行号指定的方式,主要有两种: -b a :表示不论是否为空行,也同样列出行号(类似 cat -n): -b t :如果 ...
- 06-struts2与ognl的结合
1 参数接收 2 配置文件中 1 Demo2Action package www.test.c_config; import com.opensymphony.xwork2.ActionSupport ...
- linux下统计文本行数的各种方法(二)
上一篇讲的都是统计单个文件的方法,直接在命令行执行就可以.现在试试脚本的方式,统计多个文件的行数 一.统计目录下所有文件的文件数及所有行数 脚本暂时命名为count.sh,代码如下: #!/bin/b ...
- HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】
Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- 【linux相识相知】独立硬盘冗余阵列-RAID
独立硬盘冗余阵列(RAID,Redundant Array of Independant Disks),旧称为廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks ...
- 【linux相识相知】VIM编辑器
Vim是一个类似Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了许多的功能,VIM是自由软件,今天我们就来讲讲VIM的使用方法. 本文是基于centos7上的vim编辑器演示的 ...
- bzoj 5302: [Haoi2018]奇怪的背包
Description Solution 首先 \(v_1,v_2,v_3...v_n,P\) 能够构成的最小数是 \(gcd(P,v_1,v_2,v_3...v_n)\) 然后 \(gcd(P,v_ ...
- 封装http请求键值对的js函数
//封装http请求键值对的函数 function Map() { this.keys = new Array(); this.data = {}; //添加键值对 this.put = functi ...
- 字符串(1)——Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- vue+axios+easy-mock+element-ui实现表格分页功能
废话不多,效果如图: LineSource.vue文件内代码如下: <template> <div class="c-main auth userControl" ...