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 ...
随机推荐
- 游戏AI:行为树
Behavior Tree 行为树通过子Task的返回值决定整棵树的走向 Task 行为树上的每个节点都称为一个Task, 每个Task存在三种状态, success, failure, runnin ...
- 【CODEVS】1034 家园
[算法]网络流-最大流(dinic) [题解] 飞船有可承载人数限制,地球为源点,月球为汇点,人像水流一样从以飞船上限为容量的边流向汇点. 人在各站点都面临着上船与否的选择,难以用DP解决最优策略,于 ...
- node、npm及node_modules中依赖的版本更新
好久没用node了,想重新拾起来发现node还有相关模块的版本都太低了,使用npm install全是报版本低的警告. 这里记录一下,版本管理和node_modules更新的方法. 我用的是Windo ...
- NYOJ 138 找球号(二) (哈希)
题目链接 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是&qu ...
- python之yagmail库笔记
1. yagmail是啥 yagmail是给正常人用的,封装的比较彻底的一个python邮件库,发送接收邮件只需要几行代码,炒鸡简单. 2. 安装 使用pip安装,炒鸡简单: pip install ...
- Linux mint 18.1 / Ubuntu 16.04 安装steam
这里以Limit Mint 18.1为例: 安装steam: sudo dpkg -i steam.deb 运行后会有如下错误: 直接运行如下命令修复, 并自动启动steam: LD_PRELOAD= ...
- wifi钓鱼 强势拿你的wifi密码
钓鱼wifi 首先设一个场景!!! 如何得到一个免费的wifi 有人可能做过抓包跑包的方法或者跑pin码的方法然而这些方法可能会耗去你大量的时间(我曾经跑包花了一天的时间 跑pin码花了一晚上)感 ...
- MongoDB之安装和基本使用(一)
环境 ubuntu16.04 mongodb基本特点 MongoDB 是一个基于分布式 文件存储的NoSQL数据库;可以把MongoDB想象成一个大py字典. 模式自由 :可以把不同结构的文档存储在同 ...
- linux系统分区参考
UPDATE: update is used to download package information from all configured sources. UPGRADE: upgrad ...
- HTTP 请求 的方法Util
HTTP请求 的一系列方法总结 /** * *******************************传统请求--开始************************************** ...