Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- using namespace std;
- struct point {
- double x, y;
- point operator-(point a) {
- return {x - a.x, y - a.y};
- }
- double operator^(point a) {
- return x * a.y - y * a.x;
- }
- };
- double perimeter(vector<point> & points) {
- point p0 = points[0];
- for (point p : points)
- if (p.x < p0.x || (p.x == p0.x && p.y < p0.y))
- p0 = p;
- for (point &x : points) {
- x.x -= p0.x;
- x.y -= p0.y;
- }
- sort(points.begin(), points.end(), [&](point a, point b){
- return (a ^ b) > 0 || (a ^ b) == 0 && a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y;
- });
- vector<point> hull;
- for (point p : points) {
- while (hull.size() >= 2 && (((p - hull.back()) ^ (hull[hull.size() - 2] - hull.back())) <= 0)) {
- hull.pop_back();
- }
- hull.push_back(p);
- }
- for (point &x : hull) {
- x.x += p0.x;
- x.y += p0.y;
- }
- double sum = 0;
- for (int i = 1; i < hull.size(); i++) {
- sum += sqrt((hull[i - 1].x - hull[i].x)*(hull[i - 1].x - hull[i].x) +
- (hull[i - 1].y - hull[i].y)*(hull[i - 1].y - hull[i].y));
- }
- sum += sqrt((hull[hull.size() - 1].x - hull[0].x)*(hull[hull.size() - 1].x - hull[0].x) +
- (hull[hull.size() - 1].y - hull[0].y)*(hull[hull.size() - 1].y - hull[0].y));
- return sum;
- }
- int main()
- {
- int N;
- cin >> N;
- vector <point> points (N);
- for (int i = 0; i < N; i++)
- cin >> points[i].x >> points[i].y;
- double sum = perimeter(points);
- printf("%.2f", sum);
- return 0;
- }
Add Comment
Please, Sign In to add comment