#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct Point
{
int x,y;
}Point;
int direction(Point p0,Point p1,Point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int distSquared(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int comp(Point p0,Point a, Point b)
{
int d=direction(p0,a,b);
if(d!=0) return (d>0);
else return distSquared(p0,a) > distSquared(p0,b);
}
void heapify(int l,int r, Point *P)
{
int i,j;
Point t;
int isHeap;
t=P[l];
i=l;
j=2*i;
isHeap=0;
while(j<=r && !isHeap)
{
if(j<r && comp(P[0],P[j],P[j+1]))
j++;
if(comp(P[0],t,P[j]))
{
P[i]=P[j];
i=j;
j=2*i;
}
else
isHeap=1;
}
P[i]=t;
}
void buildHeap(int n,Point *P)
{
int i;
for(i=n/2;i>=1;i--)
heapify(i,n,P);
}
void heapSort(int n,Point *P)
{
int i;
Point t;
buildHeap(n,P);
for(i=n;i>=2;i--)
{
t=P[1];
P[1]=P[i];
P[i]=t;
heapify(1,i-1,P);
}
}
int removeDuplicates(int size, Point *P)
{
int i,prev,count;
prev=1;
for(i=1;i<size;i++)
{
if(direction(P[0],P[i],P[prev])!=0)
{
prev++;
P[prev]=P[i];
}
}
count=prev+1;
return count;
}
void Solve(int n,Point *P, int *top, Point *S)
{
int i,k,min,m;
Point t;
min=0;
for(i=1;i<n;i++)
if(P[i].y<P[min].y ||(P[i].y==P[min].y && P[i].x <P[min].x))
min=i;
t=P[0];
P[0]=P[min];
P[min]=t;
heapSort(n-1,P);
m=removeDuplicates(n,P);
k=-1;
i=0;
while(i<n && i<3)
{
k++;
S[k]=P[i];
i++;
}
for(i=3;i<m;i++)
{
while(k>=1 && direction(S[k-1],S[k],P[i])<=0)
k--;
k++;
S[k]=P[i];
}
k++;
S[k]=P[0];
(*top)=k;
}
int main()
{
int j,k,n;
Point *a;
Point *b;
FILE *in;
FILE *out;
if ((in
= fopen("inputg.txt", "rt")) == NULL)
{
fprintf(stderr
, "Cannot open input file.\n"); return 1;
}
if ((out
= fopen("outputg.txt", "wt")) == NULL)
{
fprintf(stderr
, "Cannot open output file.\n"); return 1;
}
{
a
=(Point
*)malloc(n
*sizeof(Point
)); b
=(Point
*)malloc(n
*sizeof(Point
)); for(j=0;j<n;j++)
fscanf(in
,"%d %d",&a
[j
].
x,&a
[j
].
y); Solve(n,a,&k,b);
for(j=0;j<k;j++)
printf("%d %d \n",b
[j
].
x,b
[j
].
y); for(j=0;j<k;j++)
fprintf(out
,"%d %d ",b
[j
].
x,b
[j
].
y); }
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPGNvbmlvLmg+Cgp0eXBlZGVmIHN0cnVjdCBQb2ludAp7CiAgaW50IHgseTsgICAgICAgCn1Qb2ludDsKCmludCBkaXJlY3Rpb24oUG9pbnQgcDAsUG9pbnQgcDEsUG9pbnQgcDIpCnsKICByZXR1cm4gKHAxLngtcDAueCkqKHAyLnktcDAueSktKHAyLngtcDAueCkqKHAxLnktcDAueSk7Cn0KCmludCBkaXN0U3F1YXJlZChQb2ludCBhLFBvaW50IGIpCnsKICByZXR1cm4gKGEueC1iLngpKihhLngtYi54KSsoYS55LWIueSkqKGEueS1iLnkpOwp9CgppbnQgY29tcChQb2ludCBwMCxQb2ludCBhLCBQb2ludCBiKQp7CiAgaW50IGQ9ZGlyZWN0aW9uKHAwLGEsYik7CiAgaWYoZCE9MCkgcmV0dXJuIChkPjApOwogIGVsc2UgcmV0dXJuIGRpc3RTcXVhcmVkKHAwLGEpID4gZGlzdFNxdWFyZWQocDAsYik7IAp9Cgp2b2lkIGhlYXBpZnkoaW50IGwsaW50IHIsIFBvaW50ICpQKQp7CiAgICAgaW50IGksajsKICAgICBQb2ludCB0OwogICAgIGludCBpc0hlYXA7CiAgICAgdD1QW2xdOwogICAgIGk9bDsKICAgICBqPTIqaTsKICAgICBpc0hlYXA9MDsKICAgICB3aGlsZShqPD1yICYmICFpc0hlYXApCiAgICAgewogICAgICAgaWYoajxyICYmIGNvbXAoUFswXSxQW2pdLFBbaisxXSkpIAogICAgICAgICAgaisrOwogICAgICAgaWYoY29tcChQWzBdLHQsUFtqXSkpCiAgICAgICB7CiAgICAgICAgICAgIFBbaV09UFtqXTsKICAgICAgICAgICAgaT1qOwogICAgICAgICAgICBqPTIqaTsKICAgICAgIH0KICAgICAgIGVsc2UgCiAgICAgICAgICAgIGlzSGVhcD0xOwogICAgIH0KICAgICBQW2ldPXQ7Cn0KCnZvaWQgYnVpbGRIZWFwKGludCBuLFBvaW50ICpQKQp7CmludCBpOwogICBmb3IoaT1uLzI7aT49MTtpLS0pCiAgICAgIGhlYXBpZnkoaSxuLFApOwp9Cgp2b2lkIGhlYXBTb3J0KGludCBuLFBvaW50ICpQKQp7CmludCBpOwpQb2ludCB0OwogICAgYnVpbGRIZWFwKG4sUCk7IAogICAgZm9yKGk9bjtpPj0yO2ktLSkKICAgIHsKICAgICAgICB0PVBbMV07CiAgICAgICAgUFsxXT1QW2ldOwogICAgICAgIFBbaV09dDsKICAgICAgICBoZWFwaWZ5KDEsaS0xLFApOwogICAgfSAgICAgICAgICAgICAgICAgCn0KCmludCByZW1vdmVEdXBsaWNhdGVzKGludCBzaXplLCBQb2ludCAqUCkKewppbnQgaSxwcmV2LGNvdW50OwogICAgcHJldj0xOwogICAgZm9yKGk9MTtpPHNpemU7aSsrKQogICAgewogICAgICAgaWYoZGlyZWN0aW9uKFBbMF0sUFtpXSxQW3ByZXZdKSE9MCkKICAgICAgIHsKICAgICAgICAgICBwcmV2Kys7CiAgICAgICAgICAgUFtwcmV2XT1QW2ldOwogICAgICAgfQogICAgfQogICAgY291bnQ9cHJldisxOwpyZXR1cm4gY291bnQ7Cn0KCnZvaWQgU29sdmUoaW50IG4sUG9pbnQgKlAsIGludCAqdG9wLCBQb2ludCAqUykKewogICAgIGludCBpLGssbWluLG07CiAgICAgUG9pbnQgdDsKICAgICBtaW49MDsKICAgICBmb3IoaT0xO2k8bjtpKyspCiAgICAgICAgaWYoUFtpXS55PFBbbWluXS55IHx8KFBbaV0ueT09UFttaW5dLnkgJiYgUFtpXS54IDxQW21pbl0ueCkpCiAgICAgICAgICAgIG1pbj1pOwogICAgIHQ9UFswXTsKICAgICBQWzBdPVBbbWluXTsKICAgICBQW21pbl09dDsKICAgICBoZWFwU29ydChuLTEsUCk7CiAgICAgbT1yZW1vdmVEdXBsaWNhdGVzKG4sUCk7CiAgICAgaz0tMTsKICAgICBpPTA7CiAgICAgd2hpbGUoaTxuICYmIGk8MykKICAgICB7CiAgICAgICAgaysrOwogICAgICAgIFNba109UFtpXTsKICAgICAgICBpKys7CiAgICAgfQogICAgIGZvcihpPTM7aTxtO2krKykKICAgICB7CiAgICAgICAgd2hpbGUoaz49MSAmJiBkaXJlY3Rpb24oU1trLTFdLFNba10sUFtpXSk8PTApCiAgICAgICAgICAgIGstLTsKICAgICAgICBrKys7CiAgICAgICAgU1trXT1QW2ldOyAgICAKICAgICB9CiAgICAgaysrOwogICAgIFNba109UFswXTsKKCp0b3ApPWs7ICAgICAgICAgICAgICAgICAgICAgICAKfQoKaW50IG1haW4oKQp7CiBpbnQgaixrLG47CiBQb2ludCAqYTsKIFBvaW50ICpiOwogRklMRSAqaW47CiBGSUxFICpvdXQ7CiAKIGlmICgoaW4gPSBmb3BlbigiaW5wdXRnLnR4dCIsICJydCIpKQogICAgICAgPT0gTlVMTCkKICAgewogICAgICBmcHJpbnRmKHN0ZGVyciwgIkNhbm5vdCBvcGVuIGlucHV0IGZpbGUuXG4iKTsKICAgICAgcmV0dXJuIDE7CiAgIH0KIGlmICgob3V0ID0gZm9wZW4oIm91dHB1dGcudHh0IiwgInd0IikpCiAgICAgICA9PSBOVUxMKQogICB7CiAgICAgIGZwcmludGYoc3RkZXJyLCAiQ2Fubm90IG9wZW4gb3V0cHV0IGZpbGUuXG4iKTsKICAgICAgcmV0dXJuIDE7CiAgIH0KIHdoaWxlKCFmZW9mKGluKSkKIHsKICAgZnNjYW5mKGluLCIlZCIsJm4pOwogICBhPShQb2ludCAqKW1hbGxvYyhuKnNpemVvZihQb2ludCkpOwogICBiPShQb2ludCAqKW1hbGxvYyhuKnNpemVvZihQb2ludCkpOwogICBmb3Ioaj0wO2o8bjtqKyspCiAgICAgIGZzY2FuZihpbiwiJWQgJWQiLCZhW2pdLngsJmFbal0ueSk7CiAgIFNvbHZlKG4sYSwmayxiKTsKICAgZm9yKGo9MDtqPGs7aisrKQogICAgICBwcmludGYoIiVkICVkIFxuIixiW2pdLngsYltqXS55KTsKICAgcHJpbnRmKCJcbiIpOwogICBmcHJpbnRmKG91dCwiJWRcbiIsayk7CiAgIGZvcihqPTA7ajxrO2orKykKICAgICAgZnByaW50ZihvdXQsIiVkICVkICIsYltqXS54LGJbal0ueSk7CiAgIGZwcmludGYob3V0LCJcbiIpOwogICBmcmVlKGEpOwogICBmcmVlKGIpOwogfQogZmNsb3NlKGluKTsKIGZjbG9zZShvdXQpOwogCiBnZXRjaCgpOwogcmV0dXJuIDA7Cn0K