1. 程式人生 > >2017.10.2 國慶清北 D2T2 樹上搶男主

2017.10.2 國慶清北 D2T2 樹上搶男主

main none foo double include const 開始 getc opened

技術分享

技術分享

技術分享
  1 /*
  2 一開始寫的是走點之後不能走了,這樣菊花圖會被卡。。但是,為什麽爆0? 
  3 先求lca,然後在兩個人搶路,搶完路後各自走各自的。 
  4 */
  5 #include<cstdio>
  6 #include<cstdlib>
  7 #include<cstring>
  8 #include<cmath>
  9 #include<algorithm>
 10 using namespace std;
 11 const double eps=1e-8;
 12 int sgn(double a)
 13
{ 14 if (fabs(a)<eps) return 0; 15 else 16 { 17 if (a>0.0) return 1; 18 else return -1; 19 } 20 } 21 struct point 22 { 23 double x,y; 24 point(){} 25 point(double a,double b) 26 { 27 x=a;y=b; 28 } 29 void init() 30 {
31 scanf("%lf%lf",&x,&y); 32 } 33 point operator+(const point &a)const 34 { 35 point ans; 36 ans.x=x+a.x; 37 ans.y=y+a.y; 38 return ans; 39 } 40 point operator-(const point &a)const 41 { 42 point ans;
43 ans.x=x-a.x; 44 ans.y=y-a.y; 45 return ans; 46 } 47 point operator*(const double &a)const 48 { 49 point ans; 50 ans.x=x*a; 51 ans.y=y*a; 52 return ans; 53 } 54 void print() 55 { 56 printf("%lf %lf\n",x,y); 57 } 58 }v,p,w1,w2,m1,m2; 59 double cross(point a,point b) 60 { 61 return a.x*b.y-a.y*b.x; 62 } 63 double dot(point a,point b) 64 { 65 return a.x*b.x+a.y*b.y; 66 } 67 bool cross(point p1,point p2,point p3,point p4) 68 { 69 if (sgn(cross(p2-p1,p3-p1))*sgn(cross(p2-p1,p4-p1))==1) return false; 70 if (sgn(cross(p4-p3,p1-p3))*sgn(cross(p4-p3,p2-p3))==1) return false; 71 if (sgn(max(p1.x,p2.x)-min(p3.x,p4.x))==-1) return false; 72 if (sgn(max(p1.y,p2.y)-min(p3.y,p4.y))==-1) return false; 73 if (sgn(max(p3.x,p4.x)-min(p1.x,p2.x))==-1) return false; 74 if (sgn(max(p3.y,p4.y)-min(p1.y,p2.y))==-1) return false; 75 return true; 76 } 77 point getcross(point p1,point p2,point p3,point p4) 78 { 79 double a=p2.y-p1.y; 80 double b=p1.x-p2.x; 81 double c=-p1.x*p2.y+p1.y*p2.x; 82 double d=p4.y-p3.y; 83 double e=p3.x-p4.x; 84 double f=-p3.x*p4.y+p3.y*p4.x; 85 double x=(b*f-c*e)/(a*e-b*d); 86 double y=(a*f-c*d)/(b*d-a*e); 87 return point(x,y); 88 } 89 point calcfoot(point p1,point p2,point p3) 90 { 91 double ratio=dot(p1-p2,p3-p2)/dot(p3-p2,p3-p2); 92 return p2+(p3-p2)*ratio; 93 } 94 bool check() 95 { 96 if (!cross(v,p,w1,w2)) 97 { 98 if (!cross(v,p,m1,m2)) return true; 99 if (sgn(cross(m1-v,m2-v))==0 && sgn(cross(m1-p,m2-p)==0)) return true; 100 } 101 if (sgn(cross(m2-m1,v-m1))*sgn(cross(m2-m1,p-m1))==1) 102 { 103 point foot=calcfoot(p,m1,m2); 104 foot=foot*2.0-p; 105 if (cross(v,foot,m1,m2)) 106 { 107 foot=getcross(v,foot,m1,m2); 108 if (!cross(v,foot,w1,w2) && !cross(foot,p,w1,w2)) return true; 109 } 110 } 111 return false; 112 } 113 int main() 114 { 115 freopen("b.in","r",stdin); 116 freopen("b.out","w",stdout); 117 v.init(); 118 p.init(); 119 w1.init(); 120 w2.init(); 121 m1.init(); 122 m2.init(); 123 if (check()) printf("YES\n"); 124 else printf("NO\n"); 125 return 0; 126 }
View Code

2017.10.2 國慶清北 D2T2 樹上搶男主