cable master POJ 1064
来源:互联网 发布:人工智能的未来 mobi 编辑:程序博客网 时间:2024/05/19 13:21
问题描述如下:
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.
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 input consists of several testcases. The first line of each testcase contains two integer numbers 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 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.
The input is ended by line containing two 0's.
The input is ended by line containing two 0's.
Output
For each testcase write to the output 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 must contain the single number "0.00" (without quotes).
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output must contain the single number "0.00" (without quotes).
Sample Input
4 118.027.434.575.390 0
Sample Output
2.00
二分搜索的过程, 每一次得到的x, 我们都检查这个值是否满足能够切割为K条相同长度的绳子。 也就是说用长度为x, 得到的Count条长度相同的绳子, Count > K, 不难看出, 这个值也是最大的x。 程序如下:
#include <iostream>#include <climits>#include <cmath> // for floor function#include <cstdio>using namespace std;const int INF = 100000000;int N, K;double L[10010];bool check(double x) { int Count = 0; for(int i = 0; i < N; ++i) { Count += (int)(L[i] / x); } return Count >= K;}int main() { double lb = 0, ub = INF;// freopen("..\\C-large.in","r",stdin);// freopen("..\\C-large.out","w",stdout); while(scanf("%d %d", &N, &K) != EOF) { // for loop for(int i = 0; i < N; ++i) { scanf("%lf", &L[i]); }// double L[] = {8.02, 7.43, 4.57, 5.39}; double mid; for(int i = 0; i < 100; ++i) { mid = (lb + ub) / 2; if(check(mid)) lb = mid; else ub = mid; } printf("%.2f\n", floor(ub * 100) / 100); } return 0;}// a question// double d;//scanf("%lf", &d);//printf("%f", d);// C will promote floats to doubles for// functions that take variable arguments. Pointers aren't//promoted to anything, so you should be using %lf, %lg,//%le or %la (C99) to read in doubles
运行结果如下:
0 0
- poj 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- POJ-1064-Cable master
- poj 1064--Cable master
- POJ 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ(1064)cable master
- POJ 1064 Cable master
- cable master POJ 1064
- POJ 1064 Cable master
- POJ 1064 Cable Master
- POJ-1064 Cable master
- POJ 1064 Cable master
- EF之概念先行
- Java中的假克隆、浅克隆、深克隆
- 18、MySQL-删除记录
- Android Lint工具
- mybatis与hibernate比较
- cable master POJ 1064
- beego框架获取http请求内容
- 19、MySQL-查询表达式
- 查找网站搬迁错误Unable to connect to your database server using the provided settings
- Ubuntu 配置 Android 开发环境遇到的问题
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- FlappyBird开发总结(六)—— 计分制的实现
- Scala 点滴:参数序列 : _ *
- Qt开发 槽函数定义,以及槽函数二次响应多次响应问题