0
点赞
收藏
分享

微信扫一扫

NYOJ952--最大四边形--叉积分成三角形


最大四边形

链接:​​click here​​



时间限制:1000 ms  |  内存限制:65535



难度:2


描述 平面坐标上有n个点,你知道能组成四边形中面积最大的是多少吗? 输入 有多组测试数据

第一行整数n,表示有n个点,( 4<=n<=300 )

然后n行,每行x,y表示点的坐标。(没有重复的点)

输出 最大四边形的面积.(保留六位小数) 样例输入

5
0 0
0 4
4 0
4 4
2 3

样例输出

16.000000


模板题;

/*************
Times:236ms
NYOJ 952
***************/
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};
const double Pi = acos(-1.0);
const int maxn=500;
const double eps=1e-10;
struct point
{
double x,y;
point(double xx=0,double yy=0):x(xx),y(yy) {}//构造函数
} p[maxn];
typedef point PP;
PP operator - (PP a,PP b)
{
return PP(a.x-b.x,a.y-b.y);
}
double cross (PP a,PP b)
{
return (a.x*b.y-a.y*b.x)*0.5;
}
int main()
{
//freopen("1.txt","r",stdin);
int n,i,j,k;
while(cin>>n)
{
for(i=0; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
double ans=0;
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
double fx=0,fy=0;
for(int k=0; k<n; k++)
{
if(k==i||k==j)
continue;
double s=cross(p[i]-p[k],p[j]-p[k]);//||叉积
if(s<eps)
fx=max(fx,-s);//因为方向相反,利用最大值求出来刚好是两边的最大值
else
fy=max(fy,s);
}
if(fx==0||fy==0)排除三角形
continue;
ans=max(ans,fx+fy);
}
}
printf("%f\n",ans);
}
return 0;
}

其他方法:
<pre name="code" class="cpp">/**************
Times:116ms;
**************/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
#define LL long long

const int N = 305;
const double eps = 1e-8;
struct POINT{
double x, y;
}p[N];
int n;

double cross(POINT o, POINT a, POINT b){
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}

double Area(POINT a, POINT b){
double lmax = 0, rmax = 0;
for(int i = 0; i < n; i ++){
double tmp = cross(a, b, p[i]);
if(tmp < eps) lmax = max(lmax, -tmp);
if(tmp > eps) rmax = max(rmax, tmp);
}
if(lmax == 0 || rmax == 0) return 0;
return (lmax + rmax) / 2.0;
}
int main(){
// freopen("input.txt", "r", stdin);
while(~scanf("%d", &n)){
for(int i = 0; i < n; i ++){
scanf("%lf %lf", &p[i].x, &p[i].y);
}
double ans = 0;
for(int i = 0; i < n; i ++){
for(int j = i + 1; j < n; j ++)
ans = max(ans, Area(p[i], p[j]));
}
printf("%.6lf\n", ans);
}
return 0;
}



 








举报

相关推荐

0 条评论