链接:https://ac.nowcoder.com/acm/contest/7830/J
来源:牛客网

题目描述

Recently Jack becomes much more romantic. He would like to prepare several bunches of flowers.

Each bunch of flowers must have exactly M flowers. As Jack does not want to be boring, he hopes that flowers in the same bunch are all different species. Now there are N{N}N species of flowers in the flower shop, and the number of the i-th species of flower is aia_iai​. Now Jack would like to know how many bunches of flowers he can prepare at most.

(Flowers are used to propose.)\textit{(Flowers are used to propose.)}(Flowers are used to propose.)

输入描述:

The first line contains an integer T{T}T (1≤T≤101 \le T \le 101≤T≤10) --- the number of test cases.
In the first line of each test case, there are two integers N{N}N, M{M}M (1≤N,M≤300 0001 \le N, M \le 300\,0001≤N,M≤300000) --- the number of flowers' species and the number of flowers in a bunch.
In the second line of each test case, there are N{N}N integers --- the i{i}i-th integer indicates aia_iai​ (1≤ai≤1091 \le a_i \le 10^91≤ai​≤109), the number of i{i}i-th species' flowers.

输出描述:

For each test case, output one integer in one line --- the answer of the corresponding test case.

示例1

输入

复制1 5 3 1 1 1 2 1

1
5 3
1 1 1 2 1

输出

复制2

2

题意:

有n种花,然后要求每束花种至少有m种不同种类的花,问最多能有多少束花

二分答案:

通过先推答案来找是否符合要求

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
#define ll long long
using namespace std;
ll a[300100];
ll b[300100];
int n,m;ll check(ll mid){//判断是否符合要求ll p = 1ll*mid*m;ll sum = 0;for(ll i = 1; i <= n; i++){sum += min(1ll*a[i],mid);//如果小于mid(即答案)则其必然能被使用完全//大于mid的最多只能使用mid个}return sum >= p;}
int main(){ll sum = 0;ll t;scanf("%lld",&t);while(t--){//memset(a,0,sizeof(a));cin >> n >> m;sum=0;for(int i = 1; i <= n; i++){scanf("%lld",&a[i]);sum += a[i];}sort(a+1,a+1+n);//特例:如果n > m ,则不能完成要求//相同则输出最小的数if(m > n){cout << 0 << endl;}else if(m == n){cout << a[1] << endl;}else{//从0到1e14中寻找答案ll l = 0;ll r = 1e14;ll ans = 0;while(l <= r){ll mid = (l+r)/2;ll num = check(mid);if(num == 1){l = mid+1;ans= mid;}else{r = mid-1;}}printf("%lld\n",ans);}}return 0;
}

Flowers(二分答案)相关推荐

  1. 2019ICPC(沈阳) - Flowers(二分)

    题目链接:点击查看 题目大意:给出 nnn 种颜色不同的鲜花,每种花有 aia_iai​ 个,现在规定 mmm 个不同的鲜花可以扎成一束,问最多可以扎多少束鲜花 题目分析:考虑问题的简化版:有三种颜色 ...

  2. UVA1396 Most Distant Point from the Sea(AM - ICPC - Tokyo - 2007)(计算几何,半平面交 + 二分答案)

    整理的算法模板合集: ACM模板 题目传送门 见<训练指南>P279 很明显就是一个二分答案,它问的是最远的点,直接枚举因为这里都是double类型的数所以有无限个点,我们可以直接二分. ...

  3. UVA1146 / LA3211(ACM-ICPC 2004 Europe - Southwestern) Now or later(2-SAT问题 + 二分答案)

    题目要求为 最大化最小值,很明显就是二分答案. 题目中每个飞机 要么是一种状态(早),要么是另一种状态(晚),考虑 2-SAT. 我们二分答案,二分着陆时间间隔的最小值 x. 枚举每两个飞机 p , ...

  4. 解题报告:luoguP2868 Sightseeing Cows G(最优比率环,负环判定,二分答案)

    根据题意,我们要环上各点权值之和除以各边权值之和最大. 求最大答案,很明显可以使用二分答案.那么我们假设当前答案为 x,如果有更大的答案,那么方程就可以按下图转换: 也就是说如果有更大的答案,则有一个 ...

  5. P2759 奇怪的函数(二分答案,数学运算)

    P2759 奇怪的函数 范围2e92e92e9,直接枚举肯定超时,正着直接求答案求不出来,那么运用逆向思维,直接二分答案判断即可.这道题涉及简单的数学运算. 要xx>=nx^x>=nxx& ...

  6. P3743 kotori的设备(二分答案,思维,线性)难度⭐⭐⭐

    题目链接 题目背景 kotori 有 n 个可同时使用的设备. 题目描述 第 i 个设备每秒消耗ai个单位能量.能量的使用是连续的,也就是说能量不是某时刻突然消耗的,而是匀速消耗.也就是说,对于任意实 ...

  7. 【基础算法】二分法(二分答案,二分查找),三分法,Dinkelbach算法,算法详解+例题剖析

    目录 一 . 二分法 二分搜索得要求: 二分查找步骤: 二分答案: 玄学的二分(二分答案) 二 . 三分法 例题 三.01分数规划问题相关算法与题目讲解(二分法与Dinkelbach算法) 一 . 二 ...

  8. 洛谷P1182 数列分段Section II 二分答案

    洛谷P1182 数列分段Section II 二分答案 题意:将 n 个 数 分为 m段 求一种方案,使这m段中最大的和 最小 额..可能有点拗口,其实就是说每一种方案,都有对应的 每段和的最大值, ...

  9. 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate...

    [法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...

最新文章

  1. python文件操作举例
  2. golang中的sjson
  3. 学习笔记100—强制免费下载 百度文库等网站上文档 以及客道巴巴文档 教程
  4. 【jQuery】parent()和parents遍历
  5. Got minus one from a read call异常
  6. iOS 5.0.1完美越狱教程
  7. notnull注解_Hibernate Validator 第19篇:自定义约束-约束注解
  8. c语言程序设计备考,《C语言程序设计》复习资料.doc
  9. matlab 警告(warning)、错误(error)、异常(exception)与断言(assert)
  10. ValueError: I/O operation on closed file 解决办法
  11. Mysql--mysqldump命令 备份数据库
  12. 二维haar小波matlab_MATLAB实验之二维小波变换[附效果图]
  13. 行测中图形推理题的规律
  14. Nvivo用法--数据可视化工具
  15. html点击某部分后弹出展开,点击按钮弹出框并显示内容
  16. android egl 代码,Android配置EGL环境
  17. 微信支付账号服务商快速进件H5源码
  18. java计算机毕业设计乐多多宠物店网站源代码+数据库+系统+lw文档
  19. [数据库原理] 事务的隔离等级 (ANSI标准)
  20. 独木舟上的旅行-OJ

热门文章

  1. HDU1800 字典树写法
  2. 蒙特卡洛法求非线性方程组
  3. 算法进阶--SVM原理
  4. 【考研】分清带头结点和不带头结点的单链表
  5. 小米开放平台接入笔记
  6. 基于ssm学生在线教育微课管理系统
  7. iOS - iOS 12 之后的归档(NSKeyedArchiver)、解档(NSKeyedUnarchiver)用法
  8. react高阶组件的使用
  9. PLC常用的通信协议有哪些?如何进行协议解析和远程上下载?
  10. JDBC连接数据库五步