高效算法设计专项:UVa 10691
2013-07-09 23:47
507 查看
这道题可以转化为区间选点问题,即给定一些区间,找到最少的点使得每个区间至少存在一个点。只不过这道题上的区间是在一个圆上的,注意到n小于200,可以枚举开始的区间,然后分别进行一次贪心即可,时间复杂度为O(n^2)。只不过由于是在圆上,所以有很多细节需要注意,WA了n炮才调过了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=410; const double eps=1e-6; const double pi=acos(-1.0); int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } int n,m; double d; struct interval { double s,e; interval(double s=0,double e=0):s(s),e(e){} bool operator<(const interval& tmp) const { if(dcmp(e-tmp.e)<0) return true; if(dcmp(e-tmp.e)==0) { double s1=dcmp(e-s)<0?s-2*pi:s; double s2=dcmp(tmp.e<tmp.s)<0?tmp.s-2*pi:tmp.s; if(dcmp(s1-s2)>0) return true; } return false; } bool operator==(const interval& tmp) const { return dcmp(e-tmp.e)==0&&dcmp(s-tmp.s)==0; } } p[maxn]; double length(double x,double y) { return sqrt(x*x+y*y); } double formal(double rad) { if(dcmp(rad-2*pi)>=0) return rad-2*pi; if(dcmp(rad)<0) return rad+2*pi; return rad; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%lf",&n,&d); m=0; for(int i=0;i<n;i++) { double x,y; scanf("%lf%lf",&x,&y); if(dcmp(length(x,y)-d)<=0) continue; double angle=asin(d/length(x,y)); p[m++]=interval(formal(atan2(y,x)-angle),formal(atan2(y,x)+angle)); } sort(p,p+m); m=unique(p,p+m)-p; for(int i=0;i<m;i++) { p[i+m]=p[i]; } int ans=m; for(int i=0;i<m;i++) { if(i&&dcmp(p[i].e-p[i-1].e)==0) continue; int tmp=1,pre=i; for(int j=i+1;j<m+i;j++) { double s=dcmp(p[j].e-p[j].s)<0?p[j].s-2*pi:p[j].s; if(dcmp(s-p[pre].e)<=0) continue; else { pre=j; tmp++; } } ans=min(ans,tmp); double v=p[i].e+2*eps; for(int j=0;j<2*m;j++) p[j]=interval(formal(p[j].s-v),formal(p[j].e-v)); } printf("%d\n",ans); } return 0; }
相关文章推荐
- 高效算法设计专项:UVa 11572
- 高效算法设计专项:UVa 10125
- 高效算法设计专项:UVa 10730
- 高效算法设计专项:UVa 10391
- 高效算法设计专项:UVa 10827
- 高效算法设计专项:UVa 10810
- 高效算法设计专项:UVa 10535
- 高效算法设计专项:UVa 11054
- 【高效算法设计——双向扫描】 UVa 1442 Cave
- 【高效算法设计——递归】 UVa 1608 Non-boring sequences
- (高效算法设计)之高维问题 废料堆 Garbage heap Uva 10755
- ( 设计高效算法 ) 年龄排序 Age Sort Uva 11462
- 【高效算法设计——跳跃枚举】Uva 11093 Just Finish it up
- 【高效算法设计——滑动窗口】 UVa 12174 Shuffle
- 高效算法设计专项:LA 4726
- 【高效算法设计——等价转换】UVa 11054
- 【高效算法设计——滑动窗口】UVa 11572 Unique Snowflakes
- 【高效算法设计】UVa120 Stack of Flapjacks
- 高效算法设计专项:LA 2689
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers