Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7573 | Accepted: 3440 |
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
给你n棵树,可以用这n棵树围一个圈,然后在圈里面可以养牛,每个牛需要50平方米的空间,问最多可以养多少牛
求出凸包面积/50 即可
#include <iostream> //G++
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define inf 0x3f3f3f3f
#define N 10010
using namespace std;
struct Point
{
int x, y;
int dis;
} pt[N], stack[N],p0;
int top, tot;
int Dis(int x1 ,int y1, int x2, int y2)
{
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int Cmp_PolarAngel(struct Point p1, struct Point p2, struct Point pb)
{
int delta = (p1.x - pb.x) * (p2.y - pb.y) - (p2.x - pb.x) * (p1.y - pb.y);
if(delta < 0) return 1;
if(delta == 0) return 0;
return -1;
}
bool Is_LeftTurn(struct Point p3, struct Point p2, struct Point p1)
{
int type = Cmp_PolarAngel(p3, p1, p2);
if(type < 0) return true;
return false;
}
int cmp(const void *p1, const void *p2)
{
struct Point *a1 = (struct Point*) p1;
struct Point *a2 = (struct Point*) p2;
int type = Cmp_PolarAngel(*a1, *a2, p0);
if(type < 0) return -1;
if(type == 0)
{
if(a1->dis < a2->dis) return -1;
if(a1->dis == a2->dis) return 0;
return 1;
}
return 1;
}
void Hull(int n)
{
int k;
p0.x = inf;
p0.y = inf;
for(int i = 0; i < n; i++)
{
scanf("%d%d", &pt[i].x, &pt[i].y);
if(pt[i].y < p0.y )
{
p0.y = pt[i].y;
p0.x = pt[i].x;
k = i;
}
else if( pt[i].y == p0.y )
{
if(pt[i].x < p0.x)
{
p0.x = pt[i].x;
k = i;
}
}
}
pt[k] = pt[0];
pt[0] = p0;
for(int i = 1; i < n; i++)
{
pt[i].dis = Dis(pt[i].x, pt[i].y, p0.x, p0.y);
}
qsort(pt+1, n-1, sizeof(struct Point), cmp);
tot = 1;
for(int i = 2; i < n; i++)
{
if(Cmp_PolarAngel(pt[i], pt[i-1], p0))
pt[tot++] = pt[i-1];
}
pt[tot++] = pt[n-1];
top = 1;
stack[0] = pt[0];
stack[1] = pt[1];
for(int i = 2; i < tot; i++)
{
while(top >= 1 && Is_LeftTurn(pt[i], stack[top], stack[top - 1]) == false )
{
top--;
}
stack[++top] = pt[i];
}
}
void Area()
{
int sum = 0;
for(int i = 0; i <= top; i++)
{
sum += stack[i].x * stack[(i + 1) %( top + 1)].y
- stack[i].y * stack[(i+1) % (top + 1)].x;
}
printf("%d\n",sum/100); //凸包面积为 sum/2
}
int main()
{
int n;
while(~scanf("%d", &n))
{
Hull(n);
Area();
}
return 0;
}