Cod sursa(job #2270211)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 27 octombrie 2018 10:08:22
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.42 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");

const ld EPS = 1e-16;

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

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 (abs (d) < EPS) 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 = 1;
  for (int i = 1; i <= n; ++ i) {

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

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

  int ind = 1;
  while (ind < sz (points)) {
    if (points[ind].x == points[best].x and points[ind].y == points[best].y) {
      swap (points[ind], points[sz (points) - 1]);
      points.pop_back ();
    }
    else {
      ++ ind;
    }
  }

  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;
  });

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

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

    while (ind >= 2 and 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';
  }
}