Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #define _CRT_SECURE_NO_WARNINGS
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <queue>
- #include <assert.h>
- using namespace std;
- struct point
- {
- double x = 0;
- double y = 0;
- double distance() const;
- double angle() const;
- };
- point center;
- double point::distance() const { return (x - center.x)*(x - center.x) + (y - center.y)*(y - center.y); };
- double point::angle() const { return atan2(x - center.x, y - center.y); };
- bool operator<(const point&A, const point& B)
- {
- if (A.angle() + 1e-10 < B.angle()) return true;
- if (A.angle() - 1e-10 > B.angle()) return false;
- return A.distance() < B.distance();
- }
- bool operator==(const point&A, const point& B)
- {
- return A.x == B.x && A.y == B.y;
- }
- bool cw(point& a, point& b, point& c) {
- return a.x*(b.y - c.y) + b.x*(c.y - a.y) + c.x*(a.y - b.y) < 1e-10;
- }
- double convex_hull(vector<point> &D, int s, vector<point> &hull)
- {
- for (size_t j = 0; j < D.size(); j++)
- {
- int i = (j + s) % D.size();
- while (hull.size() > 1 && !cw(hull[hull.size() - 2], hull[hull.size() - 1], D[i]))
- hull.pop_back();
- hull.push_back(D[i]);
- }
- double sum = 0.0;
- for (size_t i = 0; i < hull.size(); i++)
- {
- point &v1 = hull[i];
- // cout << fixed << setprecision(2) << v1.x << " " << v1.y << "\n";
- point &v2 = hull[(i + 1) % hull.size()];
- double distance = sqrt((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
- sum += distance;
- }
- // cout << sum + 2.0 << "\n\n";
- return sum;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- //freopen("test.txt", "r", stdin);
- //freopen("reagents.out", "w", stdout);
- int t;
- cin >> t;
- while (t--)
- {
- center.x = 0;
- center.y = 0;
- int n;
- cin >> n;
- vector<point> D1(n);
- for (int i = 0; i < n; i++)
- {
- cin >> D1[i].x >> D1[i].y;
- center.x += D1[i].x;
- center.y += D1[i].y;
- }
- center.x /= n + 1;
- center.y /= n + 1;
- vector<point> D(D1);
- point C;
- D.push_back(C);
- sort(D.begin(), D.end());
- int best = 0;
- for (size_t i = 0; i < D.size(); i++)
- if (D[best].distance() < D[i].distance()) best = i;
- vector<point> hull;
- hull.push_back(D[best]);
- double best_length = convex_hull(D, best + 1, hull);
- if (find(hull.begin(), hull.end(), C) == hull.end())
- {
- best_length = 1e30;
- center.x = 0;
- center.y = 0;
- sort(D1.begin(), D1.end());
- for (size_t i = 0; i < D.size(); i++)
- {
- hull.clear();
- hull.push_back(C);
- best_length = min(best_length, convex_hull(D1, i, hull));
- }
- }
- cout << fixed << setprecision(2) << best_length + 2.0 << "\n";
- if (t != 0) cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement