2017-2018 ACM-ICPC Southeast Regional Contest (Div. 1) F.Move Away 几何

来源:互联网 发布:算法设计最值 编辑:程序博客网 时间:2024/06/05 09:02

http://codeforces.com/gym/101617/attachments

题意:这个Tom要搬出家里,他想离父母越远越好,但是他有n个朋友,他搬新家的位置要保证离这些朋友不超过他们之间最远的距离。问最后离父母家最多有多远。

输入:第一行一个n,下面n行,朋友的坐标,离第i个朋友最远的距离。

输出离父母(0,0)的距离,保留3位小数。


做法:圆与圆之间的交点,一定是可能的坐标,因为要满足点在所有圆里面,那么这个点至少是两个圆之间的交点,另外对于一个圆最远的距离是圆心+半径。

那么我需要处理两两圆相交的情况的所有交点,判他们是不是在所有圆范围里面,如果是就更新答案。


///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.///             __.'              ~.   .~              `.__///           .'//                  \./                  \\`.///        .'//                     |                     \\`.///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.///     .'//.-"                 `-.  |  .-'                 "-.\\`.///   .'//______.============-..   \ | /   ..-============.______\\`./// .'______________________________\|/______________________________`.#pragma GCC optimize(2)#pragma comment(linker, "/STACK:102400000,102400000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)#define S_1(x) scan_d(x)#define S_2(x,y) scan_d(x),scan_d(y)#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_back#define sz size()typedef long long LL;typedef pair <int, int> ii;const int INF=~0U>>1;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=2e4+10;const int maxx=1e3+10;const double EPS=1e-8;const double eps=1e-8;const int mod=19260817;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;//void readString(string &s)//{//static char str[maxn];//scanf("%s", str);//s = str;//}int n;int dcmp(db x){if(fabs(x)<eps) return 0;else return x<0?-1:1;}struct Point{db x,y;Point(db x=0,db y=0):x(x),y(y){ }Point operator + (Point q){ return Point(x+q.x,y+q.y);}  Point operator - (Point q){ return Point(x-q.x,y-q.y);}      Point operator * (double q){ return Point(x*q,y*q);}bool operator ==(const Point &e)const{return dcmp(x-e.x)==0&&dcmp(y-e.y)==0;}db len2(){return x*x+y*y;}db len(){return sqrt(x*x+y*y);}}Q[maxn];typedef Point Vector;db Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}db Length(Vector A){return sqrt(Dot(A,A));}db angle(Vector v){return atan2(v.y,v.x);}db Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } struct Line  {      Point P;    //直线上一点      Vector v; //方向向量(半平面交中该向量左侧表示相应的半平面)      double ang; //极角,即从x正半轴旋转到向量v所需要的角(弧度)      Line() { }  //构造函数      Line(const Line& L): P(L.P), v(L.v), ang(L.ang) { }      Line(const Point& P, const Vector& v): P(P), v(v) { ang = atan2(v.y, v.x); }        bool operator < (const Line& L) const//极角排序      {          return ang < L.ang;      }      Point point(double t) { return P + v*t; }  };struct Circle  {      Point c;      double r;        Circle(){}      Circle(Point c, double r):c(c), r(r){}    inline void input()      {          scanf("%lf%lf%lf",&c.x,&c.y,&r);      }     Point point(double a)      {          return Point(c.x + r * cos(a), c.y + r * sin(a));      }  }C[maxn]; double DistanceToLine(Point P, Point A, Point B)  {      Vector v1 = B - A, v2 = P - A;      return fabs(Cross(v1, v2) / Length(v1)); //不取绝对值,得到的是有向距离  }  int getLineCircleIntersecion(Line l, Circle C, double& t1, double& t2, vector<Point>& sol)  {      double a = l.v.x;      double b = l.P.x - C.c.x;      double c = l.v.y;      double d = l.P.y - C.c.y;      double e = a * a + c * c;      double f = 2 * (a * b + c * d);      double g = b * b + d * d - C.r * C.r;      double delta = f * f - 4 * e * g;      double dist = DistanceToLine(C.c, l.P, l.P+l.v);      if(dcmp(dist - C.r) == 0)      {      //相切,此处需特殊判断,不能用delta          t1 = t2 = -f / (2 * e);          sol.push_back(l.point(t1));          return 1;      }      if(dcmp(delta) < 0) return 0;       //相离      else      {       //相交          t1 = (-f - sqrt(delta)) / (2 * e); sol.push_back(l.point(t1));          t2 = (-f + sqrt(delta)) / (2 * e); sol.push_back(l.point(t2));          return 2;      }  } int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point>& sol){db d=Length(C1.c-C2.c);if(dcmp(d)==0){if(dcmp(C1.r-C2.r)==0) return -1;return 0;}if(dcmp(C1.r+C2.r-d)<0) return 0;if(dcmp(fabs(C1.r-C2.r)-d)>0) return 0;db a=angle(C2.c-C1.c);db da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));Point p1=C1.point(a-da),p2=C1.point(a+da);sol.pb(p1);if(p1==p2) return 1;sol.pb(p2);return 2;}db dis(Point a,Point b){return (a-b).len();}bool check(Point p){for(int i=1;i<=n;i++)if(dcmp(dis(p,C[i].c)-C[i].r)>0) return 0;return 1;}vector<Point>v;void solve(){s_1(n);db minc=INF;FOR(1,n,i){C[i].input();db t1,t2;if(C[i].c==(Point){0.0,0.0}){minc=min(minc,C[i].r);n--,i--;}else getLineCircleIntersecion(Line(C[i].c,C[i].c),C[i],t1,t2,v);}if(n==0){printf("%.3f\n",C[1].c.len()+C[1].r);return ;}FOR(1,n,i)FOR(i+1,n,j)int xx=getCircleCircleIntersection(C[i],C[j],v);db ans=0;//cout<<minc<<endl;for(int i=0;i<v.size();i++){if(check(v[i])) ans=max(ans,v[i].len());ans=min(ans,minc);}printf("%.3f\n",ans);}int main(){    //freopen( "in.txt" , "r" , stdin );    //freopen( "data.txt" , "w" , stdout );    int t=1;    //init();    //s_1(t);    for(int cas=1;cas<=t;cas++)    {        //printf("Case #%d:\n",cas);        solve();    }}



阅读全文
0 0
原创粉丝点击