UVA_11796_Dog_Distance_(计算几何)
描述
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896
两只狗分别沿着各自的折线匀速跑,同时出发,同时到达.求其间它们两的最大距离和最小距离之差.
11796
Dog Distance
Two dogs, Ranga and Banga, are running randomly following two
different paths. They both run for T seconds with different speeds.
Ranga runs with a constant speed of R m/s, whereas Banga runs
with a constant speed of S m/s. Both the dogs start and stop at the
same time. Let D(t) be the distance between the two dogs at time
t.
The dog distance is equal to the difference between the maximum
and the minimum distance between the two dogs in their whole jour-
ney.
Mathematically,
Dog Distance = {max(D(a)) 0 ≤ a ≤ T } − {min(D(b)) 0 ≤ b ≤ T }
Given the paths of the two dogs, your job is to find the dog distance.
Each path will be represented using N points, (P 1 P 2 P 3 . . . P N ). The dog following this path will
start from P 1 and follow the line joining with P 2 , and then it will follow the line joining P 2 -P 3 , then
P 3 -P 4 and so on until it reaches P n .
Input
Input starts with an integer I (I ≤ 1000), the number of test cases.
Each test case starts with 2 positive integers A (2 ≤ A ≤ 50), B (2 ≤ B ≤ 50). The next line
contains the coordinates of A points with the format X 1 Y 1 X 2 Y 2 . . . X A Y A , (0 ≤ X i , Y i ≤ 1000).
These points indicate the path taken by Ranga. The next line contains B points in the same format.
These points indicate the path taken by Banga. All distance units are given in meters and consecutive
points are distinct. All the given coordinates are integers.
Note that the values of T , R and S are unknown to us.
Output
For each case, output the case number first. Then output the dog distance rounded to the nearest
integer. Look at the samples for exact format.
Sample Input
2
2 2
0 0
0 1
3 2
635
117
10 0
10 1
187 241 269 308 254
663 760 413
Sample Output
Case 1: 0
Case 2: 404
分析
先考虑简单的情况:两条狗都在线段上跑.由于运动是相对的,可以以其中一条狗为参考系,那么另一条狗以v2-v1(矢量差)的速度相对于狗#1运动,那么最大最小距离就是点到线段的距离了.
再考虑复杂的情况:可以把复杂情况分解为多个上述的简单情况.用pa,pb记录两条狗当前的位置,sa,sb表示它们刚经过各自的第sa和第sb个折点,计算它们俩谁先到达下一个折点,从当前时间到它到达下一个折点的时间,在这段时间里就是简单情况,求解完之后再更新pa,pb,如果需要的话还要更新sa,sb.由于每次至少经过一个折点,所以最多进行A+B次处理.
#include <bits/stdc++.h>
using namespace std; const int maxn=;
const double INF=1e10,eps=1e-; int n,A,B,kase;
double Max,Min;
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}a[maxn],b[maxn];
typedef Point Vector;
int dcmp(double x){
if(fabs(x)<eps) return ;
return x>?:-;
}
Vector operator + (Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y); }
Vector operator - (Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
Vector operator * (Vector a,double p){ return Vector(a.x*p,a.y*p); }
Vector operator / (Vector a,double p){ return Vector(a.x/p,a.y/p); }
bool operator < (Vector a,Vector b){ return a.x<b.x||(a.x==b.x&&a.y<b.y); }
bool operator == (Vector a,Vector b){ return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==; }
double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; }
double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; }
double length(Vector a){ return sqrt(dot(a,a)); }
double distance_to_segment(Point p,Point a,Point b){//点到线段距离
if(a==b) return length(p-a);
Vector v1=b-a,v2=p-a,v3=p-b;
if(dcmp(dot(v1,v2)<)) return length(v2);
if(dcmp(dot(v1,v3)>)) return length(v3);
return fabs(cross(v1,v2)/length(v1));
}
void update(Point p,Point a,Point b){//简单情况
Min=min(Min,distance_to_segment(p,a,b));
Max=max(Max,length(p-a));
Max=max(Max,length(p-b));
}
void solve(){
scanf("%d%d",&A,&B);
double lena=,lenb=;
for(int i=;i<=A;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=;i<=B;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
for(int i=;i<A;i++) lena+=length(a[i+]-a[i]);
for(int i=;i<B;i++) lenb+=length(b[i+]-b[i]);
Min=INF,Max=-INF;
int sa=,sb=;
Point pa=a[],pb=b[];
while(sa<A){
double la=length(a[sa+]-pa);
double lb=length(b[sb+]-pb);//用总长来代表速度
double t=min(la/lena,lb/lenb);
Vector va=(a[sa+]-pa)/la*t*lena;
Vector vb=(b[sb+]-pb)/lb*t*lenb;//两个位移向量
update(pa,pb,pb+vb-va);
pa=pa+va;
pb=pb+vb;
if(pa==a[sa+]) sa++;
if(pb==b[sb+]) sb++;
}
printf("Case %d: %.0lf\n",++kase,Max-Min);
}
int main(){
scanf("%d",&n);
while(n--) solve();
return ;
}
UVA_11796_Dog_Distance_(计算几何)的更多相关文章
- ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)
POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...
- HDU 2202 计算几何
最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ACM 计算几何中的精度问题(转)
http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- [知识点]计算几何I——基础知识与多边形面积
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...
- POJ 1106 Transmitters(计算几何)
题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...
- TYVJ计算几何
今天讲了计算几何,发几道水水的tyvj上的题解... 计算几何好难啊!@Mrs.General....怎么办.... 这几道题都是在省选之前做的,所以前面的Point运算啊,dcmp啊,什么什么的,基 ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
随机推荐
- 如何恢复oracle中已删除的表
在9i中Oracle引入了flashback的概念,可以将数据返回到某个时间点,但对于诸如drop/truncate等DDL语句却尚不支持.进入Oracle10g,这一缺陷得到了弥补.可以将丢失掉的表 ...
- iOS开发——基于corelocation位置定位——工具类
(代码工具类已写好,空闲时间整理成文档,待更新……)
- let和const====均参考阮大神的es6入门
// 解构复制// let [foo,[[bar],baz]] = [1,[[2],3]];// console.log(foo);//1// console.log(bar);//2// conso ...
- JVM - 内存溢出问题排查相关命令jcmd jmap
jcmd http://docs.oracle.com/javase/8/docs/technotes/tools/windows/jcmd.html jcmd-l 列出正在执行的JAVA进程ID ...
- Linux的IO调度
Linux的IO调度 IO调度发生在Linux内核的IO调度层.这个层次是针对Linux的整体IO层次体系来说的.从read()或者write()系统调用的角度来说,Linux整体IO体系可以分为七层 ...
- Iptables網路連線限制及攻擊防護和相關設定
[筆記整理]Iptables網路連線限制及攻擊防護和相關設定 1. 限制每個IP連接HTTP最大併發50個連接數 iptables -A INPUT -p tcp --dport 80 -m conn ...
- webp介绍与使用
webp是谷歌10年发布的一种新的图片格式,支持有损压缩或无损压缩.据官方称无损压缩的webp在体积上要比png小26%,而有损压缩要比同质量jpg小25%~34%.经本人测试,由腾讯智图处理 ...
- input多选图片与显示
input标签 在使用input选择文件时遇到了 在遍历input file.files 只显示最后一个,修改如下: CSS: <style type="text/css"& ...
- HTML实体
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 用javascript操作xml(二)JavaScript 将XML转换成字符串(xml to string)
function xmlToString(xmlData) { var xmlString; //IE if (window.ActiveXObject){ xmlString = xmlData.x ...