(二分搜索)cable master

题目:

Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a “star” topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.
Input
The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.
Output
Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number “0.00” (without quotes).
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00

分析与解答

汉语意思:
有n条绳子,他们的长度分别为L,如果从他们中切割出K条长度相同的绳子,试问这K条绳子最长为多长?答案保留小数点后两位

1.求最大值问题,[L,R)
2.判断条件:
可以得到k条长为x的绳子
3.循环进行二分搜索,目的让其足够精确
4.输出

按套路来:
https://blog.csdn.net/qq_40828914/article/details/81320977

#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 10010
#define INF 100001
double a[maxn];
int n,k;bool c(double x)
{int num=0,i;for(i=0;i<n;++i)num+=(int)(a[i]/x);return num>=k;
}int main()
{while(scanf("%d%d",&n,&k)!=EOF){int i;for(i=0;i<n;++i)scanf("%lf",&a[i]);double left=0,right=INF,mid;i=1000;while(i--){mid=(left+right)/2;if(c(mid))left=mid;elseright=mid;}printf("%0.2f\n",floor(left*100)/100);}return 0;
} 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560774.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql max case连用_mysql 嵌套 case when 的问题

sql 语句如下&#xff1a;SELECTa.uc_id id,(CASE WHEN a.uc_realname IS NULL OR a.uc_realname THEN a.uc_sys_name ELSE a.uc_realname END) AS realName,a.uc_register_time registerTime,a.uc_phone phone,a.uc_last_login_time lastLoginTime,(CASEWHEN LEFT (a.uc_code,…

(二分搜索法尺取法)subsequence

题目 A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the s…

Abp mysql guid_ABP领域层 – 值对象 - ABP 中文文档

3.2 ABP领域层 – 值对象3.2.1 简介用来描述领域的特殊方面、且没有标识符的一个对象&#xff0c;叫做值对象。实体有自己的唯一标识&#xff0c;而值对象是没有标识的。如果两个实体的标识是不同的&#xff0c;那么它们是两个不同的实体&#xff0c;即使这两个实体的其它属性值…

(分治法)归并排序

分治算法一般分为如下3个步骤。 划分问题&#xff1a;把问题的实例划分成子问题。 递归求解&#xff1a;递归解决子问题。 合并问题&#xff1a;合并子问题的解得到原问题的解。 归并排序 按照分治三步法&#xff0c;对归并排序算法介绍如下。 划分问题&#xff1a;把序列…

mysql order by rand 优化_mysql order by与by rand() 的优化经验

介绍下MySQL中的order by语句。几种order by的情况从最简单的case开始看起。用这个表来说明&#xff1a;(10w行数据)1、 最简单的order ―― order by索引字段从explain的结果来看(Extra列)&#xff0c;这个语句并不作排序。因为字段a已经是有顺序的。就是按照索引a的顺序依次…

(分治)逆序对问题

题目&#xff1a; 给一列数a1,a2,…,an&#xff0c;求它的逆序对数&#xff0c;即有多少个有序对(i,j)&#xff0c;使得i< j但ai>aj。n可以高达1e6 分析与解答 由于是从小到大进行合并操作&#xff0c;可以发现如果右半数组的元素q放入临时空间中&#xff0c;那么左半…

python locust api_干货 | 基于Locust的接口压测

作者介绍&#xff1a;吴锋雷。PP云高级测试工程师&#xff0c;拥有10年业务研发和测试经验&#xff0c;目前负责SDK及相关产品的测试工作。系统上线前一般要通过压力测试来判断下是否能承受预估的访问量&#xff0c;假设我们要测试的接口每天访问量是10万&#xff0c;那么平均下…

(贪心)背包问题

1.最优装载问题。 给出n个物体&#xff0c;第i个物体重量为wi。选择尽量多的物体&#xff0c;使得总重量不 超过C。 由于只关心物体的数量&#xff0c;所以装重的没有装轻的划算。只需把所有物体按重量从小到大 排序&#xff0c;依次选择每个物体&#xff0c;直到装不下为止。…

(贪心)区间问题大致思路

