CJOJ P1893 【Nescafé29模拟赛】穿越七色虹

来源:互联网 发布:哪个新顶级域名好 编辑:程序博客网 时间:2024/05/17 06:33
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std; double h,x0;struct CH {double x,r;}ch[8];struct NCH {double x,y;}nch[8];bool cmp(CH a,CH b) {return a.x<b.x;}double qj(double bj) {return sqrt(bj*bj-h*h);}bool check(double x) {for(int i=1; i<=7; ++i) {//if(x+ch[i].r<h) return 0;  这一句不能加,因为某个在x0之后的彩虹的高度小于h也是合法的 if(x+ch[i].r<h && ch[i].x<=x0) return 0; nch[i].x=ch[i].x-qj(ch[i].r+x);   nch[i].y=ch[i].x+qj(ch[i].r+x);}double maxx=nch[1].y,minx=nch[1].x;for(int i=2; i<=7; ++i) {if(nch[i].x<=maxx) {if(maxx<nch[i].y) maxx=nch[i].y;if(minx>nch[i].x) minx=nch[i].x;}}if(maxx>=x0 && minx<=0) return 1;else return 0;}double bs() {double l=0,r=x0,mid;while(r-l>0.001) {mid=(l+r)/2.0;if(check(mid)) r=mid;else l=mid;}return mid;}int main() {cin>>h>>x0;//h:高度,x0:末位置 for(int i=1; i<=7; ++i)cin>>ch[i].x>>ch[i].r;//ch[].x:彩虹的圆心位置。ch[].r:彩虹的半径 sort(ch+1,ch+8,cmp);//ch按圆心位置排序 double ans=bs();//二分答案 printf("%.2lf", ans);return 0;}

1 0
原创粉丝点击