国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1328 Radar Installation (貪心)

2019-11-08 03:09:10
字體:
來源:轉載
供稿:網友

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.

img

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 21 2-3 12 11 20 20 0

Sample Output

Case 1: 2Case 2: 1

題意

海上有n多島,在海岸線上(x軸)建一個雷達能覆蓋到與它距離不超過d的島,求覆蓋所有島的最小雷達數。

思路

以島嶼為圓心,d為半徑畫圓,交x軸于s,e兩點,然后以線段末尾為排序標準對所有這樣的線段排序,從左至右依次判斷,假設雷達放置在一個線段的末端,然后忽略它所能涉及到的島嶼。

AC 代碼

#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<stdlib.h>#include<queue>using namespace std;struct point{ double x,y; double s,e; bool Operator<(const point &o)const { return e<o.e; }} land[1005];int solve(int n,int d){ for(int i=0; i<n; i++) //計算所有的線段 { double t=sqrt(d-land[i].y*land[i].y); land[i].s=land[i].x-t; land[i].e=land[i].x+t; } sort(land,land+n); //排序 int ans=1; double r=land[0].e; for(int i=1; i<n; i++) { if(land[i].s>r) { ++ans; r=land[i].e; } } return ans;}int main(){ int n,d,cat=1; while(~scanf("%d%d",&n,&d)&&(n||d)) { bool flag=false; for(int i=0; i<n; i++) { scanf("%lf%lf",&land[i].x,&land[i].y); if(land[i].y>d)flag=true; //是否可以檢測到該島嶼 } if(flag)printf("Case %d: -1/n", cat++); else printf("Case %d: %d/n", cat++, solve(n,d*d)); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼玛县| 通州市| 大化| 乐至县| 锦州市| 资溪县| 怀来县| 凭祥市| 根河市| 鸡泽县| 平远县| 武穴市| 微山县| 平阴县| 饶河县| 绥棱县| 齐齐哈尔市| 临泽县| 惠东县| 密山市| 濮阳市| 栾城县| 泰兴市| 博白县| 宣武区| 西乌珠穆沁旗| 思南县| 台安县| 固镇县| 高唐县| 禹州市| 通化市| 南城县| 富平县| 肇州县| 琼结县| 清水县| 会昌县| 同德县| 逊克县| 永城市|