【题解】Railway [Uva10263]
【题解】Railway [Uva10263]
传送门:\(\text{Railway [Uva10263]}\)
【题目描述】
给出点 \(M\) 以及一个由 \(n\) 条线段依次相连的类曲形图(由 \(n+1\) 个点构成),求该“曲形”图中距离点 \(M\) 最近的点 \(P\) 的坐标(\(P\) 可以在任意一条线段上)。
【输入】
输入多组数据,读至 \(EOF\) 结束。
每组数据第一、二行为点 \(M\) 的坐标,接下来一个整数 \(n\) 以及 \(2(n+1)\) 行表示 \(n+1\) 的点的坐标。
【输出】
对于每组数据,输出两行分别表示点 \(P\) 的横、纵坐标。
【样例】
样例输入:
6
-3
3
0
1
5
5
9
-5
15
3
0
0
1
1
0
2
0
样例输出:
7.8966
-2.2414
1.0000
0.0000
【分析】
计算几何基础题。
在输入时记录上一个点的坐标,分别求点 \(M\) 与 \(n\) 条线段的最短距离即可。
\((1).\) 求点 \(P\) 到线段 \(AB\) 最短距离:
先判断垂足 \(F\) 的位置,如果在 \(AB\) 的延长线上,则最短距离为 \(PB\),如果在 \(BA\) 的延长线上,则最短距离为 \(PA\),否则为 \(PF\)。
\((2).\) 求点 \(P\) 到线段 \(AB\) 的垂足 \(F\):
分别计算 \(AP,BP\) 在 \(AB,BA\) 上的投影并作比,得到 \(AF\) 与 \(AB\) 的比,而 \(AB\) 已知,则可得点 \(AF\),最后再算出 \(F\)。
【Code】
#include<algorithm>
#include<cstdio>
#include<cmath>
#define LD double
#define LL long long
#define Vector Point
#define Re register int
using namespace std;
const int N=1e5+3;
const LD eps=1e-8;
int n;
inline int dcmp(LD a){return a<-eps?-1:(a>eps?1:0);}
inline LD Abs(LD a){return a*dcmp(a);}
struct Point{
LD x,y;Point(LD X=0,LD Y=0){x=X,y=Y;}
inline void in(){scanf("%lf%lf",&x,&y);}
}M,P1,P2;
inline LD Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
inline LD Cro(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
inline LD Len(Vector a){return sqrt(Dot(a,a));}
inline LD Angle(Vector a,Vector b){return acos(Dot(a,b)/Len(a)/Len(b));}
inline Point operator+(Point a,Vector b){return Point(a.x+b.x,a.y+b.y);}
inline Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
inline Vector operator*(Vector a,LD x){return Vector(a.x*x,a.y*x);}
inline bool operator==(Point a,Point b){return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}
struct ANS{Point a;LD dis;ANS(Point A,LD D=0){a=A,dis=D;}};
inline Point FootPoint(Point p,Point a,Point b){//点P到直线AB的垂足
Vector x=p-a,y=p-b,z=b-a;
LD len1=Dot(x,z)/Len(z),len2=-1.0*Dot(y,z)/Len(z);//分别计算AP,BP在AB,BA上的投影
return a+z*(len1/(len1+len2));//点A加上向量AF
}
inline Point dis_PL(Point p,Point a,Point b){//点P到线段AB距离
if(a==b)return a;//AB重合
Vector x=p-a,y=p-b,z=b-a;
if(dcmp(Dot(x,z))<0)return a;//P距离A更近
if(dcmp(Dot(y,z))>0)return b;//P距离B更近
return FootPoint(p,a,b);//返回垂足
}
int main(){
// freopen("123.txt","r",stdin);
while(~scanf("%lf%lf",&M.x,&M.y)){
scanf("%d",&n),P1.in();
Point ans=P1;
while(n--){
P2.in();Point tmp=dis_PL(M,P1,P2);
if(dcmp(Len(M-tmp)-Len(M-ans))<0)ans=tmp;
P1=P2;
}
printf("%.4lf\n%.4lf\n",ans.x,ans.y);
}
}
【题解】Railway [Uva10263]的更多相关文章
- HDU3394 Railway 题解(边双连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 题目大意: 给定一个无向图,如果从一个点出发经过一些点和边能回到该点本身,那么一路走过来的这些点 ...
- CF605A Sorting Railway Cars 题解
To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...
- 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划
C. Sorting Railway Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/conte ...
- [Codeforces 606C]Sorting Railway Cars
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
- POI2010题解
POI2010题解 我也不知道我为什么就开始刷POI了 有些题目咕掉了所以不完整(我都不知道POI到底有多少题) [BZOJ2079][Poi2010]Guilds (貌似bz跟洛谷上的不是一个题?) ...
- Tarjan & LCA 套题题目题解
刷题之前来几套LCA的末班 对于题目 HDU 2586 How far away 2份在线模板第一份倍增,倍增还是比较好理解的 #include <map> #include <se ...
- 虚拟化构建二分图(BZOJ2080 题解+浅谈几道双栈排序思想的题)
虚拟化构建二分图 ------BZOJ2080 题解+浅谈几道双栈排序思想的题 本题的题解在最下面↓↓↓ 不得不说,第一次接触类似于双栈排序的这种题,是在BZOJ的五月月赛上. [BZOJ4881][ ...
随机推荐
- Socket 结构体
proto socket 关联结构: { .type = SOCK_STREAM, .protocol = IPPROTO_TCP, .prot = &tcp_prot, .ops = &am ...
- linux nf_conntrack 连接跟踪机制 3-hook
conntrack hook函数分析 enum nf_ip_hook_priorities { NF_IP_PRI_FIRST = INT_MIN, NF_IP_PRI_CONNTRACK_DEFRA ...
- 位图bitmap应用
所有比特的编号方法是,从低字节的低位比特位开始,第一个bit为0,最后一个bit为 n-1. 比如说,现在有个数组是这样子的,int a[4],那么a[0]的比特位为0--31a[1]的比特位为32- ...
- 理解 Linux 的硬链接与软链接(转)
Linux 的文件与目录 现代操作系统为解决信息能独立于进程之外被长期存储引入了文件,文件作为进程创建信息的逻辑单元可被多个进程并发使用.在 UNIX 系统中,操作系统为磁盘上的文本与图像.鼠标与键盘 ...
- 面试题:了解MySQL的Flush-List吗?顺便说一下脏页的落盘机制!(文末送书)
Hi,大家好!我是白日梦! 今天我要跟你分享的MySQL话题是:"了解Flush-List吗?顺便说一下脏页的落盘机制!(文末送书)" 本文是MySQL专题的第 8 篇,共110篇 ...
- 【linux】gpio子系统
目录 前言 linux子系统 gpio子系统 gpio子系统实战-系统调用 前言 目前不涉及驱动源码 参考链接 linux子系统 在 Linux 系统中 绝大多数硬件设备都有非常成熟的驱动框架 驱动工 ...
- samba配置用户访问方法
配置目的: 为了给指定用户一个独立访问目录 首先在samba服务器安装samba软件 $ apt-get install samba 然后配置独立访问用户 配置samba用户前提需要是linux的用户 ...
- MySQL_where和having的区别
1. where和having都可以使用的场景 select goods_price,goods_name from sw_goods where goods_price > 100 selec ...
- no appropriate service handler found,修改数据库的最大连接数,默认150
no appropriate service handler found,频繁进行数据操作的时候,会出现这种错误.例如,当我读取excel时,一次读取好多数据,这个时候需要修改数据库的最大连接数 se ...
- 卷积神经网络图像纹理合成 Texture Synthesis Using Convolutional Neural Networks
代码实现 概述 这是关于Texture Synthesis Using Convolutional Neural Networks论文的tensorflow2.0代码实现,使用keras预训练的VGG ...