[LightOJ1240]Point Segment Distance 题解
题意简述
原题LightOJ 1240,Point Segment Distance(3D)。
求三维空间里线段AB与C。
题解
我们设一个点在线段AB上移动,然后发现这个点与原来的C点的距离呈一个单峰状,于是珂以三分。
三分的时候处理一下,把A点坐标移动到(0,0,0)珂以方便操作。
代码
#include <cstdio>
#include <cmath>
namespace fast_IO{
const int IN_LEN = 10000000, OUT_LEN = 10000000;
char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
int read(){
int x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar_();
if (ch == '-') zf = -1, ch = getchar_();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x * zf;
}
void write(int x){
if (x < 0) putchar_('-'), x = -x;
if (x > 9) write(x / 10);
putchar_(x % 10 + '0');
}
}
using namespace fast_IO;
struct Pos{
double x, y, z;
} pos[3];
long double cacDis(Pos &a, Pos &b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z));
}
long double cac(long double num){
Pos _new = (Pos){pos[1].x * num, pos[1].y * num, pos[1].z * num};
Pos mid = (Pos){(_new.x + pos[0].x) / 2, (_new.y + pos[0].y) / 2, (_new.z + pos[0].z) / 2};
return cacDis(mid, pos[2]);
}
int main(){
int t = read();
for (int j = 1; j <= t; ++j){
pos[0].x = read(), pos[0].y = read(), pos[0].z = read();
pos[1].x = read(), pos[1].y = read(), pos[1].z = read( );
pos[1].x -= pos[0].x, pos[1].y -= pos[0].y, pos[1].z -= pos[0].z;
pos[2].x = read(), pos[2].y = read(), pos[2].z = read();
pos[2].x -= pos[0].x, pos[2].y -= pos[0].y, pos[2].z -= pos[0].z;
pos[0].x -= pos[0].x, pos[0].y -= pos[0].y, pos[0].z -= pos[0].z;
long double l = 0, r = 2.0, mid;
long double mmid; long double ans = 0.0;
for (int i = 1; i <= 100; ++i){
mid = (l + r) / 2;
mmid = (mid + r) / 2;
if (cac(mid) <= cac(mmid))
r = mmid, ans = mmid;
else
l = mid;
}
printf("Case %d: %.9Lf\n", j, cac(ans));
}
return 0;
}
[LightOJ1240]Point Segment Distance 题解的更多相关文章
- 三分题两道:lightoj1146 Closest Distance、lightoj1240 Point Segment Distance (3D)
lightoj1146 Two men are moving concurrently, one man is moving from A to B and other man is moving f ...
- poj2689 Prime Distance题解报告
题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...
- POJ2689:Prime Distance——题解
http://poj.org/problem?id=2689 题目大意,给不超过int的l,r,其中r-l+1<=1000000,筛出其中的素数,并且求出相邻素数差值最大和最小的一对. ———— ...
- CF1175E Minimal Segment Cover 题解
题意:给出\(n\)个形如\([l,r]\)的线段.\(m\)次询问,每次询问区间\([x,y]\),问至少选出几条线段,使得区间\([x,y]\)的任何一个部位都被至少一条线段覆盖. 首先有一个显然 ...
- HDU 1166 敌兵布阵 Segment Tree题解
本题是最主要的分段树操作了.或者一般叫线段树,只是好像和线段没什么关系,仅仅是分段了. 不使用lazy标志,更新仅仅是更新单点. 假设不使用分段树,那么更新时间效率仅仅须要O(1),使用分段树更新效率 ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- HDU 全国多校第四场 题解
题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...
- LeetCode编程训练 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
- 算法与数据结构基础 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
随机推荐
- 记一次Python pip安装失败的总结
pip 安装失败时,可能换此方法可解决1.升级pip版本,这个一般会主动提示python3 -m pip install --upgrade pip2.修改pip源,默认的pip源速度实在无法忍受,或 ...
- seanborn使用函数regplot回归分析绘图
可以用regplot(x, y, data)绘制回归图.data参数是DataFram类型,x是其中某一列列名,是即将绘制的图的x坐标,y是其中某一列,是图的y坐标 下面代码是对seaborn内置数据 ...
- 【Linux开发】全面的framebuffer详解
全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...
- 使用使用dockerfile构建webapi镜像然后使用link和bridge两种方式进行桥接
首先新增一个webapi的项目 项目核心代码 UserContext using Microsoft.EntityFrameworkCore; using System; using System.C ...
- nginx rewrite + 排错方法 + server_name 172.19.134.43
upstream space.two.cn { ip_hash; #ip hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题. serve ...
- [DS+Algo] 003 一维表结构 Python 代码实现
接上一篇 前言 本篇共 3 个代码实现 严格来说 code1 相当于模仿了 Python 的 list 的部分简单功能 code2 与 code3 简单实现了"循环单链表"与&qu ...
- 函数 FUNCTION
函数 FUNCTION 定义: 带名字的代码块,用于执行具体任务. 注意: 应给函数指定描述性名称,只是用小写字母和下划线. 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何 ...
- Oracle SQL调优
在多数情况下,Oracle使用索引t来更快地遍历表,优化器主要根据定义的索引来提高性能. 但是,如果在SQL语句的where子句中写的SQL代码不合理,就会造成优化器删去索引而使用全表扫描,一般就这种 ...
- numpy数组的运算
numpy数组的运算 数组的乘法 >>> import numpy as np >>> arr=np.array([[1,2,3],[4,5,6]]) >&g ...
- application详解
Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的.本文主要详细介绍Application对象的用法. 一.全局应用程序类 ...