[BestCoder Round #4] hdu 4932 Miaomiao's Geometry (貪心)

來源:互聯網
上載者:User

標籤:acm   bestcoder   

Miaomiao‘s GeometryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 363    Accepted Submission(s): 92

Problem DescriptionThere 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. 
InputThere 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. 
OutputFor each test cases , output a real number shows the answser. Please output three digit after the decimal point. 
Sample Input
331 2 331 2 441 9 100 10
 
Sample Output
1.0002.0008.000HintFor 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. 
 
SourceBestCoder Round #4解題思路:

比賽時用二分做的,後來被hack掉了。。

二分結果不對:比如下面這組資料:

4

0 1 4 5

所有可能的線段長度最大可能有兩種情況(某兩個相鄰點之間的距離,某兩個相鄰點之間的距離的一半)。為什麼說是最大,比兩個相鄰點之間的距離一半小當然沒問題。

所以我們就把所有相鄰的點之間的區間距離以及它的一半儲存在數組裡面,從小到大排序,逆向遍曆,每遍曆一個數判斷是否符合題意,如果符合,就退出,得到了答案。怎麼判斷是否符合題意呢? 

用到了貪心的思想,能放左邊,放左邊,不能放左邊,放右邊。

注意到本題並沒有對線段的多少有限制,只要求線段的最大長度,我覺得這個條件對貪心起著關鍵作用。

代碼:

#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <stack>#include <algorithm>#include <cmath>#include <iomanip>using namespace std;int num[60];double inter[120];//區間,裡面保留著相鄰兩個點的區間距離以及區間距離的一半const int eps=1e-6;int n;bool ok(double l){    double temp=num[1];    for(int i=2;i<=n-1;i++)    {        if(temp==num[i])//當倒數第二個點正好被覆蓋時,不用計算後面tep=num[i]+l  if(temp>num[i+1])  最後那個點是自由的            continue;        if(num[i]-l>=temp)            temp=num[i];        else        {            temp=num[i]+l;            if(temp>num[i+1])                return false;        }    }    return true;}int main(){    int t;cin>>t;    while(t--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d",&num[i]);        sort(num+1,num+1+n);        int cnt=0;        for(int i=1;i<=n;i++)        {            inter[cnt++]=num[i]-num[i-1];            inter[cnt++]=(num[i]-num[i-1])/2.0;        }        sort(inter,inter+cnt);        double ans=0;        for(int i=cnt-1;i>=0;i--)        {            if(ok(inter[i]))            {                ans=inter[i];                break;            }        }        cout<<setiosflags(ios::fixed)<<setprecision(3)<<ans<<endl;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.