hdu2297Run(凸包的巧妙应用)
很巧妙的一道题,参考
把距离和速度分别作为x和y坐标,以斜率代表追赶速率,简直炫酷~
具体看上面的博客,画的很清楚,就不再抄写一遍了。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 50010 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19 double x,y; 20 point(double x=0,double y =0 ):x(x),y(y){} 21 }p[N],ch[N]; 22 typedef point pointt; 23 point operator -(point a,point b) 24 { 25 return point(a.x-b.x,a.y-b.y); 26 } 27 double cross(point a,point b) 28 { 29 return a.x*b.y-a.y*b.x; 30 } 31 int dcmp(double x) 32 { 33 if(fabs(x)<eps) return 0; 34 else return x<0?-1:1; 35 } 36 double mul(point p0,point p1,point p2) 37 { 38 return cross(p1-p0,p2-p0); 39 } 40 double dis(point a) 41 { 42 return sqrt(a.x*1.0*a.x+a.y*1.0*a.y); 43 } 44 bool cmp(point a,point b) 45 { 46 if(dcmp(mul(p[0],a,b))==0) 47 return dis(a-p[0])<dis(b-p[0]); 48 else 49 return dcmp(mul(p[0],a,b))>0; 50 } 51 int Graham(int n) 52 { 53 if(n<2) return n; 54 int i,k = 0,top; 55 point tmp; 56 for(i = 0 ; i < n; i++) 57 { 58 if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x)) 59 k = i; 60 } 61 if(k!=0) 62 { 63 tmp = p[0]; 64 p[0] = p[k]; 65 p[k] = tmp; 66 } 67 sort(p+1,p+n,cmp); 68 ch[0] = p[0]; 69 ch[1] = p[1]; 70 top = 1; 71 for(i = 2; i < n ; i++) 72 { 73 while(top>0&&dcmp(mul(ch[top-1],ch[top],p[i]))<=0) 74 top--; 75 top++; 76 ch[top] = p[i]; 77 } 78 return top; 79 } 80 int main() 81 { 82 int t,i,n; 83 cin>>t; 84 while(t--) 85 { 86 scanf("%d",&n); 87 for(i = 0 ; i < n; i++) 88 scanf("%lf%lf",&p[i].y,&p[i].x); 89 int m = Graham(n); 90 int maxx = -INF,tx,maxy = -INF,ty; 91 if(n<2) 92 { 93 cout<<"1\n"; 94 continue; 95 } 96 for(i = 0 ; i <= m ; i++) 97 { 98 //cout<<ch[i].x<<" "<<ch[i].y<<endl; 99 if(ch[i].x>maxx) 100 { 101 tx = i; 102 maxx = ch[i].x; 103 } 104 if(ch[i].y>maxy) 105 { 106 ty = i; 107 maxy = ch[i].y; 108 } 109 } 110 cout<<ty-tx+1<<endl; 111 } 112 return 0; 113 }