LA 2402 (枚举) Fishnet

时间:2023-03-09 21:29:55
LA 2402 (枚举) Fishnet

题意:

正方形四个边界上分别有n个点,将其划分为(n+1)2个四边形,求四边形面积的最大值。

分析:

因为n的规模很小,所以可以二重循环枚举求最大值。

求直线(a, 0) (b, 0) 和直线(0, c) (0, d)的交点,我是二元方程组求解得来的,然后再用叉积求面积即可。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> const int maxn = + ;
struct HEHE
{
double a, b, c, d;
}hehe[maxn]; struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
};
typedef Point Vector; Vector operator - (const Vector& A, const Vector& B)
{ return Vector(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); } Point GetIntersection(const double& a, const double& b, const double& c, const double& d)
{
double x = (a+(b-a)*c) / (-(b-a)*(d-c));
double y = (d-c)*x+c;
return Point(x, y);
} int main(void)
{
//freopen("2402in.txt", "r", stdin); int n;
while(scanf("%d", &n) == && n)
{
memset(hehe, , sizeof(hehe));
for(int i = ; i <= n; ++i) scanf("%lf", &hehe[i].a);
for(int i = ; i <= n; ++i) scanf("%lf", &hehe[i].b);
for(int i = ; i <= n; ++i) scanf("%lf", &hehe[i].c);
for(int i = ; i <= n; ++i) scanf("%lf", &hehe[i].d);
hehe[n+].a = hehe[n+].b = hehe[n+].c = hehe[n+].d = 1.0; double ans = 0.0;
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
{
Point A, B, C, D;
A = GetIntersection(hehe[i].a, hehe[i].b, hehe[j].c, hehe[j].d);
B = GetIntersection(hehe[i+].a, hehe[i+].b, hehe[j].c, hehe[j].d);
C = GetIntersection(hehe[i+].a, hehe[i+].b, hehe[j+].c, hehe[j+].d);
D = GetIntersection(hehe[i].a, hehe[i].b, hehe[j+].c, hehe[j+].d);
double temp = 0.0;
temp += Cross(B-A, C-A) / ;
temp += Cross(C-A, D-A) / ;
ans = std::max(ans, temp);
} printf("%.6f\n", ans);
} return ;
}

代码君