| Time Limit: 1000MS | ? | Memory Limit: 10000K |
| Total Submissions: 42925 | ? | Accepted: 9485 |
Description
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 is terminated by a line containing pair of zeros
Output
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
題目鏈接: 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 |
Accepted |
188K |
32MS |
981B |
2013-08-05 00:20:15 |
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

