POJ1328解题报告 (贪心)
来源:互联网 发布:打电话变声软件男变女 编辑:程序博客网 时间:2024/06/05 02:17
Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 87836 Accepted: 19690
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
解题思路:
找到以该岛屿为圆心的圆与x轴交点所在的区间。
我们就可以计算出所有岛屿的雷达所在的区间,得到一个区间数组。
我们将这个数组按照区间左部分进行排序,那么重叠部分就表明这些岛屿的雷达可以共用一个。从而计算出最终解。
AC代码
import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;public class Main { /** * @param args */ static Main poj=new Main(); static Node[] node; static Region region[]; static int n,d; class Node{ int begin; int end; } class Region{ double x,y; } class cmp implements Comparator<Region>{ @Override public int compare(Region o1, Region o2) { // TODO Auto-generated method stub if(o1.x>o2.x){ return 1; }else if(o1.x<o2.x){ return -1; }else{ return 0; } } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); int t=1; while(scan.hasNext()){ n=scan.nextInt(); d=scan.nextInt(); if(n==0&&d==0){ break; } node=new Node[n]; region=new Region[n]; for(int i=0;i<n;i++){ node[i]=poj.new Node(); node[i].begin=scan.nextInt(); node[i].end=scan.nextInt(); } int min = calMin(); System.out.println("Case "+t+": "+min); t++; scan.nextLine(); } } private static int calMin() { // TODO Auto-generated method stub int t=0; for(int i=0;i<n;i++){//计算每个岛屿在横坐标的区域 Node ss=node[i]; if(ss.end<0||ss.end>d||d<0){ return -1; } double x=Math.sqrt(Math.pow(d, 2)-Math.pow(ss.end, 2)); region[t]=poj.new Region(); region[t].x=ss.begin-x; region[t].y=ss.begin+x; t++; } //排序 Arrays.sort(region,0,t,poj.new cmp()); int num = 0; double right = -1; for(int i=0;i<t;i++){ Region s=region[i]; if(i==0){ num++; right=s.y; } else{ if(s.x<=right){ if(s.y<right){ right=s.y; } }else{ num++; right=s.y; } } } return num; }}
- POJ1328解题报告 (贪心)
- poj1328解题报告(贪心、线段交集)
- poj1328解题报告
- POJ1328解题报告
- poj1328解题报告
- poj1328解题报告.
- POJ1328解题报告
- poj1328 Radar Installation 解题报告
- POJ1328贪心
- POJ1328--贪心
- poj1328(贪心)
- poj1328 贪心
- poj1328 贪心
- poj1328 贪心
- poj1328贪心
- POJ1328(贪心)
- poj1328贪心
- poj1328 贪心
- 织梦cms模板搭建网站,需要做哪些准备工作?
- Markdown-Math语法
- 1003. Hangover
- BOS签名算法的PHP demo
- 不积跬步,无以至千里
- POJ1328解题报告 (贪心)
- 织梦cms源码下载:响应式3D打印设备公司网站
- Elasticsearch顶尖高手系列课程
- SparkSteaming进行UV统计
- 啥也不想干的时候怎么办呢?
- [linux] tee 命令
- 用Python实现语音合成
- protobuf入门教程(二):消息类型
- 1.2 字符串包含