Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这样对不对呢?排序以后,开一个数组,初始时候数组里面只有(-oo,+oo),然后从排序以后的区间列表中依次选取区间,和新数组中最后一个元素求交集,如果不能相交就把这个区间插入到新数组的最后。。取选完了以后,新数组中的元素个数就是答案。。。。 /* ID: talenth1 PROG: p1328 LANG: C++ */ #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn=1001; const double eata=1e-7,oo=1e100; struct space{ double left,right; }; space area[maxn]; int n; double d; int datain() { double x,y,dx; bool signal=false; for(int i=1;i<=n;i++){ cin>>x>>y; if(y>d)signal=true; dx=sqrt(d*d-y*y); area[i].left=x-dx; area[i].right=x+dx; } if(signal){ cout<<"-1"<<endl; return -1; } return 0; } int mycmp(const void *e1,const void *e2) { double xl1,xr1,xl2,xr2; xl1=((space*)e1)->left; xr1=((space*)e1)->right; xl2=((space*)e2)->left; xr2=((space*)e2)->right; if(abs(xl1-xl2)>=eata){ if(xl1<xl2)return -1; else return 1; } else{ if(xr1<xr2)return -1; else return 1; } } void work() { qsort(&area[1],n,sizeof(area[1]),mycmp); vector<space> used; space tmp={-oo,+oo}; used.push_back(tmp); for(int i=1;i<=n;i++){ if(area[i].left-eata<used[used.size()-1].right){ used[used.size()-1].left=max(used[used.size()-1].left, area[i].left); used[used.size()-1].right=min(used[used.size()-1].right, area[i].right); } else used.push_back(area[i]); } cout<<used.size()<<endl; } int main() { freopen("p1328.in","r",stdin); freopen("p1328.out","w",stdout); int ti=0,signal; while(1){ cin>>n>>d; if(n==0)break; cout<<"Case "<<++ti<<": "; signal=datain(); if(signal)continue; work(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator