2014-03-20 02:24

题目:给定二位平面上一堆点,找到一条直线,使其穿过的点数量最多。

解法:我的解法只能适用整点,对于实数坐标就得换效率更低的办法了。请参见LeetCode - Max Points on a Line - zhuli19901106 - 博客园

代码:

 // 7.6 Find the line that crosses the most points.
#include <unordered_map>
#include <vector>
using namespace std; struct Point {
int x;
int y;
Point() : x(), y() {}
Point(int a, int b) : x(a), y(b) {}
}; struct st {
int x;
int y;
st(int _x = , int _y = ): x(_x), y(_y) {}; bool operator == (const st &other) const {
return x == other.x && y == other.y;
}; bool operator != (const st &other) const {
return x != other.x || y != other.y;
};
}; struct line {
// ax + by + c = 0;
int a;
int b;
int c;
line(int _a = , int _b = , int _c = ): a(_a), b(_b), c(_c) {};
}; struct hash_functor {
size_t operator () (const st &a) const {
return (a.x * + a.y);
}
}; struct compare_functor {
bool operator () (const st &a, const st &b) const {
return (a.x == b.x && a.y == b.y);
}
}; typedef unordered_map<st, int, hash_functor, compare_functor> st_map; class Solution {
public:
int maxPoints(vector<Point> &points, line &result_line) {
int n = (int)points.size(); if (n <= ) {
return n;
} st_map um;
st tmp;
// special tangent value for duplicate points
st dup(, ); int i, j;
st_map::const_iterator umit;
int dup_count;
int max_count;
int result = ;
for (i = ; i < n; ++i) {
for (j = i + ; j < n; ++j) {
tmp.x = points[j].x - points[i].x;
tmp.y = points[j].y - points[i].y;
// make sure one tangent value has one representation only.
normalize(tmp);
if (um.find(tmp) != um.end()) {
++um[tmp];
} else {
um[tmp] = ;
}
}
max_count = dup_count = ;
for (umit = um.begin(); umit != um.end(); ++umit) {
if (umit->first != dup) {
if (umit->second > max_count) {
max_count = umit->second;
getLine(umit->first, points[i], result_line);
}
} else {
dup_count = umit->second;
}
}
max_count = max_count + dup_count + ;
result = (max_count > result ? max_count : result);
um.clear();
} return result;
}
private:
void normalize(st &tmp) {
if (tmp.x == && tmp.y == ) {
// (0, 0)
return;
}
if (tmp.x == ) {
// (0, 1)
tmp.y = ;
return;
}
if (tmp.y == ) {
// (1, 0)
tmp.x = ;
return;
}
if (tmp.x < ) {
// (-2, 3) and (2, -3) => (2, -3)
tmp.x = -tmp.x;
tmp.y = -tmp.y;
} int gcd_value; gcd_value = (tmp.y > ? gcd(tmp.x, tmp.y) : gcd(tmp.x, -tmp.y));
// (4, -6) and (-30, 45) => (2, -3)
// using a double precision risks in accuracy
// so I did it with a pair
tmp.x /= gcd_value;
tmp.y /= gcd_value;
} int gcd(int x, int y) {
// used for reduction of fraction
return y ? gcd(y, x % y) : x;
} void getLine(st tan, Point p, line &res) {
res.a = tan.y;
res.b = -tan.x;
res.c = -(res.a * p.x + res.b * p.y);
}
}; int main()
{
vector<Point> v;
Solution sol;
int i, n;
Point p;
line res; while (scanf("%d", &n) == ) {
for (i = ; i < n; ++i) {
scanf("%d%d", &p.x, &p.y);
v.push_back(p);
}
sol.maxPoints(v, res);
printf("%d %d %d\n", res.a, res.b, res.c);
v.clear();
} return ;
}

《Cracking the Coding Interview》——第7章:数学和概率论——题目6的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第7章:数学和概率论——题目7

    2014-03-20 02:29 题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个. 解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三 ...

  8. 《Cracking the Coding Interview》——第7章:数学和概率论——题目5

    2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...

  9. 《Cracking the Coding Interview》——第7章:数学和概率论——题目4

    2014-03-20 02:16 题目:只用加法和赋值,实现减法.乘法.除法. 解法:我只实现了整数范围内的.减法就是加上相反数.乘法就是连着加上很多个.除法就是减到不能减为止,数数总共减了多少个. ...

随机推荐

  1. 古老的pike

    快速略读了一下源码,记了一些东西. 先看看mapping mapping其实就是C++中的multimap,但是支持更多. array values(mapping).这个方法可以返回所有mappin ...

  2. C++ new new[]详解

    精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...

  3. CRM, C4C和Hybris的后台作业

    CRM 使用事务码SM36查看CRM系统的后台作业: 举一些例子: ABAP_TEXT_INDEX这个job执行的report是ABAP_DOCU_CREATE_TEXT_INDEX: 负责填充buf ...

  4. Facebook interview problem:13. Roman to Integer

    description: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  5. MHA 日常维护命令集

    MHA 日常维护命令集 1.查看ssh登陆是否成功 masterha_check_ssh --global_conf=/etc/masterha/masterha_default.conf --con ...

  6. SQL随手记

    数据库改名 想要达到的效果,类似于将一个文件[复制粘贴重命名]. 0.首先得断开连接,复制一份备份.然后再连接上,进行下面的操作. 1.在树形上,选中要改名的数据库,右键重命名. 2.还是它,右键,属 ...

  7. rabbitmq安装使用

    使用 http://www.open-open.com/lib/view/open1325131828249.html ubuntu:apt-get install erlang-noxsudo ap ...

  8. ofbiz研究

    近段时间,刚有有时间研究了下ofbiz ; 目前还是刚开始,后期会记录过程 有一起研究的没

  9. 基于form表单submit提交不跳转

    方法一:target <html> <body> <form action="" method="post" target=&qu ...

  10. Lo、Hi、HiByte、LoWord、HiWord、MakeWord、MakeLong、Int64Rec

    本话题会涉及到: Lo.Hi.HiByte.LoWord.HiWord.MakeWord.MakeLong.Int64Rec 譬如有一个 Cardinal 类型的整数: 1144201745其十六进制 ...