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> ...
随机推荐
- Oracle 数据库和Sql Server数据库的区别
Oracle数据库的访问方式,和SqlServer数据库是有很大差别的,下面用图来说明: 1.Sql Server数据库 SqlServer数据库的访问方式,大致是:假设用户通过sa登录SqlServ ...
- web前端与后台数据访问的对象封装
前言:通常情况下,在不使用angularJS/nodeJS/react等这类完整性的解决方案的js时,前端与后台的异步交互都是使用Ajax技术进行解决 一:作为java web开发工程师可能以下代码是 ...
- 安恒杯11月月赛web题目-ezsql详细记录
通过此题目可以学习到 1.通过load_file+like来盲注获取文件内容 2.php魔术方法__get函数的用法 3.bypass linux命令过滤 题目中给了注册和登录的功能,没有源码泄露 ...
- Ubuntu系统修改Python软链接
1.查看使用的版本 python --version 2.查看当前所使用版本的位置 which python 3.如果第二步结果是 /usr/bin/python 则直接删除即可 sudo rm /u ...
- 结合manage.py,在flask项目中使用websocket模块--- flask-socketio
前言: - 为什么我要使用 flask-socketio模块,而不是flask-sockets? - 因为flask-socketio与前端流行的websocket库socke ...
- 深入理解JavaScript系列(27):设计模式之建造者模式
介绍 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...
- Win2D 官方文章系列翻译 - 预乘 Alpha
本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-premultiplied-alpha/ 在计算机绘图中有两种表示颜色值不透明度的方法.Win2D 中两种方法 ...
- 关于JVM
Java 中通过多线程机制使得多个任务同时执行处理,所有的线程共享JVM内存区域main memory,而每个线程又单独的有自己的工作内存,当线程与内存区域进行交互时,数据从主存拷贝到工作内存,进而交 ...
- Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- 同源策略和Jsonp、CORS
一.同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之 ...