POJ1064Cable master(二分, 精度控制)
2017-03-22 13:31
393 查看
Cable master
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
Sample Output
题解:这是一道简单的二分,我却WA了8次,原因就是精度控制的问题,这里还要介绍一下floor函数, floor函数:floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求的值左边的值,也就是不大于要求的值的最大的那个)。例如:
floor(3.14) = 3.0
floor(9.999999) = 9.0
floor(-3.14) = -4.0
floor(-9.999999) = -10
然后给一个测试代码:
测试结果:
很容易看出这两个值的差距,所以建议以后要求最大的话就输出max1, 求小的话就输出min1.
下面是AC代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43118 | Accepted: 9233 |
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
题解:这是一道简单的二分,我却WA了8次,原因就是精度控制的问题,这里还要介绍一下floor函数, floor函数:floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求的值左边的值,也就是不大于要求的值的最大的那个)。例如:
floor(3.14) = 3.0
floor(9.999999) = 9.0
floor(-3.14) = -4.0
floor(-9.999999) = -10
然后给一个测试代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> using namespace std; int main() { int N, K; while(~scanf("%d%d", &N, &K)) { double a[10005], min1 = 0, max1 = 0; for(int i = 0; i< N; i++) { scanf("%lf", &a[i]); max1 += a[i]; } max1 = max1/K; double mid; while(fabs(max1-min1)>1e-8) { int sum = 0; mid = (max1+min1)*1.0/2; for(int i = 0; i < N; i++) { sum += (int)(a[i]/mid); } if(sum>=K) min1 = mid; else max1 = mid; } printf("min1 = %.10f max1 = %.10f", min1, max1); printf("%.2f\n", floor(max1*100)/100); } }
测试结果:
很容易看出这两个值的差距,所以建议以后要求最大的话就输出max1, 求小的话就输出min1.
下面是AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> using namespace std; int main() { int N, K; while(~scanf("%d%d", &N, &K)) { double a[10005], min1 = 0, max1 = 0; for(int i = 0; i< N; i++) { scanf("%lf", &a[i]); max1 += a[i]; } max1 = max1/K; double mid; while(fabs(max1-min1)>1e-8) { int sum = 0; mid = (max1+min1)*1.0/2; for(int i = 0; i < N; i++) { sum += (int)(a[i]/mid); } if(sum>=K) min1 = mid; else max1 = mid; } // printf("min1 = %.10f max1 = %.10f\n", min1, max1); printf("%.2f\n", floor(max1*100)/100); } }
相关文章推荐
- poj 1064 Cable master 【二分 + 精度控制】
- (二分,精度控制)Can you solve this equation?--HDOJ
- 二分搜索--控制精度--hdu2199 Can you solve this equation
- codeforces 24E (二分答案,精度控制)
- 二分——HDU1551:Cable master (精度控制)
- HDU—2899—strange fuction—【二分】【精度控制】
- SDUST - Training F HDU2199 方程求解,二分查找,精度控制
- 【基本二分】HDU2199-精度控制求解
- HDU 2899 Strange fuction 二分 + 精度控制
- poj 1064 二分控制精度
- HDU Strange fuction(二分+精度控制)
- poj 1064 (二分+控制精度) && hdu 1551
- POJ - 1064-Cable master-(二分,精度控制)
- HDU—2199—Can you solve this equation?—【二分】【精度控制】
- ZOJ3806 二分计算几何eps控制精度
- POJ1064 Cable master(二分,精度控制)
- POJ 1064 Cable master——二分 + 精度控制
- Spring MVC_HandlerInterceptorAdapter的使用(不仅可实现Filter的所有功能,还可以更精确的控制拦截精度)
- 控制浮点数的精度
- 小数点输出精度控制问题 .xml