O ( n 2 ) : O(n^2): O(n2):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define db double
const db eps=1e-8;
struct vec{
db x,y;
vec(){}
vec(db a,db b)
{x=a,y=b;}
};
vec operator+(vec a,vec b)
{return vec(a.x+b.x,a.y+b.y);}
vec operator-(vec a,vec b)
{return vec(a.x-b.x,a.y-b.y);}
vec operator*(vec a,db b)
{return vec(a.x*b,a.y*b);}
db operator*(vec a,vec b)
{return a.x*b.x+a.y*b.y;}
db operator^(vec a,vec b)
{return a.x*b.y-a.y*b.x;}
struct lin{
vec p,v;
lin(){}
lin(vec a,vec b)
{p=a,v=b;}
};
vec X(lin a,lin b)
{
db k=((a.p-b.p)^a.v)/(b.v^a.v);
return b.p+b.v*k;
}
int N=4;
const int Q=11111;
vec ans[Q],t[Q];
#define Ch(a,b,c) ((a-b)^c)
void Ins(lin a)
{
for(int i=1;i<=N;i++)
t[i]=ans[i];
int tp=N;
t[N+1]=t[1],t[0]=t[N];
N=0;
for(int i=1;i<=tp;i++)
if(Ch(t[i],a.p,a.v)>eps){
if(Ch(t[i-1],a.p,a.v)<-
eps)ans[++N]=X(a,lin(t[i-1],t[i]-t[i-1]));
if(Ch(t[i+1],a.p,a.v)<-eps)ans[++N]=X(a,lin(t[i+1],t[i]-t[i+1]));
}
else ans[++N]=t[i];
}
int main()
{
int n;
ans[1]=vec(1e10,1e10);
ans[2]=vec(-1e10,1e10);
ans[3]=vec(-1e10,-1e10);
ans[4]=vec(1e10,-1e10);
scanf("%d",&n);
vec p,q;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&p.x,&p.y,&q.x,&q.y);
Ins(lin(p,q-p));
}
printf("%d\n",N);
for(int i=1;i<=N;i++)
printf("%.2lf %.2lf\n",ans[i].x,ans[i].y);
return 0;
}
/*
6
-2 0
-1 -2
-1 -2
1 -2
1 -2
2 0
2 0
1 2
1 2
-1 2
-1 2
-2 0
4
0 -3
1 -1
1 -1
2 2
2 2
-1 0
-1 0
0 -3
4
0 0
1 0
1 0
0 1
0 1
0 0
1 0
1 1
*/
O ( n l o g 2 n ) : O(nlog_2^n): O(nlog2n):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define db double
const db eps=1e-8;
struct vec{
db x,y;
vec(){}
vec(db a,db b)
{x=a,y=b;}
};
vec operator+(vec a,vec b)
{return vec(a.x+b.x,a.y+b.y);}
vec operator-(vec a,vec b)
{return vec(a.x-b.x,a.y-b.y);}
vec operator*(vec a,db b)
{return vec(a.x*b,a.y*b);}
db operator*(vec a,vec b)
{return a.x*b.x+a.y*b.y;}
db operator^(vec a,vec b)
{return a.x*b.y-a.y*b.x;}
struct lin{
vec p,v;
db ang;
lin(){}
lin(vec a,vec b)
{p=a,v=b;}
};
vec X(lin a,lin b)
{
db k=((a.p-b.p)^a.v)/(b.v^a.v);
return b.p+b.v*k;
}
const int Q=211111;
int hd=1,tl=1;
lin q[Q],a[Q];
bool cmp(lin a,lin b)
{
if(a.ang==b.ang)return ((a.p-b.p)^b.v)<eps;
return a.ang<b.ang;
}
bool GG(lin a,vec b)
{
return ((b-a.p)^a.v)>eps;
}
int main()
{
int n;
scanf("%d",&n);
vec pp,qq;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&pp.x,&pp.y,&qq.x,&qq.y);
a[i]=lin(pp,qq-pp);a[i].ang=atan2(a[i].v.y,a[i].v.x);
}
hd=tl=1;
sort(a+1,a+n+1,cmp);
int tp=n;
n=0;
for(int i=1;i<=tp;i++)
if(i==1||a[i].ang>a[n].ang)a[++n]=a[i];
q[1]=a[1];
for(int i=2;i<=n;i++)
{
while(hd<tl&&GG(a[i],X(q[hd],q[hd+1])))++hd;
while(hd<tl&&GG(a[i],X(q[tl],q[tl-1])))--tl;
q[++tl]=a[i];
}
while(hd<tl&&GG(q[hd],X(q[tl],q[tl-1])))--tl;
q[tl+1]=q[hd];
printf("%d\n",tl-hd+1);
for(int i=hd;i<=tl;i++){
vec now=X(q[i],q[i+1]);
printf("%.2lf %.2lf\n",now.x,now.y);
}
return 0;
}