poj 1064 (二分+控制精度) && hdu 1551

链接:http://poj.org/problem?id=1064

Cable master

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23896   Accepted: 5118

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

×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××要注意控制精度,以及最后的舍入误差。脑残犯了个基础的错误,// sum+=(int)str[i]/x;这样强制转型,就有问题了,应该把要转型的部分用括号括起来,两边都要舍入误差在于,如果结果是1.0099999应该输出1.00,而不是1.01××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5
 6 #define eps 1e-6
 7
 8 double str[10005];
 9 int n,k;
10
11 int judge(double x)
12 {
13     int sum=0;
14     for(int i=0; i<n; i++)
15     {
16         sum+=(int)(str[i]/x);
17     }
18     return sum;
19 }
20
21 int main()
22 {
23     int i,j;
24     while(scanf("%d%d",&n,&k)!=EOF)
25     {
26         double left=0.0,right,sum=0.0,maxx=0.0;
27         //ght=n/k;
28         for(i=0; i<n; i++)
29         {
30             scanf("%lf",&str[i]);
31             //if(maxx < str[i])
32             //    maxx = str[i];
33             //sum+=str[i];
34             maxx=maxx<str[i]?str[i]:maxx;
35         }
36         right=maxx;
37         while(right-left > eps)
38         {
39             double mid=(left+right)/2.0;
40             if(judge(mid) >= k )
41                 left=mid;
42             else
43                 right=mid;
44         }
45         int tmp=right * 100;
46         double ans=(double)tmp * 0.01;
47         printf("%.2lf\n",ans);
48     }
49     return 0;
50 }

时间: 2024-11-10 08:35:47

poj 1064 (二分+控制精度) && hdu 1551的相关文章

POJ 1064 (二分)

题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 解题思路: 根据最长的棍子二分枚举切割长度. 这点很容易想到. 本题麻烦的地方在于小数的二分. 由于精度丢失问题,如果直接使用double进行二分,那么二分确定的最大长度必然是不精确的. 如:只有1根100.0的棍子,分成10段.如果使用double二分,那么就算把精度控制到0.0000001, 最

POJ 1064 Cable master 浮点数二分

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21181   Accepted: 4571 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

poj 1064 Cable master ,二分 精度!!!

给出n根绳子,求把它们切割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 代替   while(r-l>eps) 循环100次精度能达到1e-30,基本上能一般题目的精度要求. 而 浮点数二分区间的话容易产生精度缺失导致死循环. #include<cstdio> double L[10000 + 10]; int n, k; int ok(double x) { int cnt = 0; for(int i=0; i<n; ++

POJ 1064 Cable master(二分查找+精度)(神坑题)

POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条件写错了,wa了n次,当 c(mid)<=k时,令ub=mid,这个判断是错的,因为要找到最大切割长度,当满足这个条件时,可能已经不是最大长度了,此时还继续缩小区间,自然就wa了,(从大到小递减,第一次满足这个条件的值,就是最大的值),正确的判断是当 c(mid)<k时,令ub=mid,这样循环1

POJ 1064 1759 3484 3061 (二分搜索)

POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说明:还有printf的%.2f会四舍五入的,需要*100再取整以截取小数点后两位. #include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<math

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

二分搜索 POJ 1064 Cable master

题目传送门 1 /* 2 题意:n条绳子问切割k条长度相等的最长长度 3 二分搜索:搜索长度,判断能否有k条长度相等的绳子 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f

POJ 2777 &amp;&amp; ZOJ 1610 &amp;&amp;HDU 1698 --线段树--区间更新

直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 -- 区间更新的话 分为 增减 或者 修改 主要就是个 laze 标记 就是延迟更新 对于区间更新的写法 一般是有2种 其一 仔细划分到每个细小的区间    另一 粗略划分 反正 ==我的代码里会给出2种写法 看自己喜好 hdu 1 //线段树 成段更新 ---> 替换 根结点的查询 2 3 #i

POJ 3525 二分+半平面交

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3812   Accepted: 1779   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t