-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathB3.cpp
More file actions
188 lines (163 loc) · 6.23 KB
/
B3.cpp
File metadata and controls
188 lines (163 loc) · 6.23 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
double safe_sqrt(double x) {
return sqrt(max((double)0.0,x));
}
int GI(ll& x) {
return scanf("%lld", &x);
}
#define EPS 1e-6
const double PI = acos(-1.0);
double DEG_to_RAD(double d) { return d * PI / 180.0; }
double RAD_to_DEG(double r) { return r * 180.0 / PI; }
inline int cmp(double a, double b) {
return (a < b - EPS) ? -1 : ((a > b + EPS) ? 1 : 0);
}
struct Point { // <--> Vector
double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
Point operator + (const Point& a) const { return Point(x+a.x, y+a.y); }
Point operator - (const Point& a) const { return Point(x-a.x, y-a.y); }
Point operator * (double k) const { return Point(x*k, y*k); }
Point operator / (double k) const { return Point(x/k, y/k); }
bool operator<(const Point &rhs) const { return make_pair(y,x) < make_pair(rhs.y,rhs.x); }
bool operator==(const Point &rhs) const { return make_pair(y,x) == make_pair(rhs.y,rhs.x); }
double operator * (const Point& a) const { return x*a.x + y*a.y; } // dot product
double operator % (const Point& a) const { return x*a.y - y*a.x; } // cross product
int cmp(Point q) const { if (int t = ::cmp(x,q.x)) return t; return ::cmp(y,q.y); }
Point conj() { return Point(x, -y); }
double norm() { return x*x + y*y; }
// Note: There are 2 ways for implementing len():
// 1. sqrt(norm()) --> fast, but inaccurate (produce some values that are of order X^2)
// 2. hypot(x, y) --> slow, but much more accurate
double len() { return sqrt(norm()); }
Point rotate(double alpha) {
double cosa = cos(alpha), sina = sin(alpha);
return Point(x * cosa - y * sina, x * sina + y * cosa);
}
};
int ccw(Point a, Point b, Point c) {
return cmp((b-a)%(c-a),0);
}
typedef vector< Point > Polygon;
double area2(Point a, Point b, Point c) { return a%b + b%c + c%a; }
bool between(const Point &a, const Point &b, const Point &c) {
return (fabs(area2(a,b,c)) < EPS && (a.x-b.x)*(c.x-b.x) <= 0 && (a.y-b.y)*(c.y-b.y) <= 0);
}
void ConvexHull(vector<Point> &pts) {
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
vector<Point> up, dn;
for (int i = 0; i < pts.size(); i++) {
while (up.size() > 1 && area2(up[up.size()-2], up.back(), pts[i]) >= 0) up.pop_back();
while (dn.size() > 1 && area2(dn[dn.size()-2], dn.back(), pts[i]) <= 0) dn.pop_back();
up.push_back(pts[i]);
dn.push_back(pts[i]);
}
pts = dn;
for (int i = (int) up.size() - 2; i >= 1; i--) pts.push_back(up[i]);
if (pts.size() <= 2) return;
dn.clear();
dn.push_back(pts[0]);
dn.push_back(pts[1]);
for (int i = 2; i < pts.size(); i++) {
if (between(dn[dn.size()-2], dn[dn.size()-1], pts[i])) dn.pop_back();
dn.push_back(pts[i]);
}
if (dn.size() >= 3 && between(dn.back(), dn[0], dn[1])) {
dn[0] = dn.back();
dn.pop_back();
}
pts = dn;
}
istream& operator >> (istream& cin, Point& p) {
cin >> p.x >> p.y;
return cin;
}
ostream& operator << (ostream& cout, Point& p) {
cout << p.x << ' ' << p.y;
return cout;
}
#define Det(a,b,c) ((double)(b.x-a.x)*(double)(c.y-a.y)-(double)(b.y-a.y)*(c.x-a.x))
bool in_convex(vector<Point>& l, Point p){
int a = 1, b = l.size()-1, c;
if (Det(l[0], l[a], l[b]) > 0) swap(a,b);
// Allow on edge --> if (Det... > 0 || Det ... < 0)
if (Det(l[0], l[a], p) >= 0 || Det(l[0], l[b], p) <= 0) return false;
while(abs(a-b) > 1) {
c = (a+b)/2;
if (Det(l[0], l[c], p) > 0) b = c; else a = c;
}
// Alow on edge --> return Det... <= 0
return Det(l[a], l[b], p) < 0;
}
Polygon cv123;
set< pair<int,int> > all123;
bool ok(int x, int y) {
return all123.count(make_pair(x, y))
|| in_convex(cv123, Point(x, y));
}
bool check(int x, int y) {
return ok(x, y) && ok(x+1, y) && ok(x, y+1) && ok(x+1, y+1);
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
int n, m;
while (GI(n) == 1 && GI(m) == 1 && n && m) {
// Generate magic polygon for checking 0 - 4
all123.clear();
cv123.clear();
FOR(i,1,m) {
int x, y; GI(x); GI(y);
all123.insert(make_pair(x, y));
cv123.push_back(Point(x, y));
}
ConvexHull(cv123);
// Generate candidate points
set< pair<int,int> > candidates;
for(auto p : all123) {
int x = p.first;
int y = p.second;
candidates.insert(make_pair(x, y));
candidates.insert(make_pair(x, y-1));
candidates.insert(make_pair(x-1, y));
candidates.insert(make_pair(x-1, y-1));
}
// check candidate points
Polygon all;
for(auto p : candidates) {
if (check(p.first, p.second)) all.push_back(Point(p.first, p.second));
}
ConvexHull(all);
// print answer
if (ccw(all[0], all[1], all[2]) > 0) reverse(all.begin(), all.end());
vector< pair<int,int> > res;
for(auto p : all) {
res.push_back(make_pair(
(int) (round(p.x) + 1e-6),
(int) (round(p.y) + 1e-6)));
}
int start = 0;
FOR(i,1,SZ(res)-1)
if (res[i] < res[start]) start = i;
cout << SZ(res) << '\n';
REP(i,SZ(res)) {
int u = (i + start) % SZ(res);
cout << res[u].first << ' ' << res[u].second << '\n';
}
}
}