二分求解 切绳子 (浮点数易出现精度问题)

Cable master

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28644   Accepted: 6076

Description

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
 1 /*浮点数最容易出现精度问题,二分时除来除去就会产生蝴蝶效应,
 2 转化为int型来解决是很好的方法
 3 */
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 using namespace std;
 9
10 int n,k;
11 int len[10010];
12 int is_ok(double mid)
13 {
14     int sum = 0;
15     for(int i = 0; i < n; i++)
16     {
17         sum += len[i] / mid;
18     }
19
20     if(sum >= k)
21         return 1;
22     else
23         return 0;
24 }
25
26 int main()
27 {
28     while(cin>>n>>k)
29     {
30
31         int max_len = 0;
32         for(int i = 0; i < n; i++)
33         {
34             double t;
35             cin>>t;
36             len[i] = t*100;     //关键
37             if(len[i] > max_len)
38                 max_len = len[i];
39         }
40
41         int l = 1,r = max_len;
42         int ans = 0;
43         //二分求解
44         while(l<=r )
45         {
46             int mid = (l + r) / 2;
47             if(is_ok(mid))
48             {
49                 ans = max(mid,ans);  //更新ans
50                 l = mid + 1;
51             }
52             else
53             {
54                 r= mid - 1;
55             }
56         }
57
58         printf("%.2f\n",(double)ans / 100.0);  //int再转成浮点数输出
59
60     }
61     return 0;
62 }
				
时间: 2024-10-15 09:15:14

二分求解 切绳子 (浮点数易出现精度问题)的相关文章

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system. The information department told you that there are n enemy soldiers and their network w

IEEE 754 浮点数的表示精度探讨

IEEE 754 浮点数的表示精度探讨 前言 从网上看到不少程序员对浮点数精度问题有很多疑问,在论坛上发贴询问,很多热心人给予了解答,但我发现一些解答中有些许小的错误和认识不当之处.我曾经做过数值算法程序,虽然基本可用,但是被浮点数精度问题所困扰:事情过后,我花了一点时间搜集资料,并仔细研究,有些心得体会,愿意与大家分享,希望对IEEE 754标准中的二进制浮点数精度及其相关问题给予较为详尽的解释.当然,文中任何错误由本人造成,由我承担,特此声明. 1. 什么是IEEE 754标准? 目前支持二

【转】为何浮点数可能丢失精度

转自铅笔 为何浮点数可能丢失精度?浮点十进制值通常没有完全相同的二进制表示形式. 这是 CPU 所采用的浮点数据表示形式的副作用.为此,可能会经历一些精度丢失,并且一些浮点运算可能会产生意外的结果. 导致此行为的原因是下面之一:1.十进制数的二进制表示形式可能不精确.2.使用的数字之间类型不匹配(例如,混合使用浮点型和双精度型). 为解决此行为,大多数程序员或是确保值比需要的大或者小,或是获取并使用可以维护精度的二进制编码的十进制 (BCD) 库. 详细剖析:浮点型运算为什么会造成精度丢失?1.

JavaScript 浮点数及运算精度调整总结

JavaScript 浮点数及运算精度调整总结 JavaScript 只有一种数字类型 Number,而且在Javascript中所有的数字都是以IEEE-754标准格式表示的.浮点数的精度问题不是JavaScript特有的,因为有些小数以二进制表示位数是无穷的. 作者:来源:theWalker|2015-12-02 10:21 移动端 收藏 分享 [技术沙龙]AI开发者实战营-7分钟打造1个定制技能.7月22号,我们等你一起! JavaScript 只有一种数字类型 Number,而且在Jav

P1577 切绳子

P1577 切绳子 题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位. 输入输出格式 输入格式: 第一行两个整数N和K,接下来N行,描述了每条绳子的长度Li. 输出格式: 切割后每条绳子的最大长度. 输入输出样例 输入样例#1: 4 11 8.02 7.43 4.57 5.39 输出样例#1: 2.00需要高精处理,不能while(l<=r),用for枚举100次高精就够了 1 #include<cstdio

HDU ACM 1025 Constructing Roads In JGShining&amp;#39;s Kingdom-&amp;gt;二分求解LIS+O(NlogN)

#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素 int BS(int n,int x) //二分查找下标,当x比全部元素小时下标为1,比全部元素大时下标为n+1. { int low,high,mid; low=1,high=n; while(low<=high) { mid=(low+h

洛谷1577 1297切绳子(二分答案)

题目描述 Wonderland居民决定举行一届地区性程序设计大赛.仲裁委员会志愿负责这次赛事并且保证会组织一次有史以来最公正的比赛.为此,所有参赛者的电脑和网络中心会以星状网络连接,也就是说,对每个参赛者,组委会会用一根长度一定的网线将他的计算机与中心连接,使得他们到网络中心的距离相等. 为了买网线,组委会与当地的网络公司联系,要向他们购买一定数目的等长网线,这些网线要尽可能的长,使得组织者可以让选手们彼此远离. 于是公司指派管理网线事务的负责人解决此事.负责人清楚地知道仓库里每根网线的长度(精

P1577 切绳子(二分)

思路:先来分析一下数据范围,是1e4个数据,但是,是double类型,结果不超过0.01那么在绳子最大的情况下,单纯的找正确答案暴力的话就是1e7的时间复杂度,再乘上1e4的数据,这样肯定不行.那么很容易想到二分,在找答案时使用二分的话就可以让时间复杂度下降到log(1e7)这是一个比较小的值,起码不超过128,这样,我们的时间复杂度就降了下来了. 检验函数,就是单纯的假设答案x,去除以每个绳子看看能得到最后有几段sum, 如果sum>=k则说明,是可行的.至于是不是最后答案,这要交给二分模板.

luogu P1577 切绳子

题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位. 输入输出格式 输入格式: 第一行两个整数N和K,接下来N行,描述了每条绳子的长度Li. 输出格式: 切割后每条绳子的最大长度. 输入输出样例 输入样例#1: 4 11 8.02 7.43 4.57 5.39 输出样例#1: 2.00二分答案+检验,卡精度,好恶心 #include<cstdio> #include<cmath> int k,n;