URAL 1348 Goat in the Garden 2(点到线段的距离)
http://acm.timus.ru/problem.aspx?space=1&num=1348
题意:
一只羊被绑在C点;
绳子长L米;
它发现AB直线上有凤梨;
但L有可能不够长;
求吃到一个凤梨的时候L要再拉长多少?
求吃到所有凤梨的时候L要再拉长多少?
如果L足够长则输出0;
分析:
本质就是求一个点到线段的最长最短距离.
点到线段的最短距离: 用刘汝佳<<训练指南>>的模板. 大体思想是判断点的投影是否在线段上,如果点的投影在线段上,那么最短距离就是点到线段的垂线段.
否则最短距离就是点到线段两个端点距离的最小者.
点到线段的最长距离: 一定是点到线段两端点最长距离的最大值.
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
struct Point
{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
bool operator==(Point A,Point B)
{
return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
double Length(Vector A)
{
return sqrt(Dot(A,A));
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double DistanceToSegment(Point P,Point A,Point B)//P点到线段AB最短距离
{
if(A==B) return Length(P-A);
Vector v1=P-A,v2=P-B,v3=B-A;
if(Dot(v1,v3)<0) return Length(v1);
else if(Dot(v2,v3)>0) return Length(v2);
else return fabs(Cross(P-A,P-B))/Length(A-B);
}
int main()
{
Point A,B,C;
double L;
scanf("%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&L);
double max_len,min_len;
min_len = DistanceToSegment(C,A,B);
max_len = max(Length(C-A), Length(C-B));
printf("%.2lf\n%.2lf\n",min_len>L?min_len-L:0, max_len>L?max_len-L:0);
return 0;
}
分享到:
相关推荐
Ural解题思路 Ural解题思路 Ural解题思路 Ural解题思路
Ural
URAL3D
Pascal acm_timus_ural_1148.pas
大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同->注意这组数据 3 1 10 1 3 6 10 the ans is 3. hdu 1754 求区间最大值 hdu 1166 求区间和 hdu 1698 成段更新 ...
URAL(Timus Online Judge)部分测试数据 不全
Ural 1238 源代码 涉及算法(动态规划、贪心、DFS)
28. The Theoretical Background of the Ural-Altale Hypothesis The hypothesis of common origin of the Altaic languages,and even the Ural-Altaic languages,dates from the first half of the last century....
包含了ural题库中Vol_I 到Vol_III的所有题目的解题思路
In particular, the development of cloud computing, logistics networks, big data, and quantum information has played an unprecedented role in promoting the globalization of machine learning. In modern...
Pascal acm_timus_ural_1099.pas
and the National Geographic Society[5] as 4/5 of the landmass of Eurasia – with the western portion of the latter occupied by Europe – located to the east of the Suez Canal, east of the Ural ...
ural 题解 yuhch123。 关于yuhch123: ioi2008 金牌第一名,这是当初它做ural 第一卷时的题解
部分题解 大牛出品 Vol1-3 不是很全,约有200题左右
URAL-PHA
Crystal Structure of Solid Solutions and Phase Relations in the La-Co-Fe-O System Poster Crystal Structure of Solid Solutions and Phase Relations in the La-Co-Fe-O System N. V. Proskurnina, O. S. ...
Ural ACM 1000源代码(c++),vc++6.0在XPsp2下编译通过,Timus Online Judge再线测评通过
因为大三了 不弄ACM了 所以这些就删了 删之前希望可以帮到别人
资源分类:Python库 所属语言:Python 资源全名:ural-0.28.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
基于张量类模板的线性代数C ++库,可用于表示等级0的张量,标量向量,1的向量,2的矩阵,3的等级3的张量等。它还包括BLAS和LAPACK子例程的接口。