题意简述

原题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 题解的更多相关文章

  1. 三分题两道: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 ...

  2. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

  3. POJ2689:Prime Distance——题解

    http://poj.org/problem?id=2689 题目大意,给不超过int的l,r,其中r-l+1<=1000000,筛出其中的素数,并且求出相邻素数差值最大和最小的一对. ———— ...

  4. CF1175E Minimal Segment Cover 题解

    题意:给出\(n\)个形如\([l,r]\)的线段.\(m\)次询问,每次询问区间\([x,y]\),问至少选出几条线段,使得区间\([x,y]\)的任何一个部位都被至少一条线段覆盖. 首先有一个显然 ...

  5. HDU 1166 敌兵布阵 Segment Tree题解

    本题是最主要的分段树操作了.或者一般叫线段树,只是好像和线段没什么关系,仅仅是分段了. 不使用lazy标志,更新仅仅是更新单点. 假设不使用分段树,那么更新时间效率仅仅须要O(1),使用分段树更新效率 ...

  6. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

  7. HDU 全国多校第四场 题解

    题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...

  8. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  9. 算法与数据结构基础 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

随机推荐

  1. 【HANA系列】SAP HANA SQL获取当前日期

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取当前 ...

  2. 【MM系列】SAP MR21修改标准价

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]在SAP里查看数据的方法   前言部 ...

  3. Java中类和接口

    很形象的接口的使用——针对初学者 里氏代换原则是什么?听起来很高深,不过我们也不是什么学院派,就不讲大道理了,直接拿个例子来说一下. 我们拿人和程序员举个例子.人是一个大类,程序员是继承自人的子类.看 ...

  4. python 并发编程 查看进程的id pid与父进程id ppid

    查看进程id pid 不需要传参数 from multiprocessing import Process import time import os def task(): print(" ...

  5. 第九周总结&第七次实验报告

    实验7 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 实验过程 ...

  6. CentOS7 内核模块管理

    1.查看所有模块:lsmod 2.查看指定模块的详细信息:modinfo 模块名 3.动态加载模块:modprobe 模块名 4.动态卸载模块:modprobe -r 模块名 5.开机自动加载模块:假 ...

  7. 2. Docker部署tomcat, nginx, redis,及docker私有仓库

    1. 部署tomcat 1.1 下载tomcat       docker pull tomcat:7-jre8 1.2 部署容器  docker run -di --name=tomcat -p 8 ...

  8. jekins部署

    以下以在MAC上安装为例: 1.从官网下载安装包 https://jenkins.io/download 2.双击pkg包安装程序开始安装.(这种方式安装的不好卸载) 提示输入密码,按照提示的路径找到 ...

  9. 虚拟机的linux(CentOS)系统安装

    一.安装虚拟机:VirtualBox  1.软件下载:https://www.virtualbox.org/  2.一路点击下一步完成安装 二.安装linux:CentOS6.8  1.官方网站:ht ...

  10. 神经网络之:S型神经元

    1.S 型神经元和感知器类似,但是被修改为权重和偏置的微小改动只引起输出的微小变化 2.S型神经元结构: S型函数: 带有x1,x2,........,权重w1,w2.....,和偏置b的S型神经元的 ...