《Cracking the Coding Interview》——第7章:数学和概率论——题目6
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目7
2014-03-20 02:29 题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个. 解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目5
2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目4
2014-03-20 02:16 题目:只用加法和赋值,实现减法.乘法.除法. 解法:我只实现了整数范围内的.减法就是加上相反数.乘法就是连着加上很多个.除法就是减到不能减为止,数数总共减了多少个. ...
随机推荐
- 笨办法学Python(十三)
习题 13: 参数.解包.变量 在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序).你已经知道,如果要运行 ex13.py,只要在命令行运行 python e ...
- graphql 项目搭建(二)
一.Express基本框架 1.新建一个文件夹gql-server vscode 打开项目,在终端下输入yarn init -y 生成package.json 包(如果没安装yarn ,npm也一样, ...
- 少年开始学习c#编程,过路的大神请担待!
这应该真正算开始学习编程, 安装好了 linux, 开通了博客员的博客, 同时今天也需要把sublime安好, 安装MonoDevelop Codeblocks mysql-workbench 配置好 ...
- POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图
题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...
- 画X,模拟水题
题目链接:http://codeforces.com/contest/404/problem/A #include <stdio.h> #include <string.h> ...
- spring依赖注入(转)
转自:https://blog.csdn.net/taijianyu/article/details/2338311/ Spring 能有效地组织J2EE应用各层的对象.不管是控 制层的Action对 ...
- CUDA三维数组
http://hpcbbs.it168.com/forum.php?mod=viewthread&tid=1643 根据上面链接的帖子研究了下三维数组,就像他自己说的一样是有问题的,我自己修改 ...
- No such file or directory 8356:error:02001003:system library:fopen:No such process:crypto\bio\bss_file.c:7 4:fopen
使用OpenSSL生成证书,构建根证书前,需要构建随机数文件(.rand),命令如下: openssl rand - 报错如下: OpenSSL> rand - Can't open priva ...
- AngularJS 外部文件中的控制器其他实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-9enuqi/MySQL-python/
hu@hu-VirtualBox:/home/newdisk/telnet-scanner$ sudo pip install MySQL-python[sudo] hu 的密码: The direc ...