Cod sursa(job #2270433)

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

struct Point {
  ld x, y;
  bool operator < (Point other) {
    if (this -> y == other.y) {
      return this -> x < other.x;
    }
    return this -> y < other.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 (d > 1) return 1;
  return 0;
}

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

int main () {

  int n;
  cin >> n;

  vector < Point > points (n);

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

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

    if (points[i] < points[0]) {
      swap (points[i], points[0]);
    }
  }

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

      int det = Det (points[0], a, b);

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

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

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

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

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