Advertisement
ivnikkk

Untitled

Jul 23rd, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. struct Point {
  2.     x: int, y: int,
  3.    
  4.     function subtr(self: Point, other: Point) -> Point {
  5.         return new Point{x: self.x - other.x, y: self.y - other.y};
  6.     }
  7.    
  8.     function add(self: Point, other: Point) -> Point {
  9.         return new Point{x: self.x + other.x, y: self.y + other.y};
  10.     }
  11.  
  12.     function equally(self: Point, other: Point) {
  13.         self.x = other.x;
  14.         self.y = other.y;
  15.     }
  16.    
  17.     function len2(self: Point) -> int {
  18.         return self.x * self.x + self.y * self.y  
  19.     }
  20.    
  21.     function vecpr(self: Point, other: Point) -> int {
  22.         return self.x * other.y - self.y * other.x;  
  23.     }
  24.    
  25.     function less(self: Point, other: Point) -> bool {
  26.         let x: int = self.vecpr(other);
  27.         if(x != 0) {
  28.             return x > 0;  
  29.         }
  30.         else {
  31.             let indef: int = other.len2() - self.len2();
  32.             return  indef > 0;
  33.         }
  34.     }
  35. }
  36.  
  37. function get_dist(a: Point, b: Point) -> float {
  38.     let x: int = a.x - b.x;
  39.     let y: int = a.y - b.y;
  40.     return sqrt(float(x * x + y * y));
  41. }
  42.  
  43. function round_f(x: float) -> string {  
  44.     x *= float(100);
  45.     let x_t = int(x + 0.5);
  46.     let str = string(x_t % 100);
  47.     while(str.length() < 2) {
  48.         str = "0" + str;  
  49.     }
  50.     return string(x_t / 100) + "." + str;
  51. }
  52. function solve(input: string) -> string {
  53.     let line = input.split("\n"); let curline: int = 0;
  54.     let t: int = int(line[curline]); curline += 1;
  55.     let ans: string[] = [];
  56.    
  57.     for(let num = 1; num <= t; num += 1) {
  58.         let arr: Point[] = [];
  59.         let n: int = int(line[curline]); curline += 1;
  60.        
  61.         for(let j = 0; j < n; j += 1) {
  62.             let inp = line[curline].split(" "); curline += 1;
  63.             let x1: int = int(inp[0]);
  64.             let y1: int = int(inp[1]);
  65.             arr.push(new Point{x: x1, y: y1});
  66.         }
  67.         let lft = new Point{x: arr[0].x, y: arr[0].y};
  68.         let ind_left: int = 0;
  69.         for(let j = 0; j < n; j += 1) {
  70.             if(lft.x > arr[j].x) {
  71.                 lft.x = arr[j].x;
  72.                 lft.y = arr[j].y;
  73.                 ind_left = j;
  74.             }
  75.             if(lft.x == arr[j].x && lft.y > arr[j].y) {
  76.                 lft.x = arr[j].x;
  77.                 lft.y = arr[j].y;
  78.                 ind_left = j;
  79.             }
  80.         }
  81.        
  82.         let hull: Point[] = [];
  83.         hull.push(lft);
  84.      
  85.        
  86.         let to_swap: Point = new Point{x: arr[n - 1].x, y: arr[n - 1].y};
  87.         arr[n - 1] = arr[ind_left];
  88.         arr[ind_left] = to_swap;
  89.         arr.pop();
  90.         n -= 1;
  91.        
  92.         for(let i = 0; i < n; i += 1) {
  93.             arr[i] = arr[i].subtr(hull[0]);
  94.         }
  95.         arr.sort();
  96.        
  97.         for(let i = 0; i < n; i += 1) {
  98.             arr[i] = arr[i].add(hull[0]);  
  99.         }
  100.        
  101.         for(let i = 0; i < n; i += 1) {
  102.             while(hull.length() >= 2) {
  103.                 let lst: int = hull.length() - 1;  
  104.                 let v1: Point = hull[lst].subtr(hull[lst - 1]);
  105.                 let v2: Point = arr[i].subtr(hull[lst]);
  106.                
  107.                 if(v1.vecpr(v2) > 0) {
  108.                     break;  
  109.                 }
  110.                 hull.pop();
  111.             }
  112.             hull.push(arr[i]);
  113.         }
  114.        
  115.         let res: float = 0.0000;
  116.         n = hull.length();
  117.         hull.push(hull[0]);
  118.         for(let i = 0; i < n; i += 1) {
  119.             res += get_dist(hull[i], hull[i + 1]);
  120.         }
  121.         ans.push(round_f(res));
  122.     }
  123.     return ans.join("\n");
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement