题目
题意: 给定(x1,y1),(x0,y0).(x1,y1)在接下来的n秒,(x1,y1)会按照给出的向量(xi,yi)移动。第i秒移动(xi,yi)。在此期间,求(x1,y1)与(x0,y0)的最短距离。
思路: 要么是顶点与(x0,y0)求距离,要么是(x0,y0)到AB所在直线的距离。(要保证直线与(x0,y0)的交点在AB两点之间)。就呼呼的求个直线表达式,再算个交点坐标,再算个点到直线的距离,呼呼算。简单做麻烦了。
按std的做法,可以先判断三个点组成的三角形中,线段两端是否为钝角,如果是钝角,寄。否则交点在两点之间。根据S = 2 * AB * h,S由海伦公式 p = (a+b+c)/2,S = sqrt(p*(p-a)(p-b)(p-c))得到。
时间复杂度: O(n)
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
#define int long long
int n,m,k,T;
struct Point{
int x,y;
Point(){}
Point(int a,int b)
{
x = a,y = b;
}
};
int a[4];
double ans = 1e18;
double dist(Point a,Point b)
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x) + 1.0*(a.y-b.y)*(a.y-b.y) );
}
bool check(Point a,Point b,Point c)
{
double aa = dist(b,c);
double bb = dist(a,c);
double cc = dist(a,b);
if(aa * aa > bb*bb + cc*cc)
{
return true;
}
if(bb * bb > aa*aa + cc*cc)
{
return true;
}
return false;
}
double h(Point a,Point b,Point c)
{
double aa = dist(b,c);
double bb = dist(a,c);
double cc = dist(a,b);
double p = (aa+bb+cc)/2;
double S = sqrt(p*(p-aa)*(p-bb)*(p-cc));
return 2*S/cc;
}
void solve()
{
read(n);
for(int i=0;i<4;++i) read(a[i]);
Point st = Point(a[0],a[1]);
Point ed = Point(a[2],a[3]);
ans = min(ans,dist(st,ed));
for(int i=0;i<n;++i)
{
int tx,ty; read(tx),read(ty);
Point to = Point(st.x+tx,st.y+ty);
if(!check(st,to,ed)) //没有钝角
{
ans = min(ans,h(st,to,ed));
}
st.x += tx,st.y += ty;
ans = min(ans,dist(st,ed));
}
cout<<setprecision(8)<<ans;
}
signed main(void)
{
T = 1;
// OldTomato; cin>>T;
// read(T);
while(T--)
{
solve();
}
return 0;
}
// Problem: 雪色光晕
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/23479/D
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
#define int double
int n,m,k,T;
int x,y;
int sx,sy;
double ans = 1e18;
double px,py;
double dis(int x,int y,int x2,int y2)
{
return sqrt( (x2-x)*(x2-x) + (y2-y)*(y2-y) ) ;
}
double dist(int a,int b,int c,double x,double y)
{
double fenzi = a*x+b*y+c;
double fenmu = sqrt(a*a+b*b);
// cout<<fenzi<<" "<<fenmu<<"?"<<endl;
return fabs(fenzi / fenmu);
}
void solve()
{
cin>>n>>x>>y>>sx>>sy;
ans = min(ans,dis(x,y,sx,sy));
for(int i=0;i<n;++i)
{
int tx,ty; cin>>tx>>ty;
int x2 = x + tx,y2 = y + ty;
int x1 = x,y1 = y;
int a = y2-y1;int b = x1 - x2;
int c = x2*y1 - x1*y2;
double k = -1.0 * a / b;
int jieju = y1 - k*x1;
int ex,ey;
if(x1 == x2)
{
ex = x1;
ey = sy;
if( (x1<=ex&&ex<=x2) || (x2<=ex&&ex<=x1) )
{
if((y1<=ey && ey<=y2)|| (y2<=ey && ey<=y1))
{
ans = min(ans,fabs(sx-x1));
}
}
continue;
}
else
{
ex = k*sy + sx - k * jieju;
ex = (ex)/(k*k+1);
ey = (k*k*sy+k*sx+jieju)/(k*k+1);
}
// cout<<ex<<" "<<ey<<"?"<<endl;
if( (x1<=ex&&ex<=x2) || (x2<=ex&&ex<=x1) )
{
if((y1<=ey && ey<=y2)|| (y2<=ey && ey<=y1))
{
double tmp = dist(a,b,c,sx,sy);
ans = min(ans,tmp);
}
}
x = x2,y = y2;
ans = min(ans,dis(x,y,sx,sy));
}
// ans = sqrt(ans);
printf("%.8lf",ans);
}
signed main(void)
{
T = 1;
// OldTomato; cin>>T;
// read(T);
while(T--)
{
solve();
}
return 0;
}