装雷达
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<tex2html_verbatim_mark> distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d<tex2html_verbatim_mark> .
We use Cartesian coordinate system, defining the coasting is the x<tex2html_verbatim_mark> -axis. The sea side is above x<tex2html_verbatim_mark> -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<tex2html_verbatim_mark> - y<tex2html_verbatim_mark>coordinates.
Input
The input consists of several test cases. The first line of each case contains two integers n<tex2html_verbatim_mark>(1n1000)<tex2html_verbatim_mark> and d<tex2html_verbatim_mark> , where n<tex2html_verbatim_mark> is the number of islands in the sea and d<tex2html_verbatim_mark> is the distance of coverage of the radar installation. This is followed by n<tex2html_verbatim_mark> 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
思路:
贪心,找重复区间
源代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<string> 7 using namespace std; 8 #define maxn 1000+5 9 struct node 10 { 11 double left; 12 double right; 13 bool operator<(const node&a)const 14 { 15 return left < a.left; 16 } 17 }line[maxn]; 18 int main() 19 { 20 int n, d; 21 int flag, ans=0, k; 22 while (cin >> n >> d) 23 { 24 ans++; 25 flag = 1; 26 k = 0; 27 int x, y; 28 if (n == 0 && d == 0) 29 break; 30 for (int i = 0; i < n; i++) 31 { 32 cin >> x >> y; 33 34 if (flag == 0)continue; 35 if (y>d) 36 flag = 0; 37 else 38 { 39 /*Index = 0;*/ 40 line[i].left = (double)x- sqrt((double)d*d-(double)y * y); 41 line[i].right = (double)x + sqrt((double)d*d - (double)y * y); 42 43 } 44 } 45 if (flag == 0) 46 { 47 cout << "Case " << ans << ": -1" << endl; 48 continue; 49 } 50 sort(line, line + n); 51 k++; 52 double cur = line[0].right; 53 for(int i = 1; i < n; i++) 54 { 55 if (line[i].right < cur) 56 { 57 cur = line[i].right; 58 59 } 60 else if (cur<line[i].left) 61 { 62 cur = line[i].right; 63 k++; 64 } 65 66 } 67 68 cout << "Case " << ans << ": "<<k << endl; 69 } 70 return 0; 71 }
心得:
好吧,这个题目自己都还没完全搞懂,也是醉了~~~,好好研究,之后懂了再补上说明。