forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruitslicer.cpp
141 lines (106 loc) · 3.65 KB
/
fruitslicer.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
130
131
132
133
134
135
136
137
138
139
140
#include <bits/stdc++.h>
using namespace std;
#define eps 0.00000001
typedef long double ld;
ld linePointDistSqr(ld x1, ld y1, ld dirX, ld dirY, ld x, ld y) {
ld segLen = dirX*dirX + dirY*dirY;
ld proj = dirX*(x-x1) + dirY*(y-y1);
proj /= segLen;
ld projX = dirX*proj + x1;
ld projY = dirY*proj + y1;
ld result = (x-projX)*(x-projX) + (y-projY)*(y-projY);
return result;
}
int main() {
int n;
cin >> n;
vector<pair<ld, ld> > arr(n);
map<pair<ld, ld>, int> cnts;
for(int i = 0; i < n; ++i) {
cin >> arr[i].first >> arr[i].second;
cnts[{arr[i].first, arr[i].second}]++;
}
vector<pair<pair<ld, ld>, int> > arr2;
for(auto &p : cnts) {
//cout << "pushing: " << p.first.first << ' ' << p.first.second << ' ' << p.second << '\n';
arr2.push_back(p);
}
if(arr2.size() == 1) {
cout << n << '\n';
return 0;
}
int largest = 2;
n = arr2.size();
for(int i = 0; i < n; ++i) {
ld x1 = arr2[i].first.first;
ld y1 = arr2[i].first.second;
for(int j = i+1; j < n; ++j) {
ld x2 = arr2[j].first.first;
ld y2 = arr2[j].first.second;
ld offsetX = x2-x1;
ld offsetY = y2-y1;
swap(offsetX, offsetY);
offsetY = -offsetY;
//normalize offset here
ld sqrMag = offsetX*offsetX + offsetY*offsetY;
ld mag = sqrtl(sqrMag);
offsetX /= mag;
offsetY /= mag;
ld lineX = x1+offsetX;
ld lineY = y1+offsetY;
ld lineDirX = x2-x1;
ld lineDirY = y2-y1;
//test line
int cnt = 0;
for(int k = 0; k < n; ++k) {
if(linePointDistSqr(lineX, lineY, lineDirX, lineDirY, arr2[k].first.first, arr2[k].first.second) <= 1 + eps) {
cnt += arr2[k].second;
}
}
//cout << "1: " << cnt << '\n';
largest = max(largest, cnt);
lineX = x1-offsetX;
lineY = y1-offsetY;
//test line
cnt = 0;
for(int k = 0; k < n; ++k) {
if(linePointDistSqr(lineX, lineY, lineDirX, lineDirY, arr2[k].first.first, arr2[k].first.second) <= 1 + eps) {
cnt += arr2[k].second;
}
}
//cout << "2: " << cnt << '\n';
largest = max(largest, cnt);
ld midX = (x1+x2)/2;
ld midY = (y1+y2)/2;
ld h = sqrtl((x1-midX)*(x1-midX) + (y1-midY)*(y1-midY));
ld theta = asin(1.0/h);
ld theta2 = atan2(y2-y1, x2-x1);
lineDirX = cos(theta2+theta);
lineDirY = sin(theta2+theta);
lineX = midX;
lineY = midY;
//test line
cnt = 0;
for(int k = 0; k < n; ++k) {
if(linePointDistSqr(lineX, lineY, lineDirX, lineDirY, arr2[k].first.first, arr2[k].first.second) <= 1 + eps) {
cnt += arr2[k].second;
}
}
//cout << "3: " << cnt << '\n';
largest = max(largest, cnt);
lineDirX = cos(theta2-theta);
lineDirY = sin(theta2-theta);
//test line
cnt = 0;
for(int k = 0; k < n; ++k) {
if(linePointDistSqr(lineX, lineY, lineDirX, lineDirY, arr2[k].first.first, arr2[k].first.second) <= 1 + eps) {
cnt += arr2[k].second;
}
}
//cout << "4: " << cnt << '\n';
largest = max(largest, cnt);
}
}
cout << largest << '\n';
return 0;
}