-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
136 lines (123 loc) · 3.35 KB
/
main.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
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
struct Point
{
double x;
double y;
Point() : x(0), y(0) {}
Point(double a, double b) : x(a), y(b) {}
};
double crossProduct(Point A, Point B, Point C)
{
double BAx = A.x - B.x;
double BAy = A.y - B.y;
double BCx = C.x - B.x;
double BCy = C.y - B.y;
return BAx * BCy - BAy * BCx;
}
double dist(Point A, Point B)
{
return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);
}
bool check(unordered_set<int>& res, Point p, vector<Point> &points)
{
for (auto i: res)
{
if (points[i].x == p.x && points[i].y == p.y)
return false;
}
return true;
}
unordered_set<int> convex(vector<Point>& points) {
unordered_set<int> res;
Point first = points[0];
int firstIdx = 0, n = points.size();
for (int i = 1; i < n; ++i) {
if (points[i].x < first.x) {
first = points[i];
firstIdx = i;
}
}
res.insert(firstIdx);
Point cur = first;
int curIdx = firstIdx;
while (true) {
Point next = points[0];
int nextIdx = 0;
for (int i = 1; i < n; ++i) {
if (i == curIdx) continue;
int cross = crossProduct(cur, points[i], next);
if (nextIdx == curIdx || cross > 0 || (cross == 0 && dist(points[i], cur) > dist(next, cur))) {
next = points[i];
nextIdx = i;
}
}
for (int i = 0; i < n; ++i) {
if (i == curIdx) continue;
int cross = crossProduct(cur, points[i], next);
if (cross == 0) {
if (check(res, points[i], points)) res.insert(i);
}
}
cur = next;
curIdx = nextIdx;
if (curIdx == firstIdx) break;
}
return res;
}
vector<string> mysplit(string s, char t){
vector<string> info;
string cur;
for(auto c: s){
if(c == t){
if(cur != "") info.push_back(cur);
cur = "";
}
else cur += c;
}
if(cur != "") info.push_back(cur);
return info;
}
int main()
{
string line;
while (getline(cin, line)) {
vector<string> tmp = mysplit(line, ';');
int n = stoi(tmp[0]);
vector<Point> points(n);
for(int i = 1; i <= n; i++){
vector<string> xy = mysplit(tmp[i], ',');
points[i-1].x = stod(xy[0]);
points[i-1].y = stod(xy[1]);
}
vector<bool> vis(n, false);
vector<Point> cur = points, bf;
int ans = 0;
while(true){
if(cur.size() < 3) break;
unordered_set<int> index = convex(cur);
bf = cur;
ans++;
//for(auto i: index) cout << i << " ";
//cout << endl;
vector<Point> tmp;
for(int i = 0; i < cur.size(); i++)
if(index.find(i) == index.end())
tmp.push_back(cur[i]);
cur = tmp;
}
if(cur.size() == 0){
cur = bf;
unordered_set<double> st;
for(int i = 1; i < cur.size(); i++){
if(cur[i].x == cur[0].x) st.insert(INT_MAX);
else st.insert((cur[i].y-cur[0].y)/(cur[i].x-cur[0].x));
}
if(st.size() == 1) ans--;
}
cout << ans << endl;
}
return 0;
}