Cod sursă (job #307379)

Utilizator avatar Men_in_Black Marco Polo Men_in_Black IP ascuns
Problemă Înfășurătoare convexă Compilator cpp | 1,37 kb
Rundă Arhiva de probleme Status evaluat
Dată 17 iun. 2017 11:02:08 Scor 100

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#define err 1e-12

using namespace std;

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

const int NMAX = 120000;

struct Point{
  int x;
  int y;

  bool operator<(Point other) const{
    if(y != other.y)
      return (y < other.y);
    else
      return (x < other.x);
  }
};

int n, minn;
int st[1 + NMAX], last;
Point v[1 + NMAX];

int det3(Point a, Point b, Point c) {
  long long answer = 1LL * a.x * (b.y - c.y) + 1LL * b.x * (c.y - a.y) + 1LL * c.x * (a.y - b.y);
  if (answer == 0)
	return 0;
  else if (0 < answer)
	return 1;
  else
	return -1;
}

bool cmp(Point b, Point c){
  if(det3(v[1], b, c) != 0)
    return (det3(v[1], b, c) > 0);
  else
    return (b < c);
}

int main()
{
  in >> n;
  minn = 1;
  for(int i = 1; i <= n; i++){
    in >> v[i].x >> v[i].y;
    if(v[i] < v[minn])
      minn = i;
  }
  in.close();
  swap(v[1], v[minn]);
  sort(v + 2, v + n + 1, cmp);

  st[1] = 1;
  st[2] = 2;
  last = 2;

  for(int i = 3; i <= n; i++){
    while(last >= 2 && det3(v[st[last - 1]], v[st[last]], v[i]) <= 0)
      last--;
    st[++last] = i;
  }

  out << last << '\n';

  for(int i = 1; i <= last; i++){
    out << v[st[i]].x << ' ' << v[st[i]].y << '\n';
  }
  out.close();
  return 0;
}