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. 22 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  2. 命令行输入Jmeter提示不是内部或外部命令,处理方式:添加环境变量

    命令行输入Jmeter提示不是内部或外部命令,需要在环境变量path中添加jmeter的bin目录绝对路径 我的电脑 > 右击 >属性 > 高级系统变量 > 环境变量 > ...

  3. java中string类型转换成map

    背景:有时候string类型的数据取出来是个很标准的key.value形式,通过Gson的可以直接转成map 使用方式: Gson gson = new Gson(); Map<String, ...

  4. SPOJ - ORDERS--- Ordering the Soldiers---根据逆序对求原数组

    题目链接: https://vjudge.net/problem/SPOJ-ORDERS 题目大意: 根据每个数字的逆序对求出原数组 解题思路: 举个例子: n = 5 a[ n ] = { 0, 1 ...

  5. 最小堆的维护,POJ(2051)

    题目链接:http://poj.org/problem?id=2051 ///维持最小堆(优先队列)POJ2051 #include <iostream> #include <str ...

  6. 2018.8.3 Java中容易犯错误的问题思考与总结

    Java容易犯错误的问题思考 float型 float f = 3.4 是否正确 不正确,应该用强制类型转换.如下所示:float f = (float)3.4 或float f = 3.4f 在ja ...

  7. 如何不安装SQLite让程序可以正常使用

    System.Data.SQLite.dll和System.Data.SQLite.Linq.dll不必在GAC里面,关键在于Machine.config的DBProviderFactories没有正 ...

  8. Deep Learning Libraries by Language

    Deep Learning Libraries by Language Tweet         Python Theano is a python library for defining and ...

  9. 项目部署到自己的IIS上

    一般我们只能在本机上才可以开到我们的项目,这个是不需要连网的 如果想让我们的项目在网站中打开,别人也可以看到,就需要把我们的项目部署到服务器上了,输入IP就可以看到我们的项目 发布项目 然后发布网站 ...

  10. 关于vue中的nextTick深入理解

    定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新 ...