Cod sursa(job #2285658)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 18 noiembrie 2018 21:23:21
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
// By Stefan Radu

#include <algorithm>
#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 double EPS = 1e-12;

struct Point { 

  double x, y;

  void operator = (Point oth) {
    this -> x = oth.x;
    this -> y = oth.y;
  }
};

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

  double 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 (abs (d) < EPS) {
    return 0;
  }

  return d < 0 ? -1 : 1;
}

double Sq (double k) {
  return k * k;
}

double Dist (Point a, Point b) {
  return Sq (a.x - b.x) + Sq (a.y - b.y);
}

int main () {

  int n;
  cin >> n;

  vector < Point > v (n + 1);
  for (int i = 1; i <= n; ++ i) {
    cin >> v[i].x >> v[i].y;
    if (i > 1) {
      if (v[i].y < v[1].y or (abs (v[i].y - v[1].y) < EPS and v[i].x < v[1].x)) {
        swap (v[i], v[1]);
      }
    }
  }

  sort (v.begin () + 2, v.end (), [&] (Point a, Point b) -> bool {

      if (Det (v[1], a, b) == 0) {
        return Dist (v[1], a) < Dist (v[1], b);
      }

      return Det (v[1], a, b) == 1;
  });

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

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

    while (ind > 2 and Det (v[stk[ind - 1]], v[stk[ind]], v[i]) <= 0) {
      -- ind;
    }

    stk[++ ind] = i;
  }

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