Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5816 | Accepted: 2639 |
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题目大意:
你现在准备围着很多棵树来建造一个牧场来养奶牛,输入给出所有的树的二维坐标,因为每一只奶牛需要50 m^2 的牧场来生存,所以你被要求使这个牧场建的尽量大,则可以养最多的奶牛。然而牧场一定要被树木围起来哦(但是不需要每一颗树都在边界上)。要你输出最多能养的奶牛数。
解题思路:
每棵树就是一个点,要使面积最大,我们就要找一个凸包,能够把这些树全部包裹进去,然后求出凸包面积,然后除以50 得到牛数。
代码如下:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 10002
#define eps 10e-9
int n,cnt;
struct Point
{
double x,y;
Point (){}
Point ( double x, double y ) : x(x) , y(y) {}
}p[MAX],ch[MAX];
typedef Point Vector;
Point operator - ( Point a , Point b ) { return Point ( a.x - b.x , a.y - b.y ); }
bool cmp ( Point a , Point b ) //将每个点通过优先x最小然后y最小的顺序来排序
{
if ( a.x != b.x ) return a.x < b.x;
else return a.y < b.y;
}
int dcmp ( double x )
{
if ( fabs ( x ) < eps ) return 0;
else return x < 0 ? -1 : 1;
}
double Cross ( Vector u , Vector v ) //叉乘
{
return u.x * v.y - u.y * v.x;
}
int ConvexHull ( Point *p , Point *ch ) //求凸包Andrew算法
{
sort ( p , p + n , cmp );
int m = 0;
for ( int i = 0 ; i < n ; i ++ )
{
while ( m > 1 && dcmp ( 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 && dcmp ( Cross ( ch[m-1] - ch[m-2] , p[i] - ch[m-2] ) ) <= 0 ) m --;
ch[m++] = p[i];
}
return m;
}
double PolygonArea ( Point *ch ) //求多边形面积(因为是凸包所以它的有向面积就是它本身的面积
{
double area = 0;
for ( int i = 1 ; i < cnt -1 ; i ++ )
{
area += Cross ( ch[i] - ch[0] , ch[i+1] - ch[0] );
//cout << area << endl;
}
return area / 2.0;
}
int main()
{
scanf ( "%d" , &n );
for( int i = 0 ; i < n ; i ++ )
scanf ( "%lf%lf" , &p[i].x , &p[i].y );
cnt = ConvexHull ( p , ch );
printf ( "%d\n" ,( int ) ( PolygonArea( ch ) / 50 ) );
return 0;
}
技巧总结:
凸包和多边形面积的应用。