2014-05-08 05:16

题目链接

原题:

Given a circle with N defined points and a point M outside the circle, find the point that is closest to M among the set of N. O(LogN)

题目:给定一个圆上的N个点,和一个在这个圆外部的点。请找出这N个点中与外部点最近的那个。要求时间复杂度是对数级的。

解法1:这位“Guy”老兄又出了一道莫名奇妙的题:1. 这些点是等距离的吗?2. 这些点是顺时针还是逆时针排列的?在没有比较清楚思路的情况下,我只写了个O(n)枚举的算法。

代码:

 // http://www.careercup.com/question?id=4877486110277632
#include <cmath>
#include <iostream>
#include <vector>
using namespace std; struct Point {
double x;
double y;
Point(double _x = , double _y = ): x(_x), y(_y) {};
}; double dist(const Point &p1, const Point &p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
} int main()
{
int i, n;
Point pout;
vector<Point> vp;
int min_i;
double d, min_d; while (cin >> n && n > ) {
vp.resize(n);
for (i = ; i < n; ++i) {
cin >> vp[i].x >> vp[i].y;
}
cin >> pout.x >> pout.y; min_i = ;
min_d = dist(pout, vp[]);
for (i = ; i < n; ++i) {
d = dist(pout, vp[i]);
min_i = d < min_d ? i : min_i;
}
cout << '(' << vp[min_i].x << ',' << vp[min_i].y << ')' << endl;
cout << min_d << endl;
vp.clear();
} return ;
}

解法2:实际上这题不但有对数级算法,还有常数级算法。但有一个额外条件需要满足:我得知道圆心在哪儿。计算圆心需要把所有点的坐标求平均值,那样的算法复杂度还是线性的。如果我们定义P[i]为圆上的那N个点,O为圆心,M为圆外的那个点。那么我们连接OP[i]与OM,可以发现OM与OP[i]的夹角分布是循环有序的(参见Leetcode里面的Rotated Sorted Array),条件是这N个点呈顺时针或逆时针分布。你可以通过二分得到距离最小的结果,但更快的算法是常数级的。你只要计算一个夹角,就知道所有的了。因为这些夹角是个等差数列。比如四个点中,有一个的夹角是73°,那么另外三个肯定是163°、107°(253°)、17°(343°)。谁的距离最短呢?角度最小的就是了,注意优角要换算成锐角或钝角。想要通过一次计算就解决问题,用除法和取模的思想吧。此处的代码默认点是按照顺时针排列的,否则为了判断哪个方向,又得进行一些计算。那样的话,代码都乱的看不清楚了。

代码:

 // http://www.careercup.com/question?id=4877486110277632
#include <cmath>
#include <iostream>
#include <vector>
using namespace std; struct Point {
double x;
double y;
Point(double _x = , double _y = ): x(_x), y(_y) {}; Point operator - (const Point &other) {
return Point(x - other.x, y - other.y);
}; Point operator + (const Point &other) {
return Point(x + other.x, y + other.y);
}; double operator * (const Point &other) {
return x * other.x + y * other.y;
};
}; double dist(const Point &p1, const Point &p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
} int main()
{
int i, n;
Point pout;
vector<Point> vp;
Point center;
Point v0, vout;
// the angle between OM and a line of center
double angle;
// 2 * pi / n
double side_angle;
const double pi = 3.1415926;
double d; while (cin >> n && n > ) {
vp.resize(n);
for (i = ; i < n; ++i) {
cin >> vp[i].x >> vp[i].y; }
cin >> center.x >> center.y;
cin >> pout.x >> pout.y; v0 = vp[] - center;
vout = pout - center; side_angle = * pi / n;
angle = arccos((v0 * vout) / (dist(vp[], center) * dist(pout, center)));
d = angle / side_angle;
// Here I assume the points are arranged in clockwise order.
i = d - floor(d) < 0.5 ? floor(d) : floor(d) + ;
cout << vp[i].x << ' ' << vp[i].y << endl; vp.clear();
} return ;
}

Careercup - Google面试题 - 4877486110277632的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. 抢滩登陆游戏android源码

    是3d游戏开发技术详解与技术案例书里的一个例子 不多说上图{:soso_e113:} 源码下载地址:http://code.662p.com/view/2271.html <ignore_js_ ...

  2. IntelliJ IDEA 快捷键和设置

    IntelliJ IDEA 使用总结 http://my.oschina.net/xianggao/blog/97539 IntelliJ IDEA 问题解决:1.乱码,主要是快捷键的字样显示乱码 中 ...

  3. pap与chap协议

    1.pap:直接在网络上发送密码明文 2.chap: 网络上发送的是密码的密文;server给client发一段随机数(challenge),client利用随机数对密码进行加密,将用户名和加密后的密 ...

  4. vim编辑器编程配置

    打开/etc/vim/vimrc 添加命令: set cindent  "使用C样式的缩进 syntax on   "语法高亮 set tabstop=4 set softtabs ...

  5. html5 canvas画板

    点击查看演示地址 <!DOCTYPE HTML> <html> <title>HTML5-CanvasDrawDemo</title> <meta ...

  6. win7 C# winForm编程 savefiledialog 不能弹出保存窗体

    public void ResMsg()        {            while (isRecMsg)            {                //准备一个数组 准备接收 ...

  7. Java编程性能优化

    1尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 第一,控制资源的使用,通过线程同步来控制资 ...

  8. 阿里云OSS上传图片,并使用图片服务裁切

    <?php use OSS\OssClient; require_once './autoload.php'; // test $bucket = "在阿里云设置的bucket名字(这 ...

  9. jQuery在HTML文档加载完毕后自动执行某个事件;

    原来onchange=“fucntionname(parms)”: <select name="country" id="selCountries_{$sn}&qu ...

  10. Java找出所有的水仙花数并输出

    水仙花数是三位数,它的各位数字的立方和等于这个三位数本身,例如:371=33+73+13,371就是一个水仙花数. 要判断是否是水仙花数,首先得得到它的每一位上的数.个位数即为对10取余:十位数为对1 ...