JuliaMelkozerova

HW4E

Apr 7th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. struct point {
  10.     double x, y;
  11.     point operator-(point a) {
  12.         return {x - a.x, y - a.y};
  13.     }
  14.     double operator^(point a) {
  15.         return x * a.y - y * a.x;
  16.     }
  17. };
  18.  
  19. double perimeter(vector<point> & points) {
  20.     point p0 = points[0];
  21.     for (point p : points)
  22.         if (p.x < p0.x || (p.x == p0.x && p.y < p0.y))
  23.             p0 = p;
  24.  
  25.     for (point &x : points) {
  26.         x.x -= p0.x;
  27.         x.y -= p0.y;
  28.     }
  29.  
  30.     sort(points.begin(), points.end(), [&](point a, point b){
  31.         return (a ^ b) > 0 || (a ^ b) == 0 && a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y;
  32.     });
  33.  
  34.     vector<point> hull;
  35.     for (point p : points) {
  36.         while (hull.size() >= 2 && (((p - hull.back()) ^ (hull[hull.size() - 2] - hull.back())) <= 0)) {
  37.              hull.pop_back();
  38.         }
  39.         hull.push_back(p);
  40.     }
  41.    
  42.     for (point &x : hull) {
  43.         x.x += p0.x;
  44.         x.y += p0.y;
  45.     }
  46.    
  47.     double sum = 0;
  48.     for (int i = 1; i < hull.size(); i++) {
  49.         sum += sqrt((hull[i - 1].x - hull[i].x)*(hull[i - 1].x - hull[i].x) +
  50.                     (hull[i - 1].y - hull[i].y)*(hull[i - 1].y - hull[i].y));
  51.     }
  52.     sum += sqrt((hull[hull.size() - 1].x - hull[0].x)*(hull[hull.size() - 1].x - hull[0].x) +
  53.                 (hull[hull.size() - 1].y - hull[0].y)*(hull[hull.size() - 1].y - hull[0].y));
  54.    
  55.     return sum;
  56. }
  57.  
  58.  
  59. int main()
  60. {
  61.     int N;
  62.     cin >> N;
  63.     vector <point> points (N);
  64.     for (int i = 0; i < N; i++)
  65.         cin >> points[i].x >> points[i].y;
  66.    
  67.     double sum = perimeter(points);
  68.     printf("%.2f", sum);
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment