UVA 11796 - Dog Distance 向量的应用
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896
题目大意:
甲和乙两条狗分别沿着一条折线跑,它们速度未知,但同时出发并且同时到达终点,并且都是匀速奔跑。求奔跑过程中两只狗的最大距离与最小距离之差。
思路:
因为运动是相对的,我们可以认为甲静止不动,乙沿着直线走。则问题就转化为点到线段的最小距离。
那么,我们对于每段分析,看谁先到达该线段的终点,则该段可以用上面的方法求。
把a看成不动的,则有b运动到了cb+vb-va(其中va,vb分别为a和b的位移向量,ca,cb分别为a和b初始位置)
那么就转换为sa到线段cb+vb-va的距离。(最小距离为点到直线,而最大距离一定在线段两边)
至于速度的表示,设1S到达终点,那么速度就是总长。
PS:直接用模版就是爽。哈哈
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=50+10;
const int INF=99999999;
double ans_max,ans_min;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){ }
};
typedef Point Vector;
const double eps = 1e-8;
int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (const Vector& A, double p) { return Vector(A.x/p, A.y/p); }
bool operator == (const Point& a, const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
} double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
double Length(const Vector& A) { return sqrt(Dot(A, A)); } double DistanceToSegment(const Point& P, const Point& A, const Point& B)
{
if(A == B) return Length(P-A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
} void update(Point P,Point A,Point B)
{
ans_max=max(ans_max,Length(P-A));//最大的一定在端点
ans_max=max(ans_max,Length(P-B));
ans_min=min(ans_min,DistanceToSegment(P,A,B));//最小的为点到直线的距离
} Point a[MAXN],b[MAXN]; int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
int A,B;
scanf("%d%d",&A,&B);
for(int i=0;i<A;i++)
scanf("%lf%lf",&a[i].x,&a[i].y); for(int i=0;i<B;i++)
scanf("%lf%lf",&b[i].x,&b[i].y); double lena=0,lenb=0; //总长度
for(int i=0;i<A-1;i++)
lena+=Length(a[i+1]-a[i]);
for(int i=0;i<B-1;i++)
lenb+=Length(b[i+1]-b[i]); int ia=0,ib=0; //index
Point ca=a[0],cb=b[0]; //current point
ans_max=-INF,ans_min=INF;
while(ia<A-1 && ib<B-1)
{
double sa=Length(a[ia+1]-ca); //到下一个拐点的距离
double sb=Length(b[ib+1]-cb);
double ta=sa/lena; //设到终点都为1S,那么每一段时间就是除以总长。
double tb=sb/lenb;
double t=min(ta,tb);
Vector va=(a[ia+1]-ca)/sa*t*lena;
Vector vb=(b[ib+1]-cb)/sb*t*lenb;
update(ca,cb,cb+vb-va);//把a看成静止的,则b相对从cb运动到了vb-va处。
ca=ca+va;
cb=cb+vb;
if(ca==a[ia+1]) ia++;
if(cb==b[ib+1]) ib++;
}
printf("Case %d: %.0lf\n",kase++,ans_max-ans_min);
} return 0;
}
UVA 11796 - Dog Distance 向量的应用的更多相关文章
- UVA 11796 Dog Distance(几何)
Dog Distance [题目链接]Dog Distance [题目类型]几何 &题解: 蓝书的题,刘汝佳的代码,学习一下 &代码: // UVa11796 Dog Distance ...
- UVA 11796 Dog Distance(向量)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...
- ●UVA 11796 Dog Distance
题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11796 - Dog Distance
题意 两条狗啊,同时跑,,同时结束,各自跑各自的道路,问跑的过程中,他们最大距离和最小距离的差: 方法 恶心一点就是,最大最小距离的求解方法,假设两只狗都只有一条线段要跑,则可以判定在端点处有最大 ...
- 简单几何(相对运动距离最值) UVA 11796 Dog Distance
题目传送门 题意:两只狗在折线上跑,速度未知,同时出发,同时达到.问跑的过程中,两狗的最大距离和最小距离的差 分析:训练指南P261,考虑相对运动,设A静止不动,B相对A运动,相对的运动向量:Vb - ...
- UVa 11796 计算几何
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
- UVA 11437 - Triangle Fun 向量几何
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11796
题意: 有两个狗, 按照 多边形跑,不知道两条狗的速度,但是狗是同时出发,同时到达终点的 输出两条狗的 最大相距距离 - 最小相距距离: 思路 : 用物理的相对运动来计算, 每次只计算 两条狗的直线 ...
随机推荐
- 【hdu 1429】胜利大逃亡(续)
[Link]: [Description] 给你一个n*m的格子; 里面有钥匙,以及钥匙能开的门; 以及墙,以及起点,以及出口; 问你从起点出发,到出口的话,能不能在t时间内到; [Solution] ...
- Vijos——T1406 拉力赛
https://vijos.org/p/1460 描述 车展结束后,游乐园决定举办一次盛大的山道拉力赛,平平和韵韵自然也要来参加大赛. 赛场上共有n个连通的计时点,n-1条赛道(构成了一棵树).每个计 ...
- WebCollector爬取百度搜索引擎样例
使用WebCollector来爬取百度搜索引擎依照关键字搜索的结果页面,解析规则可能会随百度搜索的改版而失效. 代码例如以下: package com.wjd.baidukey.crawler; im ...
- RHEL7.1安装VNC
1.安装包 yum install vnc* -y 2.创建password vncserver 3.创建參数文件 [root@single ~]# cp /lib/systemd/system/vn ...
- POJ 2352 Stars(线段树)
题目地址:id=2352">POJ 2352 今天的周赛被虐了. . TAT..线段树太渣了..得好好补补了(尽管是从昨天才開始学的..不能算补...) 这题还是非常easy的..维护 ...
- 从设计到实现,一步步教你实现Android-Universal-ImageLoader-辅助类
通过前面几篇博文.我们分析了 AUI 的缓存.工具类.显示与载入这几个方面的代码.今天呢,我们继续研究 AUI 的源代码,学习当中的核心辅助工具类. 希望大家能在里面学到东西哈. Download 要 ...
- Codeforces Round #262 (Div. 2) 题解
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...
- Oracle-02-数据库概述
一.数据库用途 用于存放数据的软件 当中Application server重要,将数据存在表中每一个表关系就能够反映不同表之间数据的关系,比方淘宝用户注冊.商品买卖等数据存在操作系统的目录中,不便于 ...
- solr简介
Solr 是什么? Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出格式(包 ...