JNU周练1026
题意:给出n个点(无重点,无三点共线),选四个点组成平面四边形,求所能组成的四边形的最大面积。 (n<=300)
枚举四边形的对角线,再枚举每个点,左右各选最远的点组成两个三角形,形成一个最大四边形。
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
#define N 310
struct Point
{
double x, y;
}p[N];
double xmult(Point p0, Point p1, Point p2)
{
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x-p0.x) * (p1.y - p0.y);
}
int main()
{
int n;
double ans = 0;
cin >> n;
for(int i=0; i<n; i++)
cin >> p[i].x >> p[i].y;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
double a1 = 0, a2 = 0;
for(int k=0; k<n; k++)
{
double tmp = xmult(p[i],p[j],p[k]);
if(tmp>0)
a1 = max(a1, tmp);
else
a2 = min(a2, tmp);
}
a2 = -a2;
if(a1!=0 && a2!=0 && (a1+a2)/2>ans)
{
ans = (a1+a2)/2;
//cout << a1 / 2 << " " << a2 / 2 << endl;
}
}
}
cout << setprecision(10) << ans << endl;
return 0;
}
#include <iomanip>
#include <algorithm>
using namespace std;
#define N 310
struct Point
{
double x, y;
}p[N];
double xmult(Point p0, Point p1, Point p2)
{
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x-p0.x) * (p1.y - p0.y);
}
int main()
{
int n;
double ans = 0;
cin >> n;
for(int i=0; i<n; i++)
cin >> p[i].x >> p[i].y;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
double a1 = 0, a2 = 0;
for(int k=0; k<n; k++)
{
double tmp = xmult(p[i],p[j],p[k]);
if(tmp>0)
a1 = max(a1, tmp);
else
a2 = min(a2, tmp);
}
a2 = -a2;
if(a1!=0 && a2!=0 && (a1+a2)/2>ans)
{
ans = (a1+a2)/2;
//cout << a1 / 2 << " " << a2 / 2 << endl;
}
}
}
cout << setprecision(10) << ans << endl;
return 0;
}