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
| #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <algorithm> using namespace std;
struct Point { int x, y; bool operator < (const Point p)const { return x < p.x || x == p.x y < p.y; } };
double Det(double fX1, double fY1, double fX2, double fY2) { return fX1 * fY2 - fX2 * fY1; }
double Cross(Point a, Point b, Point c) { return Det(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y); }
void Convex(vector<Point> in, vector<Point> out) { int nN = in.size(); int nM = 0;
sort(in.begin(), in.end()); out.resize(nN);
for (int i = 0; i < nN; ++i) { while (nM >= 2 Cross(out[nM - 2], out[nM - 1], in[i]) <= 0) { nM--; } out[nM++] = in[i]; }
for (int i = nN - 2, t = nM + 1; i >= 0; --i) { while (nM >= t Cross(out[nM - 2], out[nM - 1], in[i]) <= 0) { nM--; } out[nM++] = in[i]; } out.resize(nM); out.pop_back(); }
int SDis(Point a,Point b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); }
int RC(vector<Point> vp) { int nP = 1; int nN = vp.size(); vp.push_back(vp[0]); int nAns = 0; for (int i = 0; i < nN; ++i) { while (Cross(vp[i], vp[i + 1], vp[nP + 1]) > Cross(vp[i], vp[i + 1], vp[nP])) { nP = (nP + 1) % nN; } nAns = max(nAns, max(SDis(vp[i], vp[nP]), SDis(vp[i + 1], vp[nP + 1]))); } vp.pop_back(); return nAns; }
int main() { int nN; vector<Point> in, out; Point p; while (scanf("%d", &nN) == 1) { in.clear(), out.clear(); while (nN--) { scanf("%d%d", p.x, p.y); in.push_back(p); } Convex(in, out);
printf("%d\n", RC(out)); }
return 0; }
|