Pagini recente » Cod sursa (job #57043) | Cod sursa (job #1058164) | Cod sursa (job #1037487) | Cod sursa (job #336687) | Cod sursa (job #2040828)
#include<cstdio>
#include<algorithm>
#define MAX_N 120000
using namespace std;
struct point {
double x, y;
};
point v[MAX_N], st[MAX_N];
int n, head;
void readPoints() {
FILE *fin = fopen("infasuratoare.in","r");
fscanf(fin,"%d",&n);
for(int i=0; i<n; i++)
fscanf(fin,"%lf%lf",&v[i].x,&v[i].y);
fclose(fin);
}
inline double det(point a, point b, point c) {
return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
}
inline int cmp(point a, point b) {
return det(v[0],a,b) > 0;
}
void sortPoints() {
int minIndex = 0;
for(int i=1; i<n; i++)
if(v[i].y < v[minIndex].y)
minIndex = i;
else if(v[i].y == v[minIndex].y && v[i].x < v[minIndex].x)
minIndex = i;
swap(v[0],v[minIndex]);
sort(v+1,v+n,cmp);
}
void convexHull() {
sortPoints();
st[0] = v[0];
st[1] = v[1];
head = 2;
for(int i=2; i<n; i++) {
while(head >= 2 && det(st[head-2],st[head-1],v[i]) < 0)
head--;
st[head++] = v[i];
}
}
void printPoints() {
FILE *fout = fopen("infasuratoare.out","w");
fprintf(fout,"%d\n",head);
for(int i=0; i<head; i++)
fprintf(fout,"%.6f %.6f\n",st[i].x,st[i].y);
fclose(fout);
}
int main() {
readPoints();
convexHull();
printPoints();
return 0;
}