Cod sursa(job #2270382)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 27 octombrie 2018 10:46:45
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 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;
};

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 + 1);

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

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

    if (points[i].y < points[1].y or (points[i].y == points[1].y and points[i].x < points[1].x)) {
      swap (points[i], points[1]);
    }
  }

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

      ld det = Det (points[1], a, b);

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

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

  for (int i = 3; 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 << fixed << setprecision (12) << points[stk[i]].x << ' ' << points[stk[i]].y << '\n';
  }
}