亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

poj 1328 Radar Installation (簡單的貪心)

系統 1773 0
Radar Installation
Time Limit: 1000MS ? Memory Limit: 10000K
Total Submissions: 42925 ? Accepted: 9485

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.
poj 1328 Radar Installation (簡單的貪心)
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


  

Source

Beijing 2002

題目鏈接: http://poj.org/problem?id=1328

題意:有一條直的海岸線,上面有雷達。以海岸線為x軸,x軸上面為海,下面為岸。海里面有很多島嶼。已知雷達的觀測半徑。問最少建多少個雷達能把所有島嶼都覆蓋到雷達

?????????? 的偵測范圍內。如果不能覆蓋全部的島嶼,按樣例輸出Case數和-1,反之輸出Case數和最少需要建立的雷達數。

分析:首先算出經過每個雷達且平行于x軸的弦:假設雷達覆蓋半徑為d。則弦的左端點為x-sqrt (d*d-y*y),右端點為x+sqrt(d*d-y*y)。然后按左端點從小到大排序。由于不能受右

?????????? 端點的影響,將弦的左右端點橫坐標用結構體存起來。然后,將第一個雷達放在排序后的弦投影在x軸的右端點temp。從過第2個島嶼的弦的投影開始掃描,如果右端點

?????????? <temp即右端點在雷達左邊。把雷達移動到此右端點,此時就能使雷達既覆蓋到這個島嶼,又覆蓋到前面的島嶼。如果左端點在雷達右邊,則不能覆蓋,需要再建立一個

??????????? 雷達。然后把新雷達建在此時弦右端點投影到x軸的坐標。忽略前面的,對后面的島嶼進行同樣的操作。(貪心思想)

注意:半徑和坐標都有可能是小數,所以用double類型比較好,不然用int又要注意計算時*1.000變為浮點數。

代碼:

    #include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;



struct point

{

    double left;

    double right;

}p[5012];

bool flag;

int cnt;



bool cmp(point &x,point &y)

{

    return x.left<y.left;

}



int main()

{

    int cases=0,i,n;

    double d;

    while(scanf("%d%lf",&n,&d)&&n&&d)

    {

        flag=true;

        double a,b;

        for(i=0;i<n;i++)

        {

            scanf("%lf%lf",&a,&b);

            if(fabs(b)>d)

	    flag=false;

            p[i].left=a-sqrt(d*d-b*b);

            p[i].right=a+sqrt(d*d-b*b);

        }

	printf("Case %d: ",++cases);

	if(!flag)

	    puts("-1");

	else

	{

	    sort(p,p+n,cmp);

	    double temp=p[0].right;

	    cnt=1;

	    for(i=1;i<n;i++)

	    {

		if(p[i].right<temp)

		   temp=p[i].right;

		else if(p[i].left>temp)

	        {

		   cnt++;

		   temp=p[i].right;

		}

	    } 

	    printf("%d\n",cnt);

	}

    }

    return 0;

}





//AC
  

?

?

?

11920732

fukan

1328

Accepted

188K

32MS

C++

981B

2013-08-05 00:20:15

?

?

?

poj 1328 Radar Installation (簡單的貪心)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩精品首页 | 国产一区二区三区亚洲综合 | 美女女女女女女bbbbbb毛片 | 免费福利影院 | 日韩中文字幕在线有码视频网 | 激情五月宗合网 | 老司机午夜在线视频免费 | 天天干天天操天天拍 | 国产精品线在线精品国语 | 香蕉一级视频 | 国产精品久久久久aaaa | 日本激情视频一区二区三区 | 一级免费看| 狠狠操天天操视频 | 日日狠日| 97久久精品人人做人人爽 | 99精品视频不卡在线观看免费 | 婷婷精品 | 欧美高清一区二区三区欧美 | 青青青在线视频国产 | 亚洲伦理 | 亚洲欧洲毛片 | 日韩成人伦理 | 在线观看一级 | 亚洲性久久久影院 | 日韩免费一级毛片欧美一级日韩片 | 奇米第八色 | 久久91亚洲精品久久91综合 | 国产69精品久久久久777 | 国产毛片一级国语版 | 99热这里只有精品久久免费 | 日韩在线一区二区三区免费视频 | 日本免费一区二区三区 | 天天摸日日| 国产成人亚洲精品91专区手机 | 亚洲精品区在线播放一区二区 | 第一福利在线视频 | 日本一区二区三区四区在线观看 | 国产精品第4页 | 亚洲国产天堂久久九九九 | 亚洲欧美成人在线 |