Poj 1064 cable master (very interesting Binary Search)

Source: Internet
Author: User

Cable master
Time limit:1000 ms   Memory limit:10000 K
Total submissions:24292   Accepted:5200

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 handle 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 118.027.434.575.39

Sample output

2.00

Question: There are n ropes with Li length, and now n ropes are divided into k equal ropes of the same length. Find the maximum severable length of these K ropes.

Analysis: After thinking about it, I feel that the general method is not easy to use. The key point is to find the point that can satisfy the K Division and is critical to the total length of the rope. Therefore, the binary search is used, find the feasible value, and C ++ on poj with a precision of 100 times. g ++ times out. It seems that no one in the discussion area has passed g ++ ....

#include<iostream>#include<cstdio>#include<cmath>const int MAXN=10000;const long INF=100000;double L[MAXN];int N,K;int Tdfs(double x){int num=0;for(int i=0;i<N;i++)num+=(int)(L[i]/x);return num>=K;}int main(){int i;double lb=0,ub=INF,mid;while(std::cin>>N>>K){for(i=0;i<N;i++)std::cin>>L[i];for(i=0;i<100;i++){mid=(lb+ub)/2;if(Tdfs(mid))lb=mid;elseub=mid;}printf("%.2f\n",floor(ub*100)/100);}return 0;}


Poj 1064 cable master (very interesting Binary Search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.