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> ...
随机推荐
- spark第二篇:Application Submission Guide
提交应用 Spark的bin目录中的spark-submit脚本用于启动集群上的应用程序.它可以通过一个统一的接口使用所有Spark支持的集群管理器. 绑定应用程序的依赖 如果你的代码依赖其他项目,你 ...
- like模糊查询%注入问题
android like 全局模糊查找文件命名 通过条件通过 like %search% 如果查找的关键字是% 那么就成了 like %%% 就会查找出所有的文件 解决办法是先把正则里面的匹配符 替换 ...
- android 学习资源网址
脚本之家: http://www.jb51.net/list/list_233_2.htm csdn: http://blog.csdn.net/xubo578/article/details/571 ...
- Oracle 过程中变量赋值
create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...
- Nodejs 如何制作命令行工具
# 全局安装,安装报错是需要前面加上sudo $ sudo npm install -g xxxb # 输出帮助 $ xxxb -h Usage: xxxb 这里是我私人玩耍的命令哦![options ...
- 【路一直都在】----img标签垂直居中问题
先上代码 .dianshang ul li a { height: 100px; vertical-align:middle; display: table-cell; width: 1 ...
- iOS内存泄露统计
1.Value stored to 'xxx' during its initialization is never read // 对象声明之后根本就没有使用 只有赋值 2.Value stored ...
- Popularize what is heart of mobile phone?
From: http://tech.sina.com.cn/mobile/n/2014-09-29/08399657494.shtml 只谈核数没意义 带你重新认识手机SoC 2014年09月29日 ...
- 19_AOP概述
[AOP的使用场景] 性能测试 访问控制 事务管理 日志记录 [AOP相关术语] [ 连接点 Joinpoint ] 程序执行的某个特定位置.(假如Car类有drive()方法,那么在drive()方 ...
- 3D开源推荐:3DWebExplorer
开源网址:https://github.com/irconde/3DWebExplorer 介绍:演示如何内嵌Google Earth 插件,开发面向公众的3D旅游展示平台