Cod sursa(job #1895655)

Utilizator oanaroscaOana Rosca oanarosca Data 28 februarie 2017 09:30:17
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <algorithm>
#include <iomanip>

using namespace std;

struct punct {
  double x, y;
};

bool cmp (punct a, punct b) {
  if (a.x < b.x or (a.x == b.x and a.y < b.y))
    return true;
  return false;
}

int semn(punct a,punct b, punct c){
  if((a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y)>=0)
    return 1;
  return 0;
}

punct a[120001], st[120001];
int n, sf, i;

int main () {
  ifstream fi("infasuratoare.in");
  ofstream fo("infasuratoare.out");
  fi >> n;
  for (i = 1; i <= n; i++)
    fi >> a[i].x >> a[i].y;
  sort (a+1, a+n+1, cmp);
  st[1] = a[1]; st[2] = a[2]; sf = 2;
  for (i = 3; i <= n; i++) {
    while (sf > 1 and !semn(st[sf-1], st[sf], a[i]))
      sf--;
    st[++sf] = a[i];
  }
  for (i = n-1; i >= 1; i--) {
    while (!semn(st[sf-1], st[sf], a[i]))
      sf--;
    st[++sf] = a[i];
  }
  fo << fixed << setprecision(6);
  fo << sf-1 << '\n';
  for (i = 1; i <= sf-1; i++)
    fo << st[i].x << ' ' << st[i].y << '\n';
  return 0;
}