poj1064 Cable master

Cable master

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25643   Accepted: 5504

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

题意:给定一系列长度的电缆,需要截取等长度的电缆,需要截取等长的k份,求所能截取的最长长度;

解题思路:二分法

参考代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,k;
double len[10005];
int f(double a,int b){
	int aa=a*100;
	return aa/b;
}
int cal_num(int l){
	int num=0;
	for (int i=0;i<n;i++){
		num+=f(len[i],l);
	}
	return num;
}
int main(){
	while (cin>>n>>k){
		double sum=0,max=0;
		int left,right,mid;
		for (int i=0;i<n;i++){
			cin>>len[i];
			if (max<len[i])
				max=len[i];
			sum+=len[i];
		}
		if (sum*100<k){
			cout<<"0.00"<<endl;
			continue;
		}
		left=0;
		right=max*100;
		//right=sum*100/k;
		while (left<=right){
			mid=(left+right)/2;
			if (cal_num(mid)<k)
				right=mid-1;
			else
				left=mid+1;
			//cout<<fixed<<setprecision(2)<<left/100.00<<" "<<right/100.00<<" "<<mid/100.00<<endl;
		}
		cout<<fixed<<setprecision(2)<<right/100.00<<endl;
	}
	return 0;
}
时间: 2024-12-26 09:05:55

poj1064 Cable master的相关文章

poj1064 cable master(最大值问题:二分+贪心)

题意: 有n条电缆,他们的长度分别为l[i].如果从n条电缆中切割出K条长度相同的电缆的话,这k条电缆每条最长能多长?答案小数点后保留两位有效数字. 输入: n, k n行:l[i] Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 数据范围: 1<=N<=10000; 1<=k<=10000; 1<=l[i]<=100000. 分析: 设命题:can(x)=能切割出k条长度为x的电缆. 问题转化:求can

POJ1064 Cable master 【精度问题】

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

【自用】POJ1064 Cable master 且来说说卡精度的心得

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44347241"); } 题意: 多组数据,n个小棒,分成m段,最长多长? 不能短于0.01,如果分不出来,输出"0.00" 题解: 满足单调性,来二分吧. 心得: 来,我们看着代码说话. 判无解的处理 首先最多能

POJ1064 Cable master(二分)

本题用二分搜索能够非常easy的求出答案.设条件C(X)为能够得到K条长度为X的绳子,C(x)=(floor(L(i)/x)).X的初始范围为(0,Max(L(i))+1). #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; double a[10005]; int n,k;

POJ1064——二分——Cable master

Language: Default Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29971   Accepted: 6349 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered an

【POJ - 1064】Cable master(二分)

Cable master 直接上中文了 Descriptions 输入2个数 N  K n条绳子    要分成大于等于k段 求每段最长多长呢?并且每段不能小于1cm 必须以厘米精度写入数字,小数点后正好是两位数.如果无法切割所请求的每个长度至少为1厘米的件数,则输出文件必须包含单个数字“0.00”(不带引号). 多组文件输入 Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 题目链接 https://vjudge.net/probl

hdu 1551 Cable master(二分)

Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2003    Accepted Submission(s): 751 Problem Description Inhabitants of the Wonderland have decided to hold a regional programming co

POJ 1064 Cable master(很好玩的二分搜索)

Cable master Time Limit: 1000MS   Memory Limit: 10000K 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

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