计算几何初步
昨天学习了一下计算几何,各种公式欲仙欲死~
顺便吐槽一句,写计算几何是个体力活...
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 #include<map>
12 using namespace std;
13 #define LL long long
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<LL,LL>
17 #define up(i,j,n) for(LL i=(j);i<=(n);++i)
18 #define poi vec
19 #define ldb long double
20 LL read(){
21 LL x=0,f=1,ch=getchar();
22 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
23 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
24 return f*x;
25 }
26 const ldb eps=1e-14;
27 struct vec{
28 ldb x,y;
29 vec(ldb x=0,ldb y=0):x(x),y(y){}
30 }a,b,c;
31 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
32 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
33 vec operator*(vec a,ldb b){return vec(a.x*b,a.y*b);}
34 vec operator/(vec a,ldb b){return vec(a.x/b,a.y/b);}
35 int dcmp(ldb a){if(a<eps)return 0;return a<0?-1:1;}
36 bool operator==(vec a,vec b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
37 ldb dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
38 ldb len(vec a){return sqrt(dot(a,a));}
39 ldb ang(vec a,vec b){return acos(dot(a,b)/len(a)/len(b));}
40 ldb cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
41 void put(vec a){printf("%.6lf %.6lf ",a.x,a.y);}
42 void read(vec& a){a.x=read(),a.y=read();}
43 vec getpoi(vec a,vec b,vec c,vec d){
44 vec e=a-c;
45 ldb t=cro(d,e)/cro(b,d);
46 return a+b*t;
47 }
48 vec rot(vec a,ldb rad){return vec(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}
49 int main(){
50 freopen("dealing.in","r",stdin);
51 freopen("dealing.out","w",stdout);
52 int t=read();
53 while(t--){
54 read(a),read(b),read(c);
55 vec bc=c-b,ba=a-b,ca=a-c,ab=b-a,ac=c-a,cb=b-c;
56 ldb abc3=ang(bc,ba)/3;
57 vec bd=rot(bc,abc3);
58 vec cd=rot(ca,2*ang(cb,ca)/3);
59 vec d=getpoi(b,bd,c,cd);
60 vec e=getpoi(c,rot(ca,ang(bc*-1,ca)/3),a,rot(ab,ang(ab,ac)/3*2));
61 vec f=getpoi(b,rot(bc,abc3*2),a,rot(ab,ang(ab,ac)/3));
62 printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",d.x,d.y,e.x,e.y,f.x,f.y);
63 }
64 return 0;
65 }
很奇怪的风格...
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 #include<map>
12 using namespace std;
13 #define LL long long
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<int,int>
17 #define up(i,j,n) for(int i=(j);i<=(n);++i)
18 #define db double
19 const double eps=1e-10;
20 const int maxn=1010000,mod=998244353;
21 int read(){
22 int x=0,f=1,ch=getchar();
23 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
24 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
25 return f*x;
26 }
27 struct vec{
28 db x,y;
29 vec(db x=0,db y=0):x(x),y(y){}
30 };
31 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
32 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
33 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
34 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
35 int dcmp(db a){if(fabs(a)<eps)return 0;return a<0?-1:1;}
36 bool operator==(vec a,vec b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
37 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
38 db len(vec a){return sqrt(dot(a,a));}
39 db cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
40 vec getsection(vec a,vec b,vec c,vec d){
41 vec p=a-c;
42 db t=cro(d,p)/cro(b,d);
43 return p+b*t;
44 }
45 db are(vec a,vec b,vec c){
46 vec d=b-a,e=c-a;
47 return fabs(cro(d,e))/4;
48 }
49 int main(){
50 freopen(FILE".in","r",stdin);
51 freopen(FILE".out","w",stdout);
52 int n=read();
53 while(n--){
54 vec a,b,c,d,e,f;
55 scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
56 d=(c-b)/3+b;e=(a-c)/3+c;f=(b-a)/3+a;
57 vec be=e-b,cf=f-c,ad=d-a;
58 vec p=getsection(b,be,a,ad);
59 vec q=getsection(c,cf,b,be);
60 vec r=getsection(c,cf,a,ad);
61 printf("%.0lf\n",are(p,q,r));
62 }
63 return 0;
64 }
还有几个不太好记的函数:
db dtoline(vec p,vec a,vec b){return fabs(cro(p-a,p-b))/len(b-a);}
db dtoseg(vec p,vec a,vec b){
if(dcmp(dot(b-a,p-a))<0)return len(a-p);
else if(dcmp(dot(a-b,p-b)<0))return len(b-p);
else return dtoline(p,a,b);
}
vec linepro(vec p,vec a,vec b){
vec v=b-a;return a+v*(dot(p,v)/dot(v,v));
}
bool shifouxiangjiao(vec a1,vec a2,vec b1,vec b2){
db v1=cro(a2-a1,b1-a1),v2=cro(a2-a1,b2-a1),
v3=cro(b2-b1,a1-b1),v4=cro(b2-b1,a2-b1);
return dcmp(v1)*dcmp(v2)<0&&dcmp(v3)*dcmp(v4)<0;
}
bool shifouzaixianduanshang(vec p,vec a,vec b){
return dcmp(cro(a-p,b-p))==0&&dcmp(dot(a1-p,a2-p))<0;
}
做的第一道凸包题,怎么调也调不过去,结果发现模板错了...
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 #include<map>
12 using namespace std;
13 #define LL long long
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<int,int>
17 #define up(i,j,n) for(int i=(j);i<=(n);++i)
18 #define db double
19 #define poi vec
20 const double eps=1e-10;
21 const int maxn=10100,mod=998244353;
22 int read(){
23 int x=0,f=1,ch=getchar();
24 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
25 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
26 return f*x;
27 }
28 int T=0;
29 const db pi=acos(-1.0);
30 struct vec{
31 db x,y;
32 vec(db x=0,db y=0):x(x),y(y){}
33 };
34 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
35 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
36 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
37 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
38 int dcmp(db a){if(fabs(a)<=eps)return 0;return a<0?-1:1;}
39 bool operator==(vec a,vec b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
40 bool operator<(vec a,vec b){return a.x<b.x||(!dcmp(a.x-b.x)&&a.y<b.y);}
41 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
42 db len(vec a){return sqrt(dot(a,a));}
43 db cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
44 bool shifouzaixianduanshang(vec p,vec a,vec b){return dcmp(cro(a-p,b-p))==0&&dcmp(dot(a-p,b-p))<0;}
45 vec rot(vec a,db b){return vec(a.x*cos(b)-a.y*sin(b),a.x*sin(b)+a.y*cos(b));}
46 vec ang(vec a,vec b){return acos(dot(a,b)/len(a)/len(b));}
47 vec ch[maxn],a[maxn];
48 int qiutubao(int n){
49 int m=0,k=0;
50 sort(a+1,a+n+1);
51 up(i,1,n){
52 while(m>=2&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<=0)m--;
53 ch[++m]=a[i];
54 }
55 k=m;
56 for(int i=n-1;i>=1;i--){
57 while(m>=k+1&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<=0)m--;
58 ch[++m]=a[i];
59 }
60 if(n>1)m--;
61 return m;
62 }
63 db torad(db a){return a/180*acos(-1.0);}
64 int main(){
65 freopen(FILE".in","r",stdin);
66 freopen(FILE".out","w",stdout);
67 int T=read();
68 while(T--){
69 int n=read(),cnt=0;
70 double x,y,w,h,rad,are=0;
71 up(i,1,n){
72 scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&rad);
73 are+=w*h;vec o(x,y);
74 a[++cnt]=o+rot(vec(w/2,h/2),-torad(rad));
75 a[++cnt]=o+rot(vec(-w/2,-h/2),-torad(rad));
76 a[++cnt]=o+rot(vec(-w/2,h/2),-torad(rad));
77 a[++cnt]=o+rot(vec(w/2,-h/2),-torad(rad));
78 }
79 int m=qiutubao(cnt);
80 db area2=0;
81 up(i,2,m-1)area2+=cro(ch[i]-ch[1],ch[i+1]-ch[1]);area2/=2;
82 printf("%.1lf %%\n",are/area2*100);
83 }
84 return 0;
85 }
简单凸包问题;
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 #include<map>
12 using namespace std;
13 #define LL long long
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<int,int>
17 #define up(i,j,n) for(int i=(j);i<=(n);++i)
18 #define db double
19 #define poi vec
20 const double eps=1e-10;
21 const int maxn=1010000,mod=998244353,inf=1000000007;
22 int read(){
23 int x=0,f=1,ch=getchar();
24 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
25 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
26 return f*x;
27 }
28 struct vec{
29 db x,y;
30 vec(db x=0,db y=0):x(x),y(y){}
31 };
32 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
33 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
34 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
35 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
36 int dcmp(db a){if(fabs(a)<eps)return 0;return a<0?-1:1;}
37 bool operator<(const vec& a,const vec& b){return a.x<b.x||(!dcmp(a.x-b.x)&&a.y<b.y);}
38 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
39 db len(vec a){return sqrt(dot(a,a));}
40 db cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
41 vec a[maxn],ch[maxn];
42 int qiutubao(int n){
43 int m=0,k;
44 sort(a+1,a+n+1);
45 up(i,1,n){
46 while(m>1&&cro(ch[m-1]-ch[m-2],a[i]-ch[m-2])<0)m--;
47 ch[m++]=a[i];
48 }
49 k=m;
50 for(int i=n-1;i>=1;i--){
51 while(m>k&&cro(ch[m-1]-ch[m-2],a[i]-ch[m-2])<0)m--;
52 ch[m++]=a[i];
53 }
54 if(n>1)m--;
55 return m;
56 }
57 void bianchengliangdianshi(vec a,vec b,db& A,db& B,db& C){
58 if(!dcmp(a.x-b.x))return (void)(A=1,B=0,C=-a.x);
59 else {
60 db k=(a.y-b.y)/(a.x-b.x);
61 A=k,B=-1,C=a.y-k*a.x;
62 }
63 }
64 int main(){
65 freopen(FILE".in","r",stdin);
66 freopen(FILE".out","w",stdout);
67 int T=read();
68 up(t,1,T){
69 int n=read();
70 db X=0,Y=0;
71 up(i,1,n){
72 scanf("%lf%lf",&a[i].x,&a[i].y);
73 X+=a[i].x,Y+=a[i].y;
74 }
75 int m=qiutubao(n);
76 db A,B,C,ans=1e20*1.0,sum;
77 up(i,0,m-1){
78 bianchengliangdianshi(ch[i],ch[(i+1)%m],A,B,C);
79 sum=fabs(X*A+B*Y+n*C)/(sqrt(A*A+B*B));
80 ans=min(ans,sum);
81 }
82 printf("Case #%d: %.3lf\n",t,ans/n);
83 }
84 return 0;
85 }
判断两点集的凸包是否可以用一条直线分开;
需要判断凸包是否有相交和凸包是否有包含关系;
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 using namespace std;
12 #define ll int
13 #define db double
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<ll,ll>
17 #define up(i,j,n) for(ll i=(j);i<=(n);i++)
18 int read(){
19 ll x=0,f=1,ch=getchar();
20 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
21 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
22 return f*x;
23 }
24 const db eps=1e-10;
25 const int maxn=2000;
26 struct vec{
27 db x,y;
28 vec(db x=0,db y=0):x(x),y(y){}
29 };
30 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
31 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
32 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
33 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
34 int dcmp(db a){if(fabs(a)<eps)return 0;return a<0?-1:1;}
35 bool operator<(vec a,vec b){return a.x<b.x||(!dcmp(a.x-b.x)&&a.y<b.y);}
36 bool operator==(vec a,vec b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
37 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
38 db len(vec a){return sqrt(dot(a,a));}
39 db cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
40 bool dianzaixianduanshang(vec p,vec a,vec b){
41 return !dcmp(cro(a-p,b-p))&&dcmp(dot(a-p,b-p))<0;
42 }
43 int qiutubao(vec* a,vec* ch,int n){
44 int m=0,k;
45 sort(a+1,a+n+1);
46 up(i,1,n){
47 while(m>1&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<=0)m--;
48 ch[++m]=a[i];
49 }
50 k=m;
51 for(int i=n-1;i>=1;i--){
52 while(m>k&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<=0)m--;
53 ch[++m]=a[i];
54 }
55 if(n>1)m--;
56 return m;
57 }
58 bool dianzaiduobianxingneibu(vec p,vec* a,int n){
59 int wn=0;
60 up(i,0,n-1){
61 if(dianzaixianduanshang(p,a[i],a[(i+1)%n]))return 1;
62 int k=dcmp(cro(a[(i+1)%n]-a[i],p-a[i]));
63 int d1=dcmp(a[i].y-p.y);
64 int d2=dcmp(a[(i+1)%n].y-p.y);
65 if(k>0&&d1<=0&&d2>0)wn++;
66 if(k<0&&d1>0&&d2<=0)wn--;
67 }
68 return !wn?0:1;
69 }
70 bool xianduanxiangjiao(vec a1,vec a2,vec b1,vec b2){
71 int v1=dcmp(cro(a2-a1,b1-a1)),v2=dcmp(cro(a2-a1,b2-a1));
72 int v3=dcmp(cro(b2-b1,a1-b1)),v4=dcmp(cro(b2-b1,a2-b1));
73 return v1*v2<0&&v3*v4<0;
74 }
75 bool xianduanyuduobianxingxiangjiao(vec a,vec b,vec* ch,int n){
76 up(i,0,n-1){
77 if(a==ch[i] || a==ch[(i+1)%n] || b==ch[i] || b==ch[(i+1)%n] )return 1;
78 if(xianduanxiangjiao(a,b,ch[i],ch[(i+1)%n]))return 1;
79 if(dianzaixianduanshang(a,ch[i],ch[(i+1)%n])||dianzaixianduanshang(b,ch[i],ch[(i+1)%n]))return 1;
80 }
81 return 0;
82 }
83 vec a[maxn],b[maxn],c[maxn],d[maxn];
84 int main(){
85 freopen(FILE".in","r",stdin);
86 freopen(FILE".out","w",stdout);
87 int n,m,nn,mm;
88 while(scanf("%d%d",&n,&m)==2&&(n||m)){
89 bool flag=0;
90 up(i,1,n)scanf("%lf%lf",&a[i].x,&a[i].y);
91 up(i,1,m)scanf("%lf%lf",&b[i].x,&b[i].y);
92 nn=qiutubao(a,c,n);mm=qiutubao(b,d,m);
93 up(i,1,nn)if(dianzaiduobianxingneibu(c[i],d+1,mm)){printf("No\n");flag=1;break;}if(flag)continue;
94 up(i,1,mm)if(dianzaiduobianxingneibu(d[i],c+1,nn)){printf("No\n");flag=1;break;}if(flag)continue;
95 up(i,1,nn-1)if(xianduanyuduobianxingxiangjiao(c[i],c[i+1],d+1,mm)){printf("No\n");flag=1;break;}if(flag)continue;
96 if(xianduanyuduobianxingxiangjiao(c[nn],c[1],d+1,mm)){printf("No\n");flag=1;break;}if(flag)continue;
97 up(i,1,mm-1)if(xianduanyuduobianxingxiangjiao(d[i],d[i+1],c+1,nn)){printf("No\n");flag=1;break;}if(flag)continue;
98 if(xianduanyuduobianxingxiangjiao(d[mm],d[1],c+1,nn)){printf("No\n");flag=1;break;}if(flag)continue;
99 printf("Yes\n");
100 }
101 return 0;
102 }
旋转卡(qia)壳第一题,模板题。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<ctime>
6 #include<cmath>
7 #include<algorithm>
8 #include<iomanip>
9 #include<queue>
10 #include<set>
11 using namespace std;
12 #define ll int
13 #define db double
14 #define FILE "dealing"
15 #define mid ((l+r)>>1)
16 #define pii pair<ll,ll>
17 #define up(i,j,n) for(ll i=(j);i<=(n);i++)
18 int read(){
19 ll x=0,f=1,ch=getchar();
20 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
21 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
22 return f*x;
23 }
24 const db eps=1e-10;
25 const int maxn=402000;
26 struct vec{
27 db x,y;
28 vec(db x=0,db y=0):x(x),y(y){}
29 };
30 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
31 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
32 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
33 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
34 int dcmp(db a){if(fabs(a)<eps)return 0;return a<0?-1:1;}
35 bool operator<(vec a,vec b){return a.x<b.x||(!dcmp(a.x-b.x)&&a.y<b.y);}
36 bool operator==(vec a,vec b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
37 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
38 db len(vec a){return sqrt(dot(a,a));}
39 db cro(vec a, vec b){return a.x*b.y-a.y*b.x;}
40 db are(vec a,vec b,vec c){return fabs(cro(a-b,c-b));}
41 vec a[maxn],b[maxn],c[maxn],d[maxn];
42 int qiutubao(vec* a,vec* ch,int n){
43 int m=0,k;sort(a+1,a+n+1);
44 up(i,1,n){
45 while(m>1&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<0)m--;
46 ch[++m]=a[i];
47 }
48 k=m;
49 for(int i=n-1;i>=1;i--){
50 while(m>k&&cro(ch[m]-ch[m-1],a[i]-ch[m-1])<0)m--;
51 ch[++m]=a[i];
52 }
53 if(n>1)m--;
54 return m;
55 }
56 db qiuxuanzhuanqiake(vec* a,int n){
57 int m=0,p=0;
58 if(n<=3){
59 db ans=0;up(i,0,n-1)ans=max(ans,len(a[i]-a[(i+1)%n]));
60 return ans;
61 }
62 up(i,2,n-1)if(are(a[0],a[1],a[i])>are(a[0],a[1],a[(i+1)%n])){p=i;break;}
63 db ans=max(len(a[0]-a[p]),len(a[1]-a[p]));
64 up(i,1,n-1){
65 while(are(a[i],a[(i+1)%n],a[p%n])<are(a[i],a[(i+1)%n],a[(p+1)%n]))p++;
66 ans=max(ans,max(len(a[i]-a[p%n]),len(a[p%n]-a[(i+1)%n])));
67 }
68 return ans;
69 }
70 int main(){
71 freopen(FILE".in","r",stdin);
72 freopen(FILE".out","w",stdout);
73 int T=read();
74 while(T--){
75 int n=read(),m=0;db x,y,w;
76 up(i,1,n){
77 scanf("%lf%lf%lf",&x,&y,&w);vec o(x,y);
78 a[++m]=o;
79 a[++m]=o+vec(w,w);
80 a[++m]=o+vec(w,0);
81 a[++m]=o+vec(0,w);
82 }
83 sort(a+1,a+m+1);
84 m=unique(a+1,a+m+1)-a-1;
85 int mm=qiutubao(a,b,m);
86 db ans=qiuxuanzhuanqiake(b+1,mm);ans=ans*ans+0.2;
87 printf("%.0lf\n",ans);
88 }
89 return 0;
90 }
Most Distant Point from the Sea
半平面交简单题。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<string>
5 #include<cstring>
6 #include<cmath>
7 #include<ctime>
8 #include<algorithm>
9 #include<iomanip>
10 #include<set>
11 #include<map>
12 #include<queue>
13 using namespace std;
14 #define LL long long
15 #define up(i,j,n) for(int i=(j);i<=(n);i++)
16 #define FILE "dealing"
17 #define poi vec
18 #define eps 1e-10
19 #define db double
20 const int maxn=101000,inf=1000000000,mod=1000000007;
21 struct vec {
22 db x,y;
23 vec (db x=0,db y=0):x(x),y(y){}
24 };
25 struct line{
26 db ang;vec p,v;
27 line(){}
28 line(vec p,vec v):p(p),v(v){ang=atan2(v.y,v.x);}
29 bool operator<(const line& b)const{return ang<b.ang;}
30 };
31 vec operator+(vec a,vec b){return vec(a.x+b.x,a.y+b.y);}
32 vec operator-(vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
33 vec operator*(vec a,db b){return vec(a.x*b,a.y*b);}
34 vec operator/(vec a,db b){return vec(a.x/b,a.y/b);}
35 int dcmp(db a){if(fabs(a)<eps)return 0;return a<0?-1:1;}
36 bool operator==(const vec& a,const vec& b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
37 db cro(vec a,vec b){return a.x*b.y-a.y*b.x;}
38 db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
39 db len(vec a){return sqrt(dot(a,a));}
40 bool onleft(line l,vec p){return cro(l.v,p-l.p)>0;}
41 vec qiuliangzhixianjiaodian(line a,line b){
42 vec u=a.p-b.p;
43 return a.p+a.v*(cro(b.v,u)/cro(a.v,b.v));
44 }
45 int qiubanpingmianjiao(line* l,int n,vec* ch){
46 sort(l,l+n);
47 vec* p=new vec[n];
48 line* q=new line[n];
49 int first,last;
50 q[first=last=0]=l[0];
51 up(i,1,n-1){
52 while(first<last&&!onleft(l[i],p[last-1]))last--;
53 while(first<last&&!onleft(l[i],p[first]))first++;
54 q[++last]=l[i];
55 if(fabs(cro(q[last].v,q[last-1].v))<eps){
56 last--;
57 if(onleft(q[last],l[i].p))q[last]=l[i];
58 }
59 if(first<last)p[last-1]=qiuliangzhixianjiaodian(q[last],q[last-1]);
60 }
61 while(first<last&&!onleft(q[first],p[last-1]))last--;
62 if(last-first<=1)return 0;
63 p[last]=qiuliangzhixianjiaodian(q[last],q[first]);
64 int m=0;
65 up(i,first,last)ch[m++]=p[i];
66 return m;
67 }
68 vec normal(vec a){return vec(-a.y/len(a),a.x/len(a));}
69 int n;
70 vec a[maxn],b[maxn],v[maxn],v2[maxn],c[maxn];
71 line l[maxn];
72 int main(){
73 freopen(FILE".in","r",stdin);
74 freopen(FILE".out","w",stdout);
75 while(scanf("%d",&n)&&n){
76 up(i,0,n-1)scanf("%lf%lf",&a[i].x,&a[i].y);
77 up(i,0,n-1){
78 v[i]=a[(i+1)%n]-a[i];
79 v2[i]=normal(v[i]);
80 }
81 db left=0,right=20000;
82 while(right-left>1e-8){
83 db mid=(left+right)/2;
84 up(i,0,n-1)l[i]=line(v2[i]*mid+a[i],v[i]);
85 int m=qiubanpingmianjiao(l,n,c);
86 if(!m)right=mid;
87 else left=mid;
88 }
89 printf("%.6lf\n",left);
90 }
91 return 0;
92 }