您的位置:首页 > 其它

SDAU训练日志第23篇----------搜索与查找二分hdu练习(1)(2018年3月16日)

2018-03-16 08:50 549 查看
这几天做hdu的练习题
有个典型的二分查找题
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.InputThe 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.OutputWrite 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 Input4 11
8.02
7.43
4.57
5.39
0 0

Sample Output2.00题意:有n段长度分别为Li的电缆,要求把它们分割成K条长度为X的电缆,问X的最大值为多少。思路:将X视为变量,可知它的范围为0~max;  那么问题就变成了电缆长度取X时,所得的电缆条数大于,还是等于,或小于K的问题。  用二分查找法能很快的找出K的值,不过要注意精度,直接输出时注意向下取整。
注意check函数的设计#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int maxn=10010;
const int INF=100001;
double a[maxn],a_sum;
int n,k;
bool check(double x)
{
int num=0,i;
for(i=0;i<n;++i)
num+=(int)(a[i]/x);
if(num>=k)
return true;
else return false;
}

int main()
{
while(~ scanf("%d%d",&n,&k)&&( n|| k))
{
int i;
for(i=0;i<n;++i)
{
scanf("%lf",&a[i]);
a_sum+=a[i];
}
double left=0.00,right=a_sum/k,mid;
while(right-left>=1e-8)
{
mid=(left+right)/2;
if(check(mid))
left=mid;
else
right=mid;
}
printf("%.2f\n",floor(right*100)/100);
}
return 0;
}
另外一个题:
hdu1003
/*时间限制:5000/1000ms (Java/其他)内存限制:65536/32768K (Java/其他)
全部提交(s): 21个接受的提交(s): 8。
问题描述
我的生日快到了,传统上我是在做馅饼。
不只是一个馅饼,不,我有一个数字N,各种口味和各种大小。
我的朋友们都来参加我的聚会,每个人都分到一块馅饼。
这应该是一块一块的,而不是几个小块,因为看起来很乱。
这一块可以是一整块。
我的朋友们都很烦人,如果他们中的一个比其他人得到了更大的一块,他们就开始抱怨。
因此,他们所有人都应该得到同样大小的(但不一定是相同形状的)碎片,即使这会导致一些馅饼被破坏(这比破坏聚会好)。
当然,我也想要一块馅饼,这一块也应该是同样大小的。
<br><br>什么是我们所有人能得到的最大的尺寸?
所有的馅饼都是圆柱形的,它们的高度都是一样的,但是馅饼的半径是不同的。
输入
一个带正整数的线:测试用例的数量。
然后对于每个测试用例:<br>——一行有两个整数N和F和1 <= N, F <= 10000:饼的数量和朋友的数量。
<br>---一行有N个整数ri和1 <= ri <= 10000:派的半径。<br>。
输出
对于每个测试用例,输出一行,最大可能体积V,这样我和我的朋友们都能得到一块馅饼的大小诉答案应该是作为一个浮点数的绝对误差最多10 ^(3)。
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double pi = acos(-1.0);
int F,N;
double V[10001];
bool check(double x)
{
int num=0;
for(int i = 0; i < N;i++)
{
num += int(V[i]/x);
}
if(num>=F)
return true;
else return false;
}
int main()
{
int t,r;
double v_sum,left,right,mid;
cin>>t;
while(t--)
{
cin>>N>>F;
F = F+1;//加上自己
for(int i = 0; i < N; i++)
{
cin>>r;
V[i] = pi*r*r;
v_sum += V[i];
}
right = v_sum/F;
left = 0.0;
while((right-left)>1e-6)
{
mid = (left+right)/2;
if(check(mid))
left = mid;
else right = mid;
}
printf("%.4f\n",mid);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: