poj1328
一、题意:有n个小岛,坐标为(x,y)。以x轴为海岸线,在海岸线上布置雷达,雷达能覆盖半径为d的圆形区域。求最少用多少个雷达能覆盖所有的小岛
二、思路:以小岛为圆心,d为半径作圆,其与x轴会有两个交点。这两个交点间的线段,就是满足这题小岛要求的雷达坐标。然后将从这个线段从左到右排序,有交集的线段就表示这两个小岛可以共用一个雷达,从而转换成一个区间贪心的问题。
三、代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include"iostream" #include"stdio.h" #include"algorithm" #include"cmath" using namespace std; int n,d; struct Node { double x,y; }; struct InterSection { double x1,x2; }; Node cor[1005]; InterSection segment[1005]; bool Cmp( const InterSection a, const InterSection b) { if (a.x2!=b.x2) return a.x2<b.x2; else return a.x1>b.x1; } void CalIntersection( int i) { segment[i].x1= sqrt (d*d-cor[i].y*cor[i].y)+cor[i].x; segment[i].x2=cor[i].x- sqrt (d*d-cor[i].y*cor[i].y); } double Min( double a, double b) { return a<b?a:b; } int Solve() { double left=segment[0].x2,right=segment[0].x1; int ans=1,i=1; while (i<n) { while (i<n&&segment[i].x2<=right) { left=segment[i].x2; right=Min(right,segment[i].x1); i++; } if (i<n) { ans++; left=segment[i].x2; right=segment[i].x1; } } return ans; } int main() { int cnt=0; while (cin>>n>>d,n||d) { int ans=0; for ( int i=0;i<n;i++) { cin>>cor[i].x>>cor[i].y; if (cor[i].y>d) ans=-1; CalIntersection(i); } if (ans!=-1){ sort(segment,segment+n,Cmp); ans=Solve(); } cout<< "Case " <<++cnt<< ": " <<ans<<endl; } return 0; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步