POJ - Cows(凸包面积)
题目链接:http://poj.org/problem?id=3348
Time Limit: 2000MS Memory Limit: 65536K
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
Problem solving report:
Description: 给你n棵树,能够用这n棵树围一个圈。然后在圈里面能够养牛,每一个牛至少要50平方米的空间,问最多能够养多少牛。
Problem solving: 先求凸包,再求凸包构成的多边形面积。
Accepted Code:
//AndrewScan
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
const double eps = 1e-8;
typedef struct Point {
double x, y;
Point(double x_ = 0, double y_ = 0) : x(x_), y(y_) {}
bool operator < (const Point& s) const {
return x != s.x ? x < s.x : y < s.y;
}
}vect;
struct Point p[MAXN], S[MAXN];
int sgn(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
vect operator - (const Point a, const Point b) {
return vect(a.x - b.x, a.y - b.y);
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
double dist(const Point a, const Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool Onleft(const Point a, const Point b, const Point c) {
return sgn(Cross(b - a, c - a)) > 0;
}
double Area(const Point p[], int n) {
double area = 0;
for (int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
}
int AndrewScan(Point p[], int n) {
sort(p, p + n);
int top = 0;
for (int i = 0; i < n; i++) {
while (top > 1 && !Onleft(S[top - 2], S[top - 1], p[i]))
top--;
S[top++] = p[i];
}
int tmp = top;
for (int i = n - 2; i >= 0; i--) {
while (top > tmp && !Onleft(S[top - 2], S[top - 1], p[i]))
top--;
S[top++] = p[i];
}
return top;
}
int main() {
double dis;
int n, l, cnt;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
cnt = AndrewScan(p, n);
printf("%d\n", (int)Area(S, cnt) / 50);
return 0;
}
//Graham扫描线
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
const double eps = 1e-8;
typedef struct Point {
double x, y;
Point(double x_ = 0, double y_ = 0) : x(x_), y(y_) {}
}vect;
struct Point p[MAXN], S[MAXN];
int sgn(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
vect operator - (const Point a, const Point b) {
return vect(a.x - b.x, a.y - b.y);
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
double dist(const Point a, const Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool operator < (const Point& a, const Point& b) {
int x = sgn(Cross(a - p[0], b - p[0]));
if (!x)
return dist(a, p[0]) < dist(b, p[0]);
return x > 0;
}
bool Onleft(const Point a, const Point b, const Point c) {
return sgn(Cross(b - a, c - a)) > 0;
}
double Area(const Point p[], int n) {
double area = 0;
for (int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
}
int Graham(Point p[], int n) {
int t = 0;
for (int i = 1; i < n; i++)
if (sgn(p[t].y - p[i].y) > 0 || (!sgn(p[t].y - p[i].y) && sgn(p[t].x - p[i].x) > 0))
t = i;
swap(p[0], p[t]);
sort(p + 1, p + n);
int top = 0;
for (int i = 0; i < n; i++) {
while (top > 1 && !Onleft(S[top - 2], S[top - 1], p[i]))
top--;
S[top++] = p[i];
}
return top;
}
int main() {
double dis;
int n, l, cnt;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
cnt = Graham(p, n);
printf("%d\n", (int)Area(S, cnt) / 50);
return 0;
}