POJ 1328 Radar Installation 贪心
把每个点转成区间,排序,首先考虑最左边的区间[l1,r1],要覆盖住这个点则需要在这个区间里的某个位置x放一个雷达,显然尽量放在靠右端是最好的,因为可以同时被右边的其他区间覆盖,但是要被右边的区间[l2,r2]覆盖还需满足l2<=x<=r2,所以要x=min(x,r2)更新x的值。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); //freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } const int maxn = 10005; const double eps = 10e-8; typedef pair<double,double> pdd; pdd da[maxn]; double cal(int x,int y,int R) { if(abs(y)>R)return -1; return sqrt(R*R-y*y)+eps; } int main() { int n,R; int ca=0; while(scanf("%d%d",&n,&R)!=EOF&&(n||R)) { int sol=1; for(int i=0;i<n;i++) { int x,y; scanf("%d%d",&x,&y); double d=cal(x,y,R); if(d<=0){sol=0;continue;} da[i]=make_pair(x-d,x+d); } if(!sol) {printf("Case %d: %d\n",++ca,-1);continue;} sort(da,da+n); double pre=-INT_MAX; int cnt=0; for(int i=0;i<n;) { while(i<n&&da[i].first<=pre){pre=min(pre,da[i].second);i++;} if(i==n)break; pre=da[i].second; cnt++; i++; } printf("Case %d: %d\n",++ca,cnt); } return 0; }