POJ1328(贪心)
题目
Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 150055 Accepted: 33255
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.
题意
在x轴的上方有若干个小岛,可以在x轴上布置雷达,给出了雷达的覆盖范围,要我们找出布置雷达的最小数量。
分析
第一反应是直接使用贪心,将小岛按照x坐标由小到大排序,从最左边的小岛开始,设定一个距离它最远的可以覆盖到该岛的雷达。遍历所有小岛,当前小岛无法被上一个雷达覆盖,则重复第一次操作。
敲代码一气呵成,然后成功wa了。(+﹏+)
冥思苦想后,发现问题出在排序的方法上,不应该按x坐标进行排序,x坐标最左边的小岛对应的雷达可能不是真正最左的那个,还要考虑y轴因素,所以直接计算每个小岛对应的雷达位置,按照雷达位置给小岛排序。
代码
第一次错误代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
struct gg
{
int x, y;
bool operator < (const gg a)const
{
return x < a.x;
}
};
int main()
{
int n, d, num = 0;
while (scanf("%d%d", &n, &d) && (n || d))
{
num ++ ;
bool flag = false;
gg g[1010];
for (int i = 0; i < n; i ++ )
{
scanf("%d%d", &g[i].x, &g[i].y);
if (g[i].y > d) flag = true;
}
if (flag)
{
printf("Case %d: -1\n", num);
continue;
}
sort(g,g + n);
double z = (double)(sqrt(d*d - g[0].y*g[0].y) + g[0].x);
int res = 1;
for (int i = 0; i < n; i ++ )
{
int x = g[i].x, y = g[i].y;
if ((double)((x - z)*(x - z) + y*y) <= d*d) continue;
else
{
z = (double)(sqrt(d*d - y*y) + x);
res ++ ;
}
}
printf("Case %d: %d\n", num, res);
}
return 0;
}
正确代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
struct gg
{
int x, y;
double leida;
bool operator < (const gg a)const
{
return leida < a.leida;
}
};
int main()
{
int n, d, num = 0;
while (scanf("%d%d", &n, &d) && (n || d))
{
num ++ ;
bool flag = false;
gg g[1010];
for (int i = 0; i < n; i ++ )
{
scanf("%d%d", &g[i].x, &g[i].y);
if (g[i].y > d) flag = true;
}
if (flag)
{
printf("Case %d: -1\n", num);
continue;
}
for (int i = 0; i < n; i ++ )
{
g[i].leida = (double)(sqrt(d*d - g[i].y*g[i].y) + g[i].x);
}
sort(g,g + n);
double z = g[0].leida;
int res = 1;
for (int i = 0; i < n; i ++ )
{
int x = g[i].x, y = g[i].y;
if ((double)((x - z)*(x - z) + y*y) <= d*d) continue;
else
{
z = g[i].leida;
res ++ ;
}
}
printf("Case %d: %d\n", num, res);
}
return 0;
}