【POJ 3348】Cows
【題目】
Description
Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.
However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.
Input
The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and y separated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).
Output
You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.
Sample Input
4
0 0
0 101
75 0
75 101
Sample Output
151
【分析】
題目大意:給出一個點集,求出凸包,並輸出凸包的面積除以
題解:凸包模板題
求出凸包上所有點,並求出面積即可
【程式碼】
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10005
using namespace std;
int n;
struct point
{
int x,y;
point(){}
point(int x,int y):x(x),y(y){}
point operator+(const point &a) {return point(x+a.x,y+a.y);}
point operator-(const point &a) {return point(x-a.x,y-a.y);}
friend int dot(const point &a,const point &b) {return a.x*b.x+a.y*b.y;}
friend int cross(const point &a,const point &b) {return a.x*b.y-a.y*b.x;}
}a[N],sta[N];
bool comp(const point &p,const point &q)
{
if(p.x!=q.x)
return p.x<q.x;
return p.y<q.y;
}
int work()
{
int i,top=0;
sta[++top]=a[1];
sta[++top]=a[2];
for(i=3;i<=n;++i)
{
while(top>1&&cross(sta[top-1]-a[i],sta[top-1]-sta[top])<0) top--;
sta[++top]=a[i];
}
for(i=n-1;i>=1;--i)
{
while(top>1&&cross(sta[top-1]-a[i],sta[top-1]-sta[top])<0) top--;
sta[++top]=a[i];
}
int ans=0;
sta[top+1]=sta[1];
for(i=1;i<=top;++i)
ans+=cross(sta[i],sta[i+1]);
return ans;
}
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,comp);
int ans=abs(work());
printf("%d",ans/100);
return 0;
}