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 ...
随机推荐
- 不用注解添加controller抛出No adapter for handler异常
不用注解添加controller时会抛出No adapter for handler异常. 解决方法:在DispatcherServlet的配置文件(***-servlet.xml)中加入如下两行: ...
- new Date('2014/04/30') 和 new Date('2014-04-30') 的区别
new Date('2014/04/30') Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间) new Date('2014-04-30'); Wed Apr 30 ...
- 【NOIP】提高组2013 货车运输
[算法]最大生成树+LCA(倍增) [题解]两点间选择一条路径最小值最大的路径,这条路径一定在最大生成树上,因为最大生成树就是从边权最大的边开始加的. 先求原图的最大生成树(森林),重新构图,然后用一 ...
- 用sqoop将mysql的数据导入到hive表中
1:先将mysql一张表的数据用sqoop导入到hdfs中 准备一张表 需求 将 bbs_product 表中的前100条数据导 导出来 只要id brand_id和 name 这3个字段 数据存 ...
- flex实例(阮一峰)
Flex 布局教程:实例篇 作者: 阮一峰 日期: 2015年7月14日 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令 ...
- MFC CListCtrl 将一个列表的选中项添加到另一个列表
MFC CListCtrl 将一个列表的选中项添加到另一个列表, 用VC6.0实现: 简单记录一下自己的学习历程, 和大家分享,如果对你有用,我很高兴. 1.新建一个基于对话框的工程(Dialog-B ...
- php常用函数——数学函数
php常用函数——数学函数
- 生成验证码tp
js里拼接随机数 页面上链接 去掉后缀名
- iOS中UITabelView
1.概述 继承自UIScrollView,只能显示一列数据,只能纵向滑动.堪称UIKit里面最复杂的一个控件了,使用起来不算难,但是要用好并不容易.当使用的时候我们必须要考虑到后台数据的设计,tabl ...
- tomcat+java的web程序持续占cpu高问题调试【转】
转自 tomcat+java的web程序持续占cpu问题调试 - 像风一样的自由 - CSDN博客http://blog.csdn.net/five3/article/details/28416771 ...