HDU 2752 WiFi

Description

One day, the residents of Main Street got together and decided that they would install wireless internet on their street, with coverage for every house. Now they need your help to decide where they should place the wireless access points. They would like to have as strong a signal as possible in every house, but they have only a limited budget for purchasing access points. They would like to place the available access points so that the maximum distance between any house and the access point closest to it is as small as possible.

Main Street is a perfectly straight road. The street number of each house is the number of metres from the end of the street to the house. For example, the house at address 123 Main Street is exactly 123 metres from the end of the street.

 

Input

The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. The first line of each test case contains two positive integers n, the number of access points that the residents can buy, and m, the number of houses on Main Street. The following m lines contain the house numbers of the houses on Main Street, one house number on each line. There will be no more than 100 000 houses on Main Street, and the house numbers will be no larger than one million.
Please process to the end of the data file.

 

Output

For each test case, output a line containing one number, the maximum distance between any house and the access point nearest to it. Round the number to the nearest tenth of a metre, and output it with exactly one digit after the decimal point.

 

Sample Input

 

1 2 3 1 3 10 1 2 3 1 3 10

 

Sample Output

 

1.0 1.0

思路:二分答案

#include<iostream>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
using namespace std;
const int maxn=2e5;
double a[maxn];
int n,m;
int check(double x)
{
    int s=1;
    double y=a[1]+x;
    for(int i=2; i<=n; i++)
    {
        //if(fabs(y-a[i])-x<=1e-5);
        //printf("y=%.3f a[%d]=%.3f x=%.3f\n",y,i,a[i],x);
        if(fabs(y-a[i])-x<=1e-5);
        else
        {
            s++;
            y=a[i]+x;
        }
    }
    return s;
}
int main()
{
    int t;

    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%d %d",&m,&n);
            for(int i=1; i<=n; i++)
            {
                scanf("%lf",&a[i]);
            }
            sort(a+1,a+1+n);
            double l=0.0,r=1e9,p=0.0;
            //while(l-r<=(1e-5))
            while(l-r<=-(1e-5))
            {
                double mid=(l+r)/2.0;

                int s=check(mid);
                //printf("l=%.3f mid=%.3f r=%.3f s=%d\n",l,mid,r,s);
                if(s<=m)
                {
                    p=mid;
                    r=mid;
                }
                else
                    l=mid;
            }
            printf("%.1f\n",p);
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值