codeforces 1077D Cutting Out 【二分】
题目:戳这里
题意:给n个数的数组,要求找k个数满足,这k个数在数组中出现的次数最多。
解题思路:k个数每个数出现次数都要最大化,可以想到二分下限,主要是正确的二分不好写。
附ac代码:
1 #include <bits/stdc++.h>
2 typedef long long ll;
3 const int maxn = 1e6+10;
4 const ll inf = 1e18;
5 using namespace std;
6 int cnt[maxn];
7 int s[maxn];
8 int n, k;
9 int ans[maxn];
10 int len = 0;
11 int check(int m,int n) {
12 int x = 0;
13 len = 0;
14 for(int i = 1; i <= n; ++i) {
15 x += cnt[s[i]] / m;
16 if(cnt[s[i]] / m > 0) {
17 ans[++len] = s[i];
18 }
19 }
20 if(x >= k) return 1;
21 else return 0;
22 }
23 int main(){
24
25 scanf("%d %d", &n, &k);
26 for(int i = 1; i <= n; ++i) {
27 scanf("%d", &s[i]);
28 cnt[s[i]]++;
29 }
30
31 int l = 1, r = n;
32 sort(s + 1, s + 1 + n);
33 n = unique(s + 1, s +1 + n) - s -1;
34 ll sum = 0;
35 while(l <= r) {
36 int m = (l + r) / 2;
37 int u = check(m, n);
38 if(u == 1) l = m + 1, sum = m;//如果u>1,则m本身一直满足条件
39 else r = m - 1;
40 }
41 int now = 0;
42 for(int i = 1; i <= n; ++i) {
43 for(int j = 1; j <= cnt[s[i]] / sum; ++j) {//注意除以的是sum,我就是这里忘改了所以最后怎么都没过。。
44 printf("%d ", s[i]);
45 ++now;
46 if(now == k) return 0;
47 }
48 }
49 return 0;
50 }
codeforces 1077D Cutting Out 【二分】的更多相关文章
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- D. Cutting Out 二分
题意是给你n个数字的序列,让你从中找含k个数字的序列,要求这k个数字要尽可能多次的从n个数字的序列中减去. 解法就是从1到n,二分查找可以删除的最大次数. http://codeforces.com/ ...
- Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]
题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...
随机推荐
- 敏捷史话(四):敏捷是人的天性 —— Arie van Bennekum
敏捷是人的天性,是你与生俱来的东西.面对敏捷,Arie van Bennekum 下了这样一个结论. 但这并不意味着人们只能通过天赋获得敏捷,对于想要学习敏捷的人来说,敏捷绝不是仅仅靠学习僵化的框架. ...
- GitLab-CI/CD入门实操
以Spring boot项目为例.传统方式是本地生成jar包,FTP上传服务器,重启服务:如果是内网测试服,也可以在服务器上安装git,在服务器上编译打包.但这都需要人为干预,于是CI/CD就出现了. ...
- 机器学习7-模型保存&无监督学习
模型保存和加载 sklearn模型的保存和加载API from sklearn.externals import joblib 保存:joblib.dump(rf, 'test.pkl') 加载:es ...
- innodb引擎的4大特性
一:插入缓冲 二:二次写 三:自适应哈希 四:预读 1.插入缓冲(insert buffer)插入缓冲(Insert Buffer/Change Buffer):提升插入性能,change buffe ...
- ElasticSearch7.2简单命令实操(postman版)
使用postman访问操作ElasticSearch数据库,数据格式均为json 目录 使用postman访问操作ElasticSearch数据库,数据格式均为json 一.集群设置 1.查看集群设置 ...
- 如何封装Promise对象?
最近看到了一个有趣的Promise的方法,这里记录下来 <script> class MyPromise { constructor(executor) { // 初始化state赋值为p ...
- shell批量解压源码包
有时候部署环境有很多安装包,如果一个一个地解压缩实在太麻烦了,可以用shell批量进行解压缩.命令如下: [root@localhost ~]# vi tar.sh #! /bin/bash #标称是 ...
- spring boot 启动 开启注解 加载 bean
业务描述:创建一个cache类然后交给spring 管理. @Component @Scope("singleton") public class Cache { public C ...
- windows10 中 svn 代码统计工具 StatSVN 使用详解
1.下载TortoiseSVN 下载地址 ps:安装时务必选上命令行的svn命令. 2.下载StatSVN 官网地址 官网上目前时0.7.0版本,我的svn是1.12.0版本,运行时会报错: 所以建议 ...
- DP中的树上边/点覆盖问题
目录 树上边覆盖问题 例:luoguP2016 战略游戏 简述题意: Solution: Code 树上点覆盖问题 简述题意 Solution Code: 树上边覆盖问题 例:luoguP2016 战 ...