Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 69114 | Accepted: 15506 |
Description
Input
Output
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1
题目大意:在平面上有n个点,告诉你圆的半径为d,求在x轴上最少需要几个点可以使得以这些点为圆心,半径为d的圆覆盖所有的n个点
思路:对于每一个点,找出他所对应的最左雷达位置与最右雷达位置,便有n个区间,从左向右遍历判断下一个区间能否与上一个区间的交集重合,若能重合则可共用同一个雷达。若能公用一个雷达则更新交集范围。注意d<0的情形,另外若|y|>d则可判断-1
实现代码:
//************************************************************************// //*Author : Handsome How *// //************************************************************************// //#pragma comment(linker, "/STA CK:1024000000,1024000000") #pragma warning(disable:4996) #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <ctime> #include <cassert> #define fur(i,a,b) for(int i=(a);i<=(b);i++) #define furr(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; typedef long long LL; const int MAXN = 1000 + 20; struct Node { double l; double r; }len[MAXN]; bool compare(Node A, Node B) { if (A.l != B.l)return A.l < B.l; else return A.r < B.r; } int main() { //freopen("E:\\data.in", "r", stdin); //freopen("E:\\data.out", "w", stdout); int n,kase=0; double d; while (scanf("%d%lfd", &n,&d) != EOF) { if(n == 0 && d == 0)break; printf("Case %d: ",++kase); bool legal = true; double x, y,t; fur(i, 0, n-1) { scanf("%lf%lf", &x,&y); if (y > d || y < d*(-1.0))legal = false; //最高点能否被雷达范围覆盖 t = sqrt(d*d - y*y); len[i].l = x - t; len[i].r = x + t; } if (d < 0) { printf("-1\n"); continue; } if(legal==false) { printf("-1\n"); continue; } sort(len, len + n, compare); //fur(i, 0, n - 1)printf("%lf %lf\n", len[i].l, len[i].r); int last = 0,cnt=1; fur(i, 1, n - 1) { if (len[i].l <= len[last].r) { len[last].l = max(len[last].l, len[i].l); len[last].r = min(len[last].r, len[i].r); continue; //交集的更新 } last = i; //新开一个区间 cnt++; } printf("%d\n",cnt); } return 0; }