标签:des style http io os ar for sp strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 54295 | Accepted: 12208 |
Description
Input
Output
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0Sample Output
Case 1: 2 Case 2: 1题意:有一个坐标轴,在x轴的上方时海,下方是陆地,x轴为海岸线,海上有n个小岛,因为种种原因要在海岸线上装雷达,每个雷达的覆盖范围是以r为半径的圆,请用最少的雷达数覆盖所有的小岛,当无法完全覆盖时输出-1;思路:典型的贪心思想。以小岛为圆心以r为半径做园,计算出圆与x轴的左交点和右交点。左交点为x-sqrt(r*r-y*y),右交点为x+sqrt(r*r+y*y);然后对左交点从小到大排序,令初始雷达为最小岛屿的右交点,如果i点的左交点在雷达的右面,则需要重新装一个雷达,然后令当前点为新雷达的右交点,否则如果i点的右交点在当前雷达的左边,则把当前雷达的位置更新为该点的右交点。#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; struct node { double l,r; }point[1010]; int cmp(struct node a,struct node b)//对左交点进行排序; { return a.l<b.l; } int main() { int n,r,i; double x,y,t;//用t来储存当前雷达的位置 int cnt=1; while(~scanf("%d %d",&n,&r)) { if(n==0&&r==0) break; int ans=1;//记录安装雷达的个数 for(i=0;i<n;i++) { scanf("%lf %lf",&x,&y); point[i].l=x-sqrt(r*r-y*y);//当前点与x轴的左交点 point[i].r=x+sqrt(r*r-y*y);//当前点与x轴的右交点 if(y>r||y<0||r<=0)//列举出三种无法覆盖的情况 ans=-1; } sort(point,point+n,cmp);//排序 t=point[0].r;//令初始值为位置最小岛屿的右交点 for(i=1;i<n&&ans!=-1;i++) { if(point[i].l>t) { ans++; t=point[i].r; } if(point[i].r<t) { t=point[i].r; } } printf("Case %d: %d\n",cnt++,ans); } return 0; }
标签:des style http io os ar for sp strong
原文地址:http://blog.csdn.net/u013486414/article/details/40538103