贪心Poj1328
题目:http://poj.org/problem?id=1328 注意 输出 Case :这里是有个空格的。。和之前序列想法差不多 尽可能 向一边贪心。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> using namespace std; struct Node { double l;double r; }node[11111]; double gao(double r,double x ) { return sqrt(r*r-x*x); } int cmp(const Node &a,const Node &b) { return a.r<b.r; } int main() { int n,d; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); double x,y;int sum=1; while(scanf("%d%d",&n,&d),n||d){ int jiba=0; for(int i=0;i<n;i++){ scanf("%lf%lf",&x,&y); if(y>d||d<0||y<0){ jiba=1;continue; } double len=gao(d,y); double l,r; l=x-len;r=x+len; node[i].l=l;node[i].r=r; } sort(node,node+n,cmp); double ret[11111]; int ans=0; for(int i=0;i<n;i++){ int flag=0; for(int j=0;j<ans;j++){ if(node[i].l<=ret[j]&&node[i].r>=ret[j]){ flag=1;break; } } if(!flag){ ret[ans++]=node[i].r; } } printf("Case %d: ",sum++); if(jiba) printf("-1\n"); else printf("%d\n",ans); } return 0; }