Nearest Neighbor Search
## Nearest Neighbor Search ##
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes
Bobo has a point p and a cube C in 3-dimension space. The point locates at coordinate (x0, y0, z0), while
C = {(x, y, z) : x1 ≤ x ≤ x2, y1 ≤ y ≤ y2, z1 ≤ z ≤ z2}.
Bobo would like to find another point q which locates inside or on the surface of the cube C so that the
square distance between point p and q is minimized.
Note that the square distance between point (x, y, z) and (x′, y′, z′) is (x − x′)2 + (y − y′)2 + (z −z′)2.
Input
The first line contains 3 integers x0, y0, z0.
The second line contains 3 integers x1, y1, z1.
The third line contains 3 integers x2, y2, z2.
(|xi|, |yi|, |zi| ≤ 104, x1 < x2, y1 < y2, z1 < z2)
Output
An integer denotes the minimum square distance.
Examples
standard input standard output
0 0 0
1 1 1
2 2 2
3
1 1 1
0 0 0
2 2 2
0
题目连接:https://acm.bnu.edu.cn/v3/statments/52296.pdf
真的是水到不能再水的题,虽然比赛的时候没做出来。。。,q点的x,y,z坐标可以独立求,每次都求最小值,如果点q在cube C之中的话,距离肯定是0,问题就是判断在cube C的外面的情况,这种情况下求到两端范围内的最小值就行了。
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=1e9+7;
const int maxn=101000;
struct Node
{
int x,y,z;
};
long long getans(Node a,Node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}
int main()
{
Node a,b,c;
while(scanf("%d%d%d%d%d%d%d%d%d",&a.x,&a.y,&a.z,&b.x,&b.y,&b.z,&c.x,&c.y,&c.z)!=EOF)
{
Node t;
if((a.x>=b.x&&a.x<=c.x)||(a.x<=b.x&&a.x>=c.x)) t.x=a.x;
else t.x=(abs(a.x-b.x)>abs(a.x-c.x))?c.x:b.x;
if((a.y>=b.y&&a.y<=c.y)||(a.y<=b.y&&a.y>=c.y)) t.y=a.y;
else t.y=(abs(a.y-b.y)>abs(a.y-c.y))?c.y:b.y;
if((a.z>=b.z&&a.z<=c.z)||(a.z<=b.z&&a.z>=c.z)) t.z=a.z;
else t.z=(abs(a.z-b.z)>abs(a.z-c.z))?c.z:b.z;
printf("%lld\n",getans(a,t));
}
}
Nearest Neighbor Search的更多相关文章
- (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search
题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...
- 【cs231n】图像分类-Nearest Neighbor Classifier(最近邻分类器)【python3实现】
[学习自CS231n课程] 转载请注明出处:http://www.cnblogs.com/GraceSkyer/p/8735908.html 图像分类: 一张图像的表示:长度.宽度.通道(3个颜色通道 ...
- 读论文系列:Nearest Keyword Search in XML Documents中使用的数据结构(CT、ECT)
Reference: [1]Y. Tao, S. Papadopoulos, C. Sheng, K. Stefanidis. Nearest Keyword Search in XML Docume ...
- Nearest neighbor graph | 近邻图
最近在开发一套自己的单细胞分析方法,所以copy paste事业有所停顿. 实例: R eNetIt v0.1-1 data(ralu.site) # Saturated spatial graph ...
- K Nearest Neighbor 算法
文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...
- Visualizing MNIST with t-SNE, MDS, Sammon’s Mapping and Nearest neighbor graph
MNIST 可视化 Visualizing MNIST: An Exploration of Dimensionality Reduction At some fundamental level, n ...
- K NEAREST NEIGHBOR 算法(knn)
K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KNN算法是相对比较容易理解的算法.其中的K表示最接近自己的K个数据样本.KNN算法和K-M ...
- ann搜索算法(Approximate Nearest Neighbor)
ANN的方法分为三大类:基于树的方法.哈希方法.矢量量化方法.brute-force搜索的方式是在全空间进行搜索,为了加快查找的速度,几乎所有的ANN方法都是通过对全空间分割,将其分割成很多小的子空间 ...
- K nearest neighbor cs229
vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...
随机推荐
- jquery实现简单轮播
先上简单的html代码 <!DOCTYPE HTML> <html> <head> <link rel="stylesheet" type ...
- POJ 3233 Matrix Power Series (矩阵快速幂)
题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...
- python设计模式之迭代器与生成器详解(五)
前言 迭代器是设计模式中的一种行为模式,它提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示.python提倡使用生成器,生成器也是迭代器的一种. 系列文章 python设计模 ...
- 在Linux(CentOS)中安装.netcore SDK
官方链接 :https://dotnet.microsoft.com/download/linux-package-manager/centos/sdk-current 可以直接根据官方链接,选择Li ...
- MGR_ERROR 3092 (HY000): DROP DATABASE failed;
start group_replication;时报以下错: ERROR 3092 (HY000): DROP DATABASE failed; some tables may have been d ...
- 细说show slave status参数详解(最全)【转】
在搭建好mysql主从之后,我们一般在从库上通过命令 show slave status\G 来查看主从的状态,会有很多的参数,接下来笔者就带大家好好的了解这些参数 root@localhost (n ...
- Linux中切换用户变成-bash4.1-$的解决方法
原因是root在/root下面的几个配置文件丢失,将/etc/skel/目录下的三个文件拷贝到用户家目录即可 cp /etc/skel/.bashrc /root/ cp /etc/skel/.bas ...
- Python基础之杂货铺
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...
- 11.python3标准库--使用进程、线程和协程提供并发性
''' python提供了一些复杂的工具用于管理使用进程和线程的并发操作. 通过应用这些计数,使用这些模块并发地运行作业的各个部分,即便是一些相当简单的程序也可以更快的运行 subprocess提供了 ...
- 集合遍历过程iterator, 添加删除元素报异常
list set 遍历过程中添加或者删除元素,报异常. 使用iterator 也会报异常 ConcurrentModificationException remove只能用迭代器的remove,而 ...