HDU-OJ-2752 WiFi

来源:互联网 发布:apache 网络驱动器 编辑:程序博客网 时间:2024/06/05 04:54

WiFi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem 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
12 3131012 31310
 

Sample Output
1.01.0
 

Source
University of Waterloo Local Contest 2008.10.04
 

————————————————————早起的分割线————————————————————

思路:同样是二分答案的题目嘛~~和POJ-2456的Aggressive cows几乎一样。请参考该篇博客。

不一样的地方在于浮点数,其实并不需要用浮点数来二分,基本上是做不对的,难以处理。那干脆就不用浮点数了。

因为房子的门牌号(距街尾的距离)都是整数。答案只有两种情况:.0 or .5 这样就好做了。建立一个下标轴:

答案区间:0  0.5  1.0  1.5  2.0……maxi/2

下标区间:0    1    2     3     4  ……maxi

二分下标区间,最后转换,除以2,即可。

代码如下:

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>int dis[100005];int house, wifi;int cmp(const void* a, const void* b) {    return *(int *)a - *(int *)b;}bool check(int m) {    int cnt = 1;    int last = dis[0];    for(int i = 1; i < house; i++) {        if(last + m >= dis[i])    continue;        else {            last = dis[i];            cnt++;            if(cnt > wifi)  return false;        }    }    return true;}int main() {    int cas;    while(~scanf("%d", &cas)) {        while(cas--) {            scanf("%d%d", &wifi, &house);            for(int i = 0; i < house; i++)                scanf("%d", dis+i);            qsort(dis, house, sizeof(dis[0]), cmp);            int l = 0, r = dis[house-1], mid;            while(l < r) {                mid = (l+r) >> 1;                if(check(mid))  r = mid;                else            l = mid + 1;            }            printf("%.1lf\n", 1.0*r/2);        }    }return 0;}


0 0
原创粉丝点击