poj 1328 Radar Installation
来源:互联网 发布:单片机有哪几部分组成 编辑:程序博客网 时间:2024/06/14 00:42
Radar Installation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 47664 Accepted: 10626
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.
假设海岸线是一个无限直线。陆地在海岸线的一边,海在另一侧。每一个小岛都是一个点,位于海的那边。然后雷达都安装在海岸线上,只能覆盖距离d,所以一个小岛在海上能够被雷达覆盖,除非他到雷达的距离小于等于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.
假设海岸线是一个无限直线。陆地在海岸线的一边,海在另一侧。每一个小岛都是一个点,位于海的那边。然后雷达都安装在海岸线上,只能覆盖距离d,所以一个小岛在海上能够被雷达覆盖,除非他到雷达的距离小于等于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.
我们使用直角坐标系,定义海岸线是x轴。海在x轴上方,x轴下面是陆地。给出各岛屿的未知,并给出了雷达的安装覆盖的距离,你的任务是写一个程序,找出雷达装置的最小数目,以覆盖所有的岛屿。请注意,一个岛的位置由它的xy坐标来表示。
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.
输入有若干组测试案例。每种情况下的第一行包含两个整数n(1<= n<= 1000)和d,n表示有多少岛屿,d表示雷达的有效距离。然后n行是岛屿的坐标,之后每组之间用一个空行隔开。
The input is terminated by a line containing pair of zeros
输入有若干组测试案例。每种情况下的第一行包含两个整数n(1<= n<= 1000)和d,n表示有多少岛屿,d表示雷达的有效距离。然后n行是岛屿的坐标,之后每组之间用一个空行隔开。
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
Source
Beijing 2002
快排加贪心,因为快排是自己写的,所以效率没有sort高,不过不用考虑cmp写的过程中需要强制转换double,其实如果用结构体保存,也就不用想那么多了。。。。然后贪心,首先将岛屿按x坐标排序,然后根据半径计算出来每个岛屿在x轴上的投影,记录投影左坐标和右坐标,然后首先雷达放在第一个岛屿的右投影坐标上,如果下一个岛屿的左坐标比雷达坐标大,则雷达坐标更新到下一个岛屿的右坐标,并增加一个雷达,如果小,则判断,下一个岛屿的右坐标比当前岛屿的右坐标大还是小,如果大,则不用管,当前雷达能覆盖,如果小,则更新当前雷达坐标为下一个岛屿的右坐标,画图可知。
#include <iostream>#include<algorithm>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;struct Coodi{ double x; double y;}coodi[1111];void qsort(int p, int r);int partition_q(int p, int r);int main(){ int n, d, case_n = 0, flag; double left[1111], right[1111], temp; int i, j, counter; freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%d%d", &n, &d) && !(n == 0 && d == 0)){ case_n ++; flag = 0; for (i = 0;i < n;i ++){ scanf("%lf%lf", &coodi[i].x, &coodi[i].y); if (coodi[i].y > d){ flag = 1; } } if (flag){ printf("Case %d: -1\n", case_n); continue; } qsort(0, n-1);// for (i = 0;i < n;i ++){// printf("%d = %lf %lf\n", i, coodi[i].x, coodi[i].y);// } for (i = 0;i < n;i ++){ left[i] = coodi[i].x - sqrt(d * d - coodi[i].y * coodi[i].y); right[i] = coodi[i].x + sqrt(d * d - coodi[i].y * coodi[i].y); } temp = right[0]; counter = 1; for (i = 0;i < n - 1;i ++){ if (left[i+1] > temp){ temp = right[i+1]; counter ++; }else if (right[i+1] < temp){ temp = right[i+1]; } } printf("Case %d: %d\n", case_n, counter); } return 0;}void qsort(int p, int r){ int q; if (p < r){ q = partition_q(p, r); qsort(p, q-1); qsort(q+1, r); }}int partition_q(int p, int r){ int i = p - 1; int j; struct Coodi temp; for (j = p;j < r;j ++){ if (coodi[j].x < coodi[r].x){ i ++; temp = coodi[i];coodi[i] = coodi[j];coodi[j] = temp; } } temp = coodi[i+1];coodi[i+1] = coodi[r];coodi[r] = temp; return i + 1;}
0 0
- POJ 1328 "Radar Installation"
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- 1328 poj Radar Installation
- poj 1328 Radar Installation
- POJ 1328 Radar Installation
- poj 1328 Radar Installation
- poj 1328 Radar Installation
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- POJ 1328 Radar Installation
- poj 1328 Radar Installation
- POJ-1328-Radar Installation
- poj 1328 Radar Installation
- POJ 1328 Radar Installation
- 《MQL4实用编程》读书笔记(1)
- 多态之人生百态
- 小度wifi当无线网卡
- ios中项目
- 实现不同网段的自动跳转访问
- poj 1328 Radar Installation
- C#委托详解(2):实现方式大全
- Linux设备驱动子系统第二弹 - SD卡
- Windows Azure的账户体系
- PHPDocumentor代码注释规范说明
- Windows线程同步--互锁变量访问(Interlocked)
- [Android]Handler的消息机制
- Binder 命令 BC_ 和BR_的16进制值
- 第一个PL/SQL过程,纪念一下