WA on test 34
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
const ld eps=1e-12;
const int N=200005;
int sgn(ld x){return (x>eps)-(x<-eps);}
struct Point
{
ld x,y;
}a[N];
ld Dis(Point A,Point B){return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));}
ld Slope(Point A,Point B){return (A.y-B.y)/(A.x-B.x);}
Point Intersection(Point A,Point B,Point C,Point D)
{
ld a1=A.y-B.y,b1=B.x-A.x,c1=A.x*B.y-B.x*A.y;
ld a2=C.y-D.y,b2=D.x-C.x,c2=C.x*D.y-D.x*C.y;
return {(b1*c2-b2*c1)/(a1*b2-a2*b1),(a2*c1-a1*c2)/(a1*b2-a2*b1)};
}
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}
return x*f;
}
int main()
{
int n,h;
cin>>n>>h;
for(int i=1;i<=n;i++)
{
int u=read(),v=read();
a[i].x=u;a[i].y=v;
}
Point L={a[n].x,a[n].y+h};
int k=n-1;
ld ans=Dis(a[n-1],a[n]);
for(int i=n-2;i>=1;i--)
{
if(sgn(Slope(a[i],L)-Slope(a[k],L))==0)
{
if(sgn(Slope(a[i],L)-Slope(a[i-1],L))<0)
ans+=Dis(a[i],a[i+1]);
}
else if(sgn(Slope(a[i],L)-Slope(a[k],L))<0)
{
ans+=Dis(a[i],Intersection(a[i],a[i+1],a[k],L));
}
if(sgn(Slope(a[i],L)-Slope(a[k],L))<=0)k=i;
}
cout<<fixed<<setprecision(10)<<ans<<'\n';
return 0;
}