UVA 11796 Dog Distance(几何)
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(几何)的更多相关文章
- 简单几何(相对运动距离最值) UVA 11796 Dog Distance
		
题目传送门 题意:两只狗在折线上跑,速度未知,同时出发,同时达到.问跑的过程中,两狗的最大距离和最小距离的差 分析:训练指南P261,考虑相对运动,设A静止不动,B相对A运动,相对的运动向量:Vb - ...
 - ●UVA 11796 Dog Distance
		
题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 - UVA 11796 - Dog Distance 向量的应用
		
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
 - UVA   11796 - Dog Distance
		
题意 两条狗啊,同时跑,,同时结束,各自跑各自的道路,问跑的过程中,他们最大距离和最小距离的差: 方法 恶心一点就是,最大最小距离的求解方法,假设两只狗都只有一条线段要跑,则可以判定在端点处有最大 ...
 - UVA 11796 Dog Distance(向量)
		
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...
 - 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 11971 - Polygon(几何概型 + 问题转换)
		
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 - UVa 11346 - Probability(几何概型)
		
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 
随机推荐
- SQLServer 索引重建
			
SQL Server 索引重建脚本 在数据的使用过程中,由于索引page碎片过多,带来一些不利的性能问题,我们有时候需要对数据库中的索引进行重组或者重建工作.通常这个阈值为30%,大于30%我们建议进 ...
 - 用mysql-connector操作MySQL数据库
			
首先是工具库的安装 pip install mysql-connector 连接数据库 #连接数据库 #常规连接方式 conn = mysql.connector.connect(user=', da ...
 - Will vs Be Going To vs Present Continuous: Talk About the Future in English
			
https://www.youtube.com/watch?v=UISiuiPd_FY will 说话的当下决定的将来要做什么,in the moment be going to 有意图去做,但没有计 ...
 - ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]
			
买了新笔记本,装软件. 在ARCSDE直连Oracle时遇到问题. esri官网给的解释是因为安装arcgis时安装目录里有特殊字符(详见:https://support.esri.com/en/te ...
 - nfs的时间问题,影响编译
			
[root@okk dpdk]# rm -rf x86_64-native-linuxapp-gcc/ [root@okk dpdk]# A=`date +%s` ; B=`expr $A + `; ...
 - 【Mysql数据库访问利器】phpMyadmin
			
缘由 我们程序员难免要和数据库打交道,经过这几年的锻炼,感觉手写SQL语句已经忘记的差不错了,促使我一定要这篇文章的原因是,有一次晚上我更新某个系统的数据库的表(由于目前公司比较严格,数据库都只能通过 ...
 - 初识jmeter(2)
			
1.层级关系: 聚合报告1记录HTTP请求1的结果: 聚合报告2记录HTTP请求2的结果: 聚合报告记录所有线程组中HTTP请求的结果. 2.线程同时启动(并发) 一是可以在把线程组里面的 Ramp- ...
 - shiro默认过滤器
 - 内置函数time
			
time import time.time() # 浮点型,给计算机看,随机 时间有三种: First: 时间戳 (time.time()) Second: 结构化时间 可以修改 Third: ...
 - 小程序支持打开APP了 还有小程序的标题栏也可以自定义
			
就在刚刚,小程序上线两个新能力——小程序支持打开APP了,小程序的标题栏区域开放自定义.用户可以在小程序里更方便地获取到APP的服务了——APP链接分享到微信,打开小程序页面后,用户从该小程序页面里, ...