Problem A
Useless Tile Packers
Input: standard input
Output: standard output
Yes, as you have apprehended the Useless Tile Packers (UTP) pack tiles. Thetiles are of uniform thickness and have simple polygonal shape. For each tile acontainer is custom-built. The floor of the container is a convex polygon and underthis constraint it has the minimum possible space inside to hold the tile it isbuilt for. But this strategy leads to wasted space inside the container.
The UTPauthorities are interested to know the percentage of wasted space for a giventile.
Input
The input file consists ofseveral data blocks. Each data block describes one tile.
The first lineof a data block contains an integer N (3 <= N <= 100) indicating the number of corner points of the tile. Each of the nextN lines contains two integers giving the(x, y) co-ordinates of a corner point (determined using a suitable originand orientation of the axes) where 0 <= x, ,y <= 1000. Starting from the first point given in the input the corner points occurin the same order on the boundary of the tile as they appear in the input. Nothree consecutive points are co-linear.
The input fileterminates with a value of 0 for N.
Output
For each tile in the input outputthe percentage of wasted space rounded to two digits after the decimal point.Each output must be on a separate line. Print a blank line after each output block.
Sample Input
50 0
2 0
2 2
1 1
0 2
5
0 0
0 2
1 3
2 2
2 0
0
Sample Output
Tile #1Wasted Space = 25.00 %
Tile #2
Wasted Space = 0.00 %
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
struct point
{double x;double y;
point(double x=0, double y=0):x(x),y(y){}
}p[100005],a,ch[100005];
typedef point vector;
vector operator-(point a, point b){return vector(a.x-b.x,a.y-b.y);}
double cross(vector a, vector b){return a.x*b.y-a.y*b.x;}
bool operator<(const point& a,const point& b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
int convexhull(point *p,int n,point *ch)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++)
{
while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)
m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)
m--;
ch[m++]=p[i];
}
if(n>1) m--;
return m;
}
double area(point *p, int N)
{
double total=0.0;
for (int i=0;i<N; i++)
{
int j=(i+1)%N;
total+=(p[i].x*p[j].y-p[j].x*p[i].y);
}
return fabs(total/2.0);
}
int main()
{
int n,t,count=1;
while(cin>>n)
{
if(!n) break;
for(int i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
double a1=area(p,n);
int m=convexhull(p,n,ch);
double a2=area(ch,m);
cout<<"Tile #"<<count++<<endl<<"Wasted Space = ";
printf("%.2f %%\n\n",(a2-a1)/a2*100);
}
return 0;
}