标签:acm algorithm poj greedy sort
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 59449 | Accepted: 13394 |
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个点,现在要你用最少的圆(圆心在x轴,半径为d)覆盖所有的点。
分析:贪心题。先预处理出每个点对应的圆心坐标区间,意思就是在这个区间内的任意一个点作为圆心坐标都能覆盖该点。然后根据区间右边界升序排序。然后就是区间有无交集的问题了。如果有交集,说明可以用同一个圆覆盖,如果没交集,ans+=1。
题目链接:http://poj.org/problem?id=1328
#include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<ctime> #include<cctype> #include<string> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 1000 + 5; struct Point{ double x; double y; }point[maxn]; int n,k=1; double d,X,Y; bool judge; bool cmp(Point a,Point b){ return a.y<b.y; } void input(){ judge=true; for(int i=0;i<n;i++){ scanf("%lf%lf",&X,&Y); if(fabs(Y)>d || d<=0){ judge=false; } else{ point[i].x=X-sqrt(d*d-Y*Y); point[i].y=X+sqrt(d*d-Y*Y); } } } void solve(){ printf("Case %d: ",k++); if(!judge || d<=0){ printf("-1\n"); return ; } sort(point,point+n,cmp); int ans=1; double maxy=point[0].y; for(int i=1;i<n;i++){ if(point[i].x>maxy){ maxy=point[i].y; ans++; } } printf("%d\n",ans); return ; } int main(){ while(scanf("%d%lf",&n,&d)!=EOF){ if(n==0&&d==0) break; input(); solve(); }return 0; }
POJ_1328_Radar Installation(greedy)
标签:acm algorithm poj greedy sort
原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/46128721