ZOJ 旋转卡壳 3762 Pan's Labyrinth

Pan's Labyrinth

 


 

Time Limit: 2 Seconds        Memory Limit: 65536 KB        Special Judge

 


 

Ofelia is chased by her evil stepfather, and now she finds herself lost in a labyrinth. She needs your help to run away from her tragic family.

There's a huge metal door standing in front of the exit of the labyrinth. There are n dots on the metal door. Pan (the god of the labyrinth) asks Ofelia to find out a triangle which has the largest height. The triangle's three vertexes must be the dots on the door, and its area must be positive. Ofelia should tell Pan the triangle's height so Pan will let Ofelia go.

Input

There are multiple cases (About 100 cases). For each case, the first line contains an integer n (3<=n<=500). In the next n lines, each line contains two real number x[i]y[i] (0<=x[i], y[i]<=10000) which indicates each dot's coordinate. There is no two dots in the same coordinate. The answers are strictly greater than 0.

Output

For each test case, output one line with the maximum triangle height. Any solution with a relative or absolute error of at most 1e-6 will be accepted.

Sample Input
6
0.000 4.000
2.000 4.000
1.000 0.000
1.000 8.000
0.900 7.000
1.100 7.000
7
6967.54555 3457.71200
3.52325 1273.85912
7755.35733 9812.97643
753.00303 2124.70937
7896.71246 8877.78054
5832.77264 5213.70478
4629.38110 8159.01498

Sample Output
7.00000
8940.96643

Hint

In case 1. Choose the dot 3, 5, 6 to make up a triangle, and this triangle has the largest height 7.000. In case 2. We choose dot 2, 3, 5.


 

题意:给出一个点集,求出高最大的三角形的高。

 

思路:http://blog.csdn.net/accelerator_916852/article/details/20394799

 

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define eps 1e-8

inline double min(double a, double b) { return a < b ? a : b; }
inline double max(double a, double b) { return a > b ? a : b; }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }

int dcmp(double a, double b)
{
	if (a<b - eps) return -1;
	else if (a>b + eps) return 1;
	return 0;
}

struct Point
{
	double x, y;
	Point(double x = 0, double y = 0)
		:x(x), y(y) { }
	bool operator<(const Point&p) const
	{
		return dcmp(x, p.x)<0 || (dcmp(x, p.x) == 0 && y<p.y);
	}
	bool operator==(const Point&p) const
	{
		return dcmp(x, p.x) == 0 && dcmp(y, p.y) == 0;
	}
};

typedef Point Vector;
Vector operator+(const Vector&a, const Vector&b) { return Vector(a.x + b.x, a.y + b.y); }
Vector operator-(const Vector&a, const Vector&b) { return Vector(a.x - b.x, a.y - b.y); }
Vector operator*(const Vector&a, double x) { return Vector(a.x*x, a.y*x); }
double Dot(const Vector&a, const Vector& b) { return a.x*b.x + a.y*b.y; }
double Cross(const Vector&a, const Vector&b) { return a.x*b.y - a.y*b.x; }
double Distance(const Point&a, const Point&b) { return sqrt(Dot(a - b, a - b)); }

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


struct Line
{
	Point a, b;
	Vector v;
	double ang;
	Line() { }
	Line(Point a, Point b)
		:a(a), b(b) { v = b - a; ang = atan((a.y - b.y) / (a.x - b.x)); }
	bool operator<(const Line&Le) const { return ang < Le.ang; }
};

bool OnLeft(const Point&a, const Line&L) { return Cross(a - L.a, L.v) > eps; }
bool OnRight(const Point&a, const Line&L) { return Cross(a - L.a, L.v) < -eps; }

const int maxn = 500 + 5;
Point p[maxn], hull[maxn];
Line L[maxn][maxn*2];
int line_cnt;

int n;


double Rotate(int m)
{
	double ans = 0;
	hull[m] = hull[0];
	for (int i = 0; i < n; ++i) {
		int k = 0;
		for (int j = 0; j < n-1; ++j) {
			while (OnRight(hull[k], L[i][j])) k = (k + 1) % m;
			while (hull[k] == L[i][j].a || hull[k] == L[i][j].b) k = (k + 1) % m;
			while (dcmp(Cross(L[i][j].v, hull[k + 1] - L[i][j].a), Cross(L[i][j].v,hull[k]-L[i][j].a))>0) k = (k + 1) % m;
			double d = Distance(L[i][j].a, L[i][j].b);
			double h = Cross(L[i][j].v, hull[k] - L[i][j].a) / d;
			if (h>ans) ans = h;
		}
	}
	return ans;
}

void input()
{
	for (int i = 0; i < n; ++i) scanf("%lf%lf", &p[i].x, &p[i].y);
	line_cnt = 0;
	for (int i = 0; i < n; ++i) {
		int cnt = 0;
		for (int j = 0; j < n; ++j) if (i != j)
			L[i][cnt++] = Line(p[i], p[j]);
		sort(L[i], L[i] + cnt);
	}
}

void solve()
{
	int m = Convexhull(p, n, hull);
	double ans = Rotate(m);
	printf("%.7lf\n", ans);
}

int main()
{
	while (scanf("%d", &n) == 1)
	{
		input();
		solve();
	}
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值