1.选择不相交区间。 a.描述&#xff1a; 数轴上有n个开区间(ai, bi)。选择尽量多个区间&#xff0c;使得这些区间两两 没有公共点。 b.思路总结&#xff1a; 1.区间x完全包含y&#xff0c;选y 2.按照bi从小到大排序&#xff0c;从第一个区间开始选 3.把所有和上一个区间相…

python queue windows_python Queue模块

创建一个“队列”对象import Queuemyqueue Queue.Queue(maxsize 10)Queue.Queue类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue的构造函数的可选参数maxsize来设定队列长度。如果maxsize小于1就表示队列长度无限。将一个值放入队列中myqueue.put(10)调用队…

STM32的DHT11

DHT11基础 DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传 感器。它应用专用的数字模块采集技术和温湿度传感技术&#xff0c;确保产品具有极高 的可靠性与卓越的长期稳定性。传感器包括一个电阻式感湿元件和一个NTC测 温元件&#xff0c;并与一个高性能…

(priority_queue)自定义优先级

思考与总结&#xff1a; 1.优先队列&#xff0c;先出队列元素不是先进队列的元素&#xff0c;而是队列中优先级最高的元素 2.遇到这种题一般把每一个数据封装到一个struct里 3.然后根据优先级判断依据&#xff0c;我们通过重定向定义优先队列的优先级 如果我们写bool opera…

python 如何匹配列表中某个单词_Python中部分指定单词的最佳匹配项

如果要重复执行此操作&#xff0c;应创建一个索引&#xff1a;wordlist [word.strip() for word in "run, ran, rat, rob, fish, tree".split(,)]from collections import defaultdictclass Index(object):def __init__(self, wordlist()):self.trie defaultdict(s…

sigkill mysql_Ubuntu不能停止mysqld

我尝试运行服务mysql stop,killall -s SIGKILL mysqld,/etc/init.d/mysql停止,并停止mysql,但仍然看到这个输出从ps aux | grep mysql&#xff1a;root 32302 0.0 0.3 59040 2120 pts/8 S 06:03 0:00 sudo mysqld_saferoot 32305 0.0 0.1 4440 772 pts/8 S 06:03 0:00 /bin/sh …

java中函数_java中的函数

函数的概述1.函数定义: 能完成特定功能的代码段就是函数,每一个函数都是一个独立的小功能2.为什么要有: 如果没有函数,在实现一个功能时候,就要写一遍这个逻辑, 如果功能多次使用,就会产生大量重复的代码.有了函数之后,把这个功能封装到函数中,当你在使用这个功能时候不需要再写…

(dag模型+最长路+字典序)嵌套矩形

题目&#xff1a; 有 n 个矩形&#xff0c;每个矩形可以用两个整数 a, b 描述&#xff0c;表示它的长和宽。矩形 X(a, b) 可以嵌套在矩形 Y(c, d) 中当且仅当 a&#xff1c;c, b&#xff1c;d&#xff0c;或者 b&#xff1c;c, a&#xff1c;d&#xff08;相当于把矩形 X 旋转…

Java智能卡 技术_java智能卡APDU学习笔记

因项目需要&#xff0c;利用APDU通信协议在ME手机端和卡端进行通讯。在实践的过程中遇到一些问题&#xff0c;先记录如下。APDU协议&#xff0c;即是智能卡与读写器间的应用层协议&#xff0c;在ISO7816-4[7]中定义了该协议的结构格式。APDU数据有两种结构&#xff0c;读写器使…

(二叉树存储+递归遍历)Binary Tree Traversals

题目&#xff1a; A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be sys…

java mutator_Java-使用Accessor和Mutator方法

小编典典让我们看一下基础知识&#xff1a;“ Accessor”和“ Mutator”只是获取器和设置器的奇特名称。一个获取器“Accessor”返回一个类的变量或其值。设置器“ Mutator”设置类变量指针或其值。因此&#xff0c;首先您需要设置一个带有一些要获取/设置的变量的类&#xff1…

(二叉树创建+查找)精灵邮差

题目 精灵是非常奇特的生物。众所周知&#xff0c;他们可以活很长时间&#xff0c;他们神奇的能力不是一件容易接受的事情。此外&#xff0c;他们住在树上。但是&#xff0c;你可能不知道有些事情。虽然通过魔法传送提供东西非常方便&#xff08;很像电子邮件&#xff09;。他…