poj3348:Cows(凸包面积)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/82956372

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<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 200005;

struct point {
	double x, y;
	point(double x = 0, double y = 0):x(x),y(y) {};
};

double dis(point p1, point p2) {
	return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}

double cross(point p0,point p1,point p2) {
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

/*------------------------------------------------------------------------------------*/

point plist[maxn], pstack[maxn];
bool cmp(const point &p1, const point &p2) {
	double tmp = cross(plist[0], p1, p2);
	if (tmp > 0.0) return true;
	else if (tmp == 0.0 && dis(plist[0], p1) < dis(plist[0], p2)) return true;
	else return false;
}

void init(int n) {
	int k = 0;
	scanf("%lf%lf", &plist[0].x, &plist[0].y);
	for (int i = 1; i < n; i++) {
		scanf("%lf%lf", &plist[i].x, &plist[i].y);
		if ((plist[k].y > plist[i].y) || ((plist[k].y == plist[i].y) && (plist[k].x > plist[i].x)))
			k = i;
	}
	swap(plist[0], plist[k]);
	sort(plist+1,plist+n,cmp);
}

int Graham(int n) {
	if(n == 1)
		return 1;
	int top = 1;
	pstack[0] = plist[0];
	pstack[1] = plist[1];
	for (int i = 2; i < n; i++) {
		while (top>0 && cross(pstack[top - 1], pstack[top], plist[i]) <= 0) top--;
		pstack[++top] = plist[i];
	}
	return top + 1;
}

int main() {
	int n;
	scanf("%d",&n);
	init(n);
	n = Graham(n);
	double ans = 0;
	for(int i = 2; i < n; i++)
		ans += cross(pstack[i - 1], pstack[i], pstack[0]) / 2.0;
	
	printf("%lld\n", (ll)ans / 50);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/82956372