洛谷传送门
BZOJ传送门
解析:
这道题BZOJ的SPJ被卡爆了啊,只需要这段程序就能AC了:
#include<bits/stdc++.h>
signed main(){
puts("nan");
return 0;
}
是的,not a number。
其实这道题稍微画一个图就会发现,答案就是所有合法的连线的半平面交的周长,为了方便,我们算半平面交的时候可以加上 n n n到 1 1 1的连线,算周长的时候减去就行了。
直接添加是 O ( n 2 ) O(n^2) O(n2)的,但是由于这是凸多边形,显然只有每个点到它能够到达的最大点连线才有贡献,所以连线是 O ( n ) O(n) O(n)的。
由于习惯用左半平面做半平面交,所以我将原图左右翻转了一下。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc get_char
#define cs const
namespace IO{
inline char get_char(){
static cs int Rlen=1<<20|1;
static char buf[Rlen],*p1,*p2;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
}
inline int getint(){
re char c;
re bool f=0;
while(!isdigit(c=gc()))if(c=='-')f=1;re int num=c^48;
while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
return f?-num:num;
}
}
using namespace IO;
cs int N=100005;
cs double eps=1e-9;
struct Point{
double x,y;
Point(cs double &_x=0,cs double &_y=0):x(_x),y(_y){}
friend Point operator+(cs Point &a,cs Point &b){return Point(a.x+b.x,a.y+b.y);}
friend Point operator-(cs Point &a,cs Point &b){return Point(a.x-b.x,a.y-b.y);}
friend Point operator*(cs Point &a,cs double &b){return Point(a.x*b,a.y*b);}
friend double operator*(cs Point &a,cs Point &b){return a.x*b.y-a.y*b.x;}
friend double dot(cs Point &a,cs Point &b){return a.x*b.x+a.y*b.y;}
double norm()cs{return sqrt(dot(*this,*this));}
friend double dist(cs Point &a,cs Point &b){return (a-b).norm();}
}p[N];
inline int sign(double x){
return fabs(x)<eps?0:(x<0?-1:1);
}
struct Line{
Point s,e;
double rad;
Line(){}
Line(cs Point &_s,cs Point &_e):s(_s),e(_e),rad(atan2(_e.y-_s.y,_e.x-_s.x)){}
friend bool operator<(cs Line &a,cs Line &b){
return sign(a.rad-b.rad)?sign(a.rad-b.rad)<0:sign((a.e-a.s)*(b.e-a.s))>0;
}
}l[N],q[N];
inline Point inter_section(cs Line &a,cs Line &b){
double s1=(a.e-a.s)*(b.s-a.s),s2=(b.e-a.s)*(a.e-a.s);
double k=s1/(s1+s2);
return b.s+(b.e-b.s)*k;
}
inline bool judge(cs Line &a,cs Line &b,cs Line &c){
return sign((c.e-c.s)*(inter_section(a,b)-c.s))<0;
}
int n,m,tot,head,tail;
inline void half_plane_inter(){
sort(l+1,l+m+1);
n=0;
for(int re i=1;i<=m;++i){
if(sign(l[i].rad-l[i-1].rad))++n;
l[n]=l[i];
}
m=n;
q[head=1]=l[1];
q[tail=2]=l[2];
for(int re i=3;i<=m;++i){
while(head<tail&&judge(q[tail-1],q[tail],l[i]))--tail;
while(head<tail&&judge(q[head+1],q[head],l[i]))++head;
q[++tail]=l[i];
}
while(head<tail&&judge(q[tail-1],q[tail],q[head]))--tail;
while(head<tail&&judge(q[head+1],q[head],q[tail]))++head;
n=0;q[tail+1]=q[head];
for(int re i=head;i<=tail;++i)p[++n]=inter_section(q[i],q[i+1]);
}
pair<int,int> ban[N*10];
double ans;
signed main(){
n=getint();
tot=getint();
for(int re i=1;i<=n;++i){
p[i].x=getint();
p[i].y=getint();
}
for(int re i=1;i<=tot;++i){
ban[i]=make_pair(getint(),getint());
if(ban[i].first>ban[i].second)swap(ban[i].first,ban[i].second);
}
sort(ban+1,ban+tot+1);
for(int re i=1,j=1;i<n;++i){
while(i==ban[j+1].first)++j;
if(i==ban[j].first){
int k=j,to=n;
while(ban[k].second==to&&ban[k].first==i)k--,to--;
if(to>i)l[++m]=Line(p[to],p[i]);
if(i==1&&to==n){
printf("%.10lf",dist(p[1],p[n]));
return 0;
}
}
else {
l[++m]=Line(p[n],p[i]);
if(i==1){
printf("%.10lf",dist(p[1],p[n]));
return 0;
}
}
}
l[++m]=Line(p[1],p[n]);
ans-=dist(p[n],p[1]);
half_plane_inter();
for(int re i=1;i<=n;++i)ans+=dist(p[i],p[i%n+1]);
printf("%.10lf",ans);
return 0;
}