ACM: 初中数学 poj 1328
2016-05-19 23:17
501 查看
Radar Installation
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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轴上安装雷达 , 用最少的雷达个数覆盖全部的岛屿.
解题思路:
1.先将全部的岛屿,按照X的大小来排序.
2.从左往右放置雷达,每放置一个雷达,根据前面一个雷达的位置判断,前一个雷达覆盖的点.
3.放置雷达有一个原则就是: 尽量往右放.
4.再根据后面的点并且在雷达位置左面的点,把雷达向左移.一个雷达经过了移的过程,
就一定是能覆盖左面的岛.
5.最后是在最小的结束点完成,结果要加1.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAX 1005
struct node
{
int x ,
y;
}islands[MAX];
int n,dist;
int cmp(const void *a,const void *b)
{
return
(*(node*)a).x - (*(node*)b).x;
}
double getX(node point)
{
return
point.x + sqrt(dist*dist-point.y*point.y);
}
int result()
{
int ans =
1;
double x =
getX(islands[0]);
for(int i =
1; i < n; ++i)
{
double t = getX(islands[i]);
if(islands[i].x < x
&& x > t)
{
x = t;
continue;
}
if( (islands[i].y*islands[i].y +
(islands[i].x-x)*(islands[i].x-x)) <= dist *dist
)
continue;
x = getX(islands[i]);
ans++;
}
return
ans;
}
int main()
{
int num =
1;
//
freopen("input.txt","r",stdin);
while(scanf("%d
%d",&n,&dist) != EOF)
{
if(n == 0 && dist
== 0)
break;
int flag = 0;
for(int i = 0; i < n; ++i)
{
scanf("%d
%d",&islands[i].x,&islands[i].y);
if(islands[i].y > dist)
flag = 1;
}
if(flag == 1)
printf("Case %d: -1\n",num++);
else
{
qsort(islands,n,sizeof(islands[0]),cmp);
printf("Case %d: %d\n",num++,result());
}
getchar();
}
return
0;
}
相关文章推荐
- hdu_4714_Tree2cycle(树形DP)
- ACM: 简单数学推导+挑战平台的精度…
- C++中的构造函数
- ACM: 一题DFS(深搜,剪枝) poj 1011
- ACM: 一题STL map题 poj 1002 (bt…
- 20160519 学习日志
- Android ListFragment 中给LinearLayout添加点击事件
- hdu_4714_Tree2cycle(树形DP)
- IIS中报错弹出调试,系统日志-错误应用程序名称: w3wp.exe,版本: 8.5.9600.16384,时间戳: 0x5215df96(360主机卫士)
- ACM: 图论题 poj 1062 (邻接矩阵+d…
- ACM: 图论题 poj 2253 (poj上的dou…
- ACM: 图论题 poj 1125 (题意读好久…
- ACM: 图论题 poj 2240 (floyd + ST…
- ACM: 图论题 poj 1789 (一次AC的水…
- ACM: 图论题 poj2485 (crazy now~!…
- ACM: 图论题 BFS + STL + priority…
- ACM: 方向dfs + bfs求最短路 poj 3…
- ACM: 一题组合数学题 poj1850 (头…
- ACM: 一题数论题(扩展欧几里得定理…
- ACM: 一题组合数学题 poj1942 (细…