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. 第五章_PHP流程控制

    1.顺序结构 2.分支结构 2.1 if...else <?php $today=date("w"); //获取今天星期几 if($today==0){ echo 'Sund ...

  2. 学生信息管理系统应用ios源码iPad版

    学生信息管理系统应用iPad版,该应用源码比较完整的,而且也很详细,这也是一款学校用的学生和老师管理系统,里面涉及到了很多ipad常用的控件,操作和数据存储. <ignore_js_op> ...

  3. 在WP8项目中使用ARMASM

    由于之前项目中某些密集运算优化的需要,涉及到ARMASM相关的内容, 所以有幸可以在此分享一下自己的经验. 先铺垫一些知识: 1. ARM处理器有两种指令ARM.THUMB, 在WP8下默认是THUM ...

  4. 实现Win7远程桌面关机和重启

    通过远程桌面控制Win7系统时,菜单中没有关机和重启按钮, 1.方法1 关机 shutdown -s -t 0重启 shutdown -r -t 0 可以先打开运行框(Win+R键),输入上述命令即可 ...

  5. 日期转换(用DateTime的ParseExact方法解析特殊的日期时间)

    今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象: [07-13 15:50:42] 主要问题是这个时间不是标准的时间,而是自定义的格式,即开头是月-日,然后是时间. 使用最 ...

  6. ThinkPHP之中利用commom被继承控制器控制访问每一个控制器方法都需要验证是否已经登录!

    防止 <?php namespace Home\Controller; use Think\Controller; class CommonController extends Controll ...

  7. php常用函数集锦[备份用的]

    1.判断是否正确的日期格式 /** * 是否正确的日期 * * @access public */ private function _isdate($str,$format="Y-m-d ...

  8. Yii-数据模型- rules类验证器方法详解

    public function rules(){ return array( array('project_id, type_id, status_id, owner_id, requester_id ...

  9. SQLite数据库管理的相关命令

    1.创建数据库 启动命令行,通过输入如下命令打开Shell模式的CLP: sqlite3 test.db 虽然我们提供了数据库名称,但如果该数据库不存在,SQLite实际上就未创建该数据库,直到在数据 ...

  10. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...