首页 > 代码库 > hdu 4932 Miaomiao's Geometry 暴力枚举

hdu 4932 Miaomiao's Geometry 暴力枚举

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932


Miaomiao‘s Geometry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 694    Accepted Submission(s): 180


Problem Description
There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length.

There are 2 limits:

1.A point is convered if there is a segments T , the point is the left end or the right end of T.
2.The length of the intersection of any two segments equals zero.

For example , point 2 is convered by [2 , 4] and not convered by [1 , 3]. [1 , 2] and [2 , 3] are legal segments , [1 , 2] and [3 , 4] are legal segments , but [1 , 3] and [2 , 4] are not (the length of intersection doesn‘t equals zero), [1 , 3] and [3 , 4] are not(not the same length).

Miaomiao wants to maximum the length of segements , please tell her the maximum length of segments.

For your information , the point can‘t coincidently at the same position.
 

Input
There are several test cases.
There is a number T ( T <= 50 ) on the first line which shows the number of test cases.
For each test cases , there is a number N ( 3 <= N <= 50 ) on the first line.
On the second line , there are N integers Ai (-1e9 <= Ai <= 1e9) shows the position of each point.
 

Output
For each test cases , output a real number shows the answser. Please output three digit after the decimal point.
 

Sample Input
3 3 1 2 3 3 1 2 4 4 1 9 100 10
 

Sample Output
1.000 2.000 8.000
Hint
For the first sample , a legal answer is [1,2] [2,3] so the length is 1. For the second sample , a legal answer is [-1,1] [2,4] so the answer is 2. For the thired sample , a legal answer is [-7,1] , [1,9] , [10,18] , [100,108] so the answer is 8.
 


看了别人题解才会的..

比赛的时候 在Y ,   以为是两个之间取最大,  然后给取出来得最大取个最小,后来发现 

7
0 3 6 7 12 14 18

这组案例 应该跑出2.5 的  而那样Y 出不来小数给跪了.


后来在hack 的时候 看到很多二分的选手居然没被X;   这题二分是不行的... 

 比如说 这组案例

3

0 1 3 4  

跑出来应该是2

2 符合案例 1.5比2 小但是不符合  所以答案不是线性关系的  所以不能二分来做.


后来看题解看到有人暴力. O(n*n); 

这就要首先判断出,答案必须是某段区间或者某段区间的一半才行.

YY一下吧.   那些人也没有说推理的过程.. 我也不知道怎么推╮(╯_╰)╭;



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100];
int vis[100];
bool ok(double tem,int n)
{
    memset(vis,0,sizeof(vis));
    vis[0]=vis[n-1]=1;
    int i;
    int flag=0;
    for(i=0; i<n; i++)
    {
        if(vis[i]==1)
            continue;
        if((double)a[i]-a[i-1]>=tem&&flag==0)//如果前面的比较大 可以容下
        {
            vis[i]=1;
        }
        else if((double)a[i+1]-a[i]>=tem)//前面容不下  后面判断
        {
            flag=0;
            if(a[i+1]-a[i]==tem)
                vis[i]=vis[i+1]=1; //如果相等 可以解决两个;
            else if(a[i+1]-a[i]>=2*tem)
                vis[i]=vis[i+1]=1; //如果 足够大  就放下两个
            else if(a[i+1]-a[i]>tem)
            {
                vis[i]=1;
                flag=1;// 占了后一个的 前端
            }
        }
        else return 0;
    }
    return 1;
}
int main()
{
    int n,t,i;
    double ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        ans=0;
        sort(a,a+n);
        for(i=1; i<n; i++)
        {
            if(ok((double)(a[i]-a[i-1])/2,n))
                ans=max((double)(a[i]-a[i-1])/2,ans);
            if(ok((double)(a[i]-a[i-1]),n))
                ans=max((double)(a[i]-a[i-1]),ans);
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}

/*
7
0 3 6 7 12 14 18
*/