區(qū)間選點_Radar Installation

source

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.

Figure A Sample Input of Radar Installations
Input
The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. The input is terminated by a line containing pair of zeros

Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1
1 2
0 2
0 0

Sample Output

Case 1: 2
Case 2: 1

題意:給出n個島伐脖,和雷達能覆蓋的最大半徑摔笤,島嶼分布在水平線以上风喇,雷達安裝在水平線上,求所需的最少的雷達數(shù)窄做。若存在雷達不能覆蓋所有島嶼的情況,輸出-1。
題解 :求出每個島嶼能夠被雷達覆蓋的雷達安裝區(qū)間,然后就等價于區(qū)間選點問題夺饲。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct section{

   double x,y;

} arr[1010];
bool cmp(const section & a,const section & b)
{
        return (a.y<b.y)||((a.y==b.y)&&(a.x>b.x));
}
int main()
{
    int n,cnt=1,flag;
    double d,x0,y0;
    while(scanf("%d%lf",&n,&d)&&(n||d)){
            int sum=1;
            flag=0;
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&x0,&y0);
            if(y0>d) {
                flag=1;
                continue;
            }
            double sqr=sqrt(d*d-y0*y0);
            arr[i].x=x0-sqr;
            arr[i].y=x0+sqr;
        }
        if(flag){
            printf("Case %d: -1\n",cnt);
        }
        else
        {
            sort(arr,arr+n,cmp);
            double dy=arr[0].y;
            for(int i=1;i<n;i++)
            {
                if(arr[i].x>dy)
                {
                    sum++;
                    dy=arr[i].y;
                }
            }
            printf("Case %d: %d\n",cnt,sum);
        }
        cnt++;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子往声,更是在濱河造成了極大的恐慌擂找,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浩销,死亡現(xiàn)場離奇詭異贯涎,居然都是意外死亡,警方通過查閱死者的電腦和手機慢洋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門塘雳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人普筹,你說我怎么就攤上這事败明。” “怎么了斑芜?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長祟霍。 經(jīng)常有香客問我杏头,道長,這世上最難降的妖魔是什么沸呐? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任醇王,我火速辦了婚禮,結(jié)果婚禮上崭添,老公的妹妹穿的比我還像新娘寓娩。我一直安慰自己,他們只是感情好呼渣,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布棘伴。 她就那樣靜靜地躺著,像睡著了一般屁置。 火紅的嫁衣襯著肌膚如雪焊夸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天蓝角,我揣著相機與錄音阱穗,去河邊找鬼。 笑死使鹅,一個胖子當(dāng)著我的面吹牛揪阶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播患朱,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鲁僚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蕴茴,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤劝评,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后倦淀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蒋畜,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年撞叽,在試婚紗的時候發(fā)現(xiàn)自己被綠了姻成。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡愿棋,死狀恐怖科展,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情糠雨,我是刑警寧澤才睹,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站甘邀,受9級特大地震影響琅攘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜松邪,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一坞琴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧逗抑,春花似錦剧辐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至褂傀,卻和暖如春羞酗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背紊服。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工檀轨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人欺嗤。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓参萄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親煎饼。 傳聞我的和親對象是個殘疾皇子讹挎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容

  • source Description Assume the coasting is an infinite str...
    Gitfan閱讀 909評論 0 0
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,572評論 0 23
  • 愛好人皆有之,吹拉彈唱,提籠架鳥筒溃,很多從前被視為游手好閑马篮、不務(wù)正業(yè)的門道,都已經(jīng)漸漸被正了聲怜奖,成了精彩生活的必要調(diào)...
    榮生實驗室閱讀 670評論 7 7
  • 當(dāng)某一天 你也可以奔跑著 張來雙臂 擁抱著我 那時陽光亮了 樹葉也會搖曳著 沙沙的 貌似在欣喜 或許在激動 但是 ...
    顧渣渣閱讀 94評論 0 0
  • 前言 在《Spring 3.x 企業(yè)應(yīng)用開發(fā)實戰(zhàn)》中我看到ThreadLocal這個類浑测,當(dāng)時自己錯誤的認(rèn)為這個類可...
    忘凈空閱讀 872評論 0 1