-
Notifications
You must be signed in to change notification settings - Fork 0
/
149_Max Points on a Line.cpp
129 lines (122 loc) · 3.19 KB
/
149_Max Points on a Line.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x,y;
Point(): x(0), y(0) {}
Point(int a, int b): x(a), y(b) {}
};
// 纯暴力,复杂度过高!!
class Solution {
public:
int maxPoints(vector<Point>& points) {
if (points.size() == 0) return 0;
if (points.size() == 1) return 1;
vector<pair<Point, Point>> lines;
makeAllLines(points, lines);
return getMaxPoints(points, lines);
}
int getMaxPoints(vector<Point>& points, vector<pair<Point, Point>>& lines) {
int res = 0;
for (int i = 0; i < lines.size(); ++i) {
pair<Point, Point> ln = lines[i];
Point a = ln.first, b = ln.second;
int cnt = 0;
for (int j = 0; j < points.size(); ++j) {
Point p = points[j];
if ((p.x == a.x && p.y == a.y)||(p.x == b.x && p.y == b.y)) {
cnt++;
continue;
}
if (a.x == b.x) {
if (p.x == a.x) cnt++;
} else {
long long temp1 = (b.y - a.y), temp2 = (p.x - a.x);
long long temp3 = (b.x - a.x), temp4 = (p.y - a.y);
if (temp1*temp2 == temp3*temp4) cnt++;
}
}
res = max(res, cnt);
}
return res;
}
void makeAllLines(vector<Point>& points, vector<pair<Point, Point>>& lines) {
for (int i = 0; i < points.size(); ++i) {
for (int j = i+1; j < points.size(); ++j) {
Point a = points[i], b = points[j];
lines.push_back(make_pair(a,b));
}
}
}
};
// 上述方案的优化,在遍历点的时候就可以将他们的斜率计算出来,两条直线,如果过同一个点,并且两条直线的斜率一致,那么说明这两条直线重合
class Solution2 {
public:
int maxPoints(vector<Point>& points) {
int res = 0;
for (int i = 0; i < points.size(); ++i) {
map<pair<int, int>, int> m;
int dup = 1;
Point a = points[i], b;
for (int j = i+1; j < points.size(); ++j) {
b = points[j];
if (a.x == b.x && a.y == b.y) {
dup++;
continue;
}
int dy = b.y - a.y;
int dx = b.x - a.x;
int gcd = getGCD(dy, dx);
m[make_pair(dy/gcd, dx/gcd)]++;
}
res = max(res, dup);
for (map<pair<int, int>, int>::iterator it = m.begin(); it!=m.end(); ++it) {
res = max(res, (it->second)+dup);
}
}
return res;
}
int getGCD(int a, int b) {
if (b == 0) return a;
return getGCD(b, a%b);
}
};
int main() {
Solution2 s;
vector<Point> points;
int n;
while (cin >> n) {
points.clear();
int x, y;
for (int i = 0; i < n; ++i) {
cin >> x >> y;
points.push_back(Point(x,y));
}
cout << s.maxPoints(points) << endl;
}
return 0;
}
/**
3
1 1
2 2
3 3
3
6
1 1
3 2
5 3
4 1
2 3
1 4
4
3
0 0
1 65536
65536 0
2
3
0 0
1 3
3 0
2
*/