poj3246(*凸包)

/*
translation:
	給出n個點,求去掉一點後,再求凸包,所能得到凸包最小的面積是多少?
solution:
	很容易想到去掉的點肯定在凸包上面。所以對上面的點一一枚舉,然後維護最小答案即可。
note:
	* 想半天只想到去掉的點肯定在凸包上,然後暴力枚舉上面的每一個點。但看了下數據範圍又否決了自己的方法。
	  但是萬萬沒想到題解就是按照暴力來寫的,這數據明顯偏弱了。不然如此暴力的方法肯定TLE。
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 100000 + 5;
const double INF = 0x3f3f3f3f * 1.0;

struct Point
{
    double x, y;
    int id;
    Point(){}
    Point(double x_, double y_):x(x_),y(y_){}
} p[maxn], q[maxn], dup[maxn], convex[maxn];
typedef Point Vector;
int n;

Vector operator + (Vector a, Vector b)  { return Vector(a.x + b.x, a.y + b.y); }
Vector operator - (Point a, Point b)    { return Point(a.x - b.x, a.y - b.y); }
Vector operator * (Vector a, double p)  { return Vector(a.x * p, a.y * p); }
Vector operator / (Vector a, double p)  { return Vector(a.x / p, a.y / p); }

bool operator < (const Point& a, const Point& b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

const double eps = 1e-10;
int dcmp(double x)
{
    if(fabs(x) < eps)    return 0;
    else                return x < 0 ? -1 : 1;
}

bool operator == (const Point& a, const Point& b)
{
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double dot(Vector a, Vector b)      { return a.x * b.x + a.y * b.y; }
double length(Vector a)             { return sqrt(dot(a, a)); }
double angle(Vector a, Vector b)    { return acos(dot(a, b) / length(a) / length(b)); }
double angle(Vector v)              { return atan2(v.y, v.x); }
double cross(Vector a, Vector b)    { return a.x * b.y - b.x * a.y; }
double dist(Point p1,Point p2)      { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }
double area(Point a, Point b, Point c) { return cross(b - a, c - a); }

int convexHull(Point* p, int n, Point* ch)
{
	sort(p, p + n);
	int m = 0;
	for(int i = 0; i < n; i++) {
		while(m > 1 && 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 && cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--;
		ch[m++] = p[i];
	}
	if(n > 1) m--;
	return m;
}

double polygonArea(Point* p, int n)
{
	double area = 0;
	for(int i = 0; i < n-1; i++) {
		area += cross(p[i] - p[0], p[i+1] - p[0]);
	}
	return area / 2;
}

int main()
{
	//freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n) && n) {
		for(int i = 0; i < n; i++) {
			scanf("%lf%lf", &p[i].x, &p[i].y);
			p[i].id = i;
		}
		memcpy(dup, p, n * sizeof(Point));
		int m = convexHull(p, n, q);
		double ans = INF;
		for(int i = 0; i < m; i++) {
			memcpy(p, dup, n * sizeof(Point));
			p[q[i].id] = p[0];
			int k = convexHull(p, n, convex);
			ans = min(ans, polygonArea(convex, k));
		}
		printf("%.2f\n", ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章