Cod sursa(job #2270174)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 27 octombrie 2018 09:50:20
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.19 kb
// By Stefan Radu

#include <algorithm>
/* #include <iostream> */
#include <fstream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <string>
#include <cctype>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

using namespace std;

#define sz(x) (int)(x).size ()

typedef pair < int, int > pii;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

ifstream cin ("infasuratoare.in");
ofstream cout ("infasuratoare.out");

struct Point {
  ld x, y;
  void operator = (Point oth) { this -> x = oth.x; this -> y = oth.y;}
  bool operator < (Point oth) { return this -> y == oth.y ? this -> x < oth.x : this -> y < oth.y;}
};

const ld EPS = 1e-12;

int Det (Point a, Point b, Point c) {

  ld d = a.x * b.y + b.x * c.y + c.x * a.y - a.y * b.x - b.y * c.x - c.y * a.x;

  if (d < 0) return -1;
  else if (d == 0) return 0;
  return 1;
}

int main () {

  ios::sync_with_stdio (false);
  cin.tie (0);cout.tie (0);

  freopen ("input", "r", stdin);
  freopen ("output", "w", stdout);

  cout << fixed << setprecision (12);

  int n;
  cin >> n;

  vector < Point > points (n + 1);

  int best = 0;
  for (int i = 1; i <= n; ++ i) {

    cin >> points[i].x >> points[i].y;

    if (not best) best = i;
    if (points[i] < points[best]) {
      best = i;
    }
  }

  points[0] = points[best];
  swap (points[n], points[best]);
  points.pop_back ();

  sort (points.begin () + 1, points.end (), [&] (Point a, Point b) {

      ld c1 = a.x - points[0].x, i1 = sqrt (c1 * c1 + (a.y - points[0].y) * (a.y - points[0].y)),
      c2 = b.x - points[0].x, i2 = sqrt (c2 * c2 + (b.y - points[0].y) * (b.y - points[0].y));

      ld co1 = c1 / i1, co2 = c2 / i2;
      
      if (abs (co1 - co2) < EPS) {
        return i1 < i2;
      }
      
      return co1 > co2;
  });

  int ind = 0;
  vector < int > stk (n);
  stk[++ ind] = 0;
  stk[++ ind] = 1;
  stk[++ ind] = 2;

  for (int i = 3; i < n; ++ i) {

    while (Det (points[stk[ind - 1]], points[stk[ind]], points[i]) <= 0) {
      -- ind;
    }
    stk[++ ind] = i;
  }

  cout << ind << '\n';
  for (int i = 1; i <= ind; ++ i) {
    cout << points[stk[i]].x << ' ' << points[stk[i]].y << '\n';
  }
}