Dog Distance

【题目链接】Dog Distance

【题目类型】几何

&题解:

蓝书的题,刘汝佳的代码,学习一下

&代码:

// UVa11796 Dog Distance
// Rujia Liu
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; const double eps = 1e-8;
int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } const double PI = acos(-1.0);
double torad(double deg) { return deg/180 * PI; } struct Point {
double x, y;
Point(double x=0, double y=0):x(x),y(y) { }
}; typedef Point Vector; 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 a.x < b.x || (a.x == b.x && a.y < b.y);
} bool operator == (const Point& a, const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
} Point read_point() {
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
}; double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
double Length(const Vector& A) { return sqrt(Dot(A, A)); }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; } 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);
} const int maxn = 60;
int T, A, B;
Point P[maxn], Q[maxn];
double Min, Max; void update(Point P, Point A, Point B) {
Min = min(Min, DistanceToSegment(P, A, B));
Max = max(Max, Length(P-A));
Max = max(Max, Length(P-B));
} int main() {
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
scanf("%d%d", &A, &B);
for(int i = 0; i < A; i++) P[i] = read_point();
for(int i = 0; i < B; i++) Q[i] = read_point();
double LenA = 0, LenB = 0;
for(int i = 0; i < A-1; i++) LenA += Length(P[i+1]-P[i]);
for(int i = 0; i < B-1; i++) LenB += Length(Q[i+1]-Q[i]);
int Sa = 0, Sb = 0;
Point Pa = P[0], Pb = Q[0];
Min = 1e9, Max = -1e9;
while(Sa < A-1 && Sb < B-1) {
double La = Length(P[Sa+1] - Pa); // 甲到下一拐点的距离
double Lb = Length(Q[Sb+1] - Pb); // 乙到下一拐点的距离
double T = min(La/LenA, Lb/LenB); // 取合适的单位,可以让甲和乙的速度分别是LenA和LenB
Vector Va = (P[Sa+1] - Pa)/La*T*LenA; // 甲的位移向量
Vector Vb = (Q[Sb+1] - Pb)/Lb*T*LenB; // 乙的位移向量
update(Pa, Pb, Pb+Vb-Va); // 求解“简化版”,更新最小最大距离
Pa = Pa + Va;
Pb = Pb + Vb;
if(Pa == P[Sa+1]) Sa++;
if(Pb == Q[Sb+1]) Sb++;
}
printf("Case %d: %.0lf\n", kase, Max-Min);
}
return 0;
}

UVA 11796 Dog Distance(几何)的更多相关文章

  1. 简单几何(相对运动距离最值) UVA 11796 Dog Distance

    题目传送门 题意:两只狗在折线上跑,速度未知,同时出发,同时达到.问跑的过程中,两狗的最大距离和最小距离的差 分析:训练指南P261,考虑相对运动,设A静止不动,B相对A运动,相对的运动向量:Vb - ...

  2. ●UVA 11796 Dog Distance

    题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 11796 - Dog Distance 向量的应用

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA 11796 - Dog Distance

    题意  两条狗啊,同时跑,,同时结束,各自跑各自的道路,问跑的过程中,他们最大距离和最小距离的差: 方法  恶心一点就是,最大最小距离的求解方法,假设两只狗都只有一条线段要跑,则可以判定在端点处有最大 ...

  5. UVA 11796 Dog Distance(向量)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...

  6. UVa 11796 计算几何

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  8. UVa 11971 - Polygon(几何概型 + 问题转换)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVa 11346 - Probability(几何概型)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. shell脚本的基本结构以及如何执行

    1.shell脚本通常以.sh为后缀,不是说不带这个.sh脚本就不会执行,而是大家一种通用的命名规范而已 2.shell脚本中的第一行一定是:#! /bin/bash.该命令说明,该文件使用的是bas ...

  2. linux命令瞎记录find xargs

    1.创建多个文件 touch test{0..100}.txt 2.重定向 “>>” 追加重定向,追加内容,到文件的尾部 “>” 重定向,清除原文件里面所有内容,然后把内容追加到文件 ...

  3. ssh agent-forward

    出于安全性考虑,服务器迁移后,将统一使用 SSH agent forwarding 方式登录所有服务器, 原则上所有 ssh 操作都要通过跳板机,而且跳板机上禁止存储一切私钥. 在此说明一下后续ssh ...

  4. 【数据库】Invalid default value for 'create_date' timestamp field

    问题 最近遇到一个这样的问题,新建数据库表的时候 提示 错误如下 Invalid default value for 'created_time' timestamp field 语句如下 `crea ...

  5. Appium入门(3)__ Appium Server安装

    安装Appium 1.下载并安装:https://bitbucket.org/appium/appium.app/downloads/ 2. 系统变量PATH 增加 C:\Program Files ...

  6. idea导出可执行jar包

    一. 在菜单中选择 File->project structure. 二. 在弹出的窗口中左侧选中"Artifacts",点击"+"选择jar,然后选择& ...

  7. 如何在Win10上永久禁用Windows Defender Antivirus

    1.使用Windows键+ R键盘快捷键打开运行命令. 2.键入regedit,然后单击确定以打开注册表. 3.浏览以下路径: HKEY_LOCAL_MACHINE/SOFTWARE/Policies ...

  8. 那些年读过的书《Java并发编程实战》一、构建线程安全类和并发应用程序的基础

    1.线程安全的本质和线程安全的定义 (1)线程安全的本质 并发环境中,当多个线程同时操作对象状态时,如果没有统一的状态访问同步或者协同机制,不同的线程调度方式和不同的线程执行次序就会产生不同的不正确的 ...

  9. min-max容斥笔记及例题

    这个东西是一个非常好玩的数学工具. $$max(S)=\sum_{T\subset S}(-1)^{|T|-1}min(T)$$ $$max_k(S)=\sum_{T\subset S}(-1)^{| ...

  10. 【三分】light bulb(zoj3203)

    题目描述: 如图,你可以在房间里移动,灯泡的高度为H,你的身高为h,灯泡与墙的水平距离为D,求你影子的最长长度(影子长度=地上影子长度+墙上影子长度) 样例输入: 0.5 样例输出: 1.000 0. ...