日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

(二分搜索)cable master

發(fā)布時(shí)間:2025/3/12 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (二分搜索)cable master 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

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

分析與解答

漢語(yǔ)意思:
有n條繩子,他們的長(zhǎng)度分別為L(zhǎng),如果從他們中切割出K條長(zhǎng)度相同的繩子,試問(wèn)這K條繩子最長(zhǎng)為多長(zhǎng)?答案保留小數(shù)點(diǎn)后兩位

1.求最大值問(wèn)題,[L,R)
2.判斷條件:
可以得到k條長(zhǎng)為x的繩子
3.循環(huán)進(jìn)行二分搜索,目的讓其足夠精確
4.輸出

按套路來(lái):
https://blog.csdn.net/qq_40828914/article/details/81320977

#include<cstdio> #include<cstring> #include<cmath> #define maxn 10010 #define INF 100001 double a[maxn]; int n,k;bool c(double x) {int num=0,i;for(i=0;i<n;++i)num+=(int)(a[i]/x);return num>=k; }int main() {while(scanf("%d%d",&n,&k)!=EOF){int i;for(i=0;i<n;++i)scanf("%lf",&a[i]);double left=0,right=INF,mid;i=1000;while(i--){mid=(left+right)/2;if(c(mid))left=mid;elseright=mid;}printf("%0.2f\n",floor(left*100)/100);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的(二分搜索)cable master的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。