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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include #include #include #include #include #include using namespace std; typedef long long ll; const double eps = 1e-8; const double pi = acos(-1.0); const int maxn = 52100; using namespace std; int sgn(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; else return 1; } struct Point { double x, y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } void input() { scanf("%lf%lf", &x, &y); } void output() { printf("%.2f %.2f\n", x, y); } bool operator == (Point b) const { return sgn(x - b.x) == 0 && sgn(y - b.y) == 0; } bool operator < (Point b) const { return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x; } Point operator - (const Point &b) const { return Point(x - b.x, y - b.y); } double operator ^ (const Point &b) const { return x * b.y - y * b.x; } double operator * (const Point &b) const { return x * b.x + y * b.y; } Point operator * (const double &k) const { return Point(x*k, y*k); } Point operator / (const double &k) const { return Point(x / k, y / k); } Point operator + (const Point &b) const { return Point(x + b.x, y + b.y); } double len() { return hypot(x, y); } double len2() { return x * x + y * y; } int Dist(Point p) { return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y); } }; double xmult(Point p, Point a, Point b) { return((a - p) ^ (b - p)); } double dmult(Point p, Point a, Point b) { return((a - p)*(b - p)); } struct Line { Point s, e; Line() {} Line(Point _s, Point _e) { s = _s; e = _e; } Line(double a, double b, double c) { if (sgn(a) == 0) { s = Point(0, -c / b); e = Point(1, -c / b); } else if (sgn(b) == 0) { s = Point(-c / a, 0); e = Point(-c / a, 1); } else { s = Point(0, -c / b); e = Point(1, (-c - a) / b); } } void input() { s.input(); e.input(); } double length() { return s.Dist(e); } bool parallel(Line v) { return sgn((e - s) ^ (v.e - v.s)) == 0; } double dispointtoline(Point p) { return fabs(xmult(s, e, p)) / length(); } Point lineprog(Point p) { return s + (((e - s)*((e - s)*(p - s))) / (e - s).len2()); } Point crosspoint(Line v) { double a1 = (v.e - v.s) ^ (s - v.s); double a2 = (v.e - v.s) ^ (e - v.s); return Point((s.x*a2 - e.x*a1) / (a2 - a1), (s.y*a2 - e.y*a1) / (a2 - a1)); } bool pointonseg(Point p) { return sgn((p - s) ^ (e - s)) == 0 && sgn((p - s)*(p - e)) <= 0; } bool SegmentProperIntrrsection(Line b) { int c1 = sgn(xmult(s, e, b.s)), c2 = sgn(xmult(s, e, b.e)); int c3 = sgn(xmult(b.s, b.e, s)), c4 = sgn(xmult(b.s, b.e, e)); if ((c1*c2) < 0 && (c3*c4) < 0) return 1; return 0; } bool UnSegmentProperIntrrsection(Line b) { if (sgn(max(s.x, e.x) - min(b.s.x, b.e.x)) >= 0 && sgn(max(b.s.x, b.e.x) - min(s.x, e.x)) >= 0 && sgn(max(s.y, e.y) - min(b.s.y, b.e.y)) >= 0 && sgn(max(b.s.y, b.e.y) - min(s.y, e.y)) >= 0 && sgn(xmult(s, b.e, b.s))*sgn(xmult(e, b.e, b.s)) <= 0 && sgn(xmult(b.s, s, e))*sgn(xmult(b.e, s, e)) <= 0) return 1; return 0; } Point NearestPointToLineSeg(Point a) { Point ans; double t = (a - e)*(s - e) / ((s - e)*(s - e)); if (t >= 0 && t <= 1) { ans.x = e.x + (s.x - e.x)*t; ans.y = e.y + (s.y - e.y)*t; } else { if (a.Dist(e) > a.Dist(s)) ans = s; else ans = e; } return ans; } Point jiaodian(Line b) { Point ans = s; double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e)); ans.x += (e.x - s.x)*t; ans.y += (e.y - s.y)*t; return ans; } };
struct polygon { int n; Point p[maxn]; Line l[maxn]; void getline() { for (int i = 0; i < n; i++) l[i] = Line(p[i], p[(i + 1) % n]); } void input(int _n) { n = _n; for (int i = 0; i < n; i++) p[i].input(); } bool isconvex() { bool vis[4]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { vis[sgn(xmult(p[i], p[(i + 1) % n], p[(i + 2) % n])) + 1] = 1; if (vis[0] && vis[2]) return 0; } return 1; } int relationpoint(Point q) { for (int i = 0; i < n; i++) { if (p[i] == q)return 3; } getline(); for (int i = 0; i < n; i++) { if (l[i].pointonseg(q))return 2; } int cnt = 0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; int k = sgn((q - p[j]) ^ (p[i] - p[j])); int u = sgn(p[i].y - q.y); int v = sgn(p[j].y - q.y); if (k > 0 && u < 0 && v >= 0)cnt++; if (k < 0 && v < 0 && u >= 0)cnt--; } return cnt != 0; } bool judge() { double sum = 0; for (int i = 1; i < n; i++) { sum += xmult(p[0], p[i], p[(i + 1) % n]); } if (sum < 0) return 1; return 0; } polygon Graham() { polygon ans; int m = 0; sort(p, p + n); for (int i = 0; i < n; i++) { while (m > 1 && sgn(xmult(ans.p[m - 2], ans.p[m - 1], p[i]) <= 0)) m--; ans.p[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && sgn(xmult(ans.p[m - 2], ans.p[m - 1], p[i]) <= 0)) m--; ans.p[m++] = p[i]; }
if (n > 1) m--; ans.n = m; return ans; } int Rotating() { if (n == 1) return 0; if (n == 2) return p[0].Dist(p[1]); int j = 2; int ans = 0; for (int i = 0; i < n; i++) { while (xmult(p[i], p[(i + 1) % n], p[j]) < xmult(p[i], p[(i + 1) % n], p[(j + 1) % n])) j = (j + 1) % n; ans = max(ans, max(p[i].Dist(p[j]), p[(i + 1) % n].Dist(p[j]))); } return ans; } };
struct halfplane : public Line { double angle; halfplane() {} halfplane(Point _s, Point _e) { s = _s; e = _e; } halfplane(Line v) { s = v.s; e = v.e; } void output() { printf("s: (%f,%f)\n", s.x, s.y); printf("e: (%f,%f)\n", e.x, e.y); } void calcangle() { angle = atan2(e.y - s.y, e.x - s.x); } bool operator < (const halfplane &b)const { return angle < b.angle; }
}; struct halfplanes { int n; halfplane hp[maxn]; Point p[maxn]; int que[maxn]; int f, l; void push(halfplane tmp) { hp[n++] = tmp; } void unique() { int m = 1; for (int i = 1; i < n; i++) { if (sgn(hp[i].angle - hp[i - 1].angle) != 0) hp[m++] = hp[i]; else if (sgn(xmult(hp[m - 1].s, hp[m - 1].e, hp[i].s)) > 0) hp[m - 1] = hp[i]; } n = m; } bool halfplaneinsert() { for (int i = 0; i < n; ++i) hp[i].calcangle(); sort(hp, hp + n); unique(); que[f = 0] = 0; que[l = 1] = 1; p[1] = hp[0].crosspoint(hp[1]); for (int i = 2; i < n; i++) { while (f < l && sgn(xmult(hp[i].s, hp[i].e, p[l])) < 0)l--; while (f < l && sgn(xmult(hp[i].s, hp[i].e, p[f + 1])) < 0)f++; que[++l] = i; if (hp[i].parallel(hp[que[l - 1]])) return false; p[l] = hp[i].crosspoint(hp[que[l - 1]]); } while (f < l && sgn(xmult(hp[que[f]].s, hp[que[f]].e, p[l])) < 0)l--; while (f < l && sgn(xmult(hp[que[l]].s, hp[que[l]].e, p[f + 1])) < 0)f++; if (f + 1 >= l) return false; return true; } void getconvex(polygon &ans) { p[f] = hp[que[f]].crosspoint(hp[que[l]]); ans.n = l - f + 1; for (int j = f, i = 0; j <= l; ++i, ++j) ans.p[i] = p[j]; } }; polygon P, ans; int main() { int n; while (scanf("%d", &n) != EOF) { P.input(n); ans = P.Graham(); int ans1 = ans.Rotating(); printf("%d\n", ans1); }
|