The 18th Jilin Provincial Collegiate Programming Contest(第十八届吉林省省赛)B - Triangle Uika

原题链接:Triangle Uika

题目大意

给定 3 3 3 条折线,每一条折线从起点开始按序给出直到终点,有 3 3 3 个人偶从对应的折线开始同时运动,每次运动一个单位距离,当到达终点处时停止,求解三个人偶的位置在运动过程中围成的三角形面积最大值。

样例输入
第一行输入为测试样例个数

1 2 2
0 0
0 0
2 2
0 2
2 2

样例输出

0.3535533906

题目分析

  • 首先对于折线,每一条折线都要按线段分段,这样 O ( n ) O(n) O(n) 可以遍历完三条折线。
  • 取当前三个点坐标为 p 1 , p 2 , p 3 p_1,p_2,p_3 p1,p2,p3,当前每个点所在对应的线段的方向向量为 o 1 , o 2 , o 3 o_1,o_2,o_3 o1,o2,o3,那么行走了 t t t 时间后的点坐标为 p 1 + t ⋅ o 1 , p 2 + t ⋅ o 2 , p 3 + t ⋅ o 3 p_1+t\cdot o_1,p_2+t\cdot o_2,p_3+t\cdot o_3 p1+to1,p2+to2,p3+to3,用叉积计算面积为 1 2 ∣ ( p 2 − p 1 + t ( o 2 − o 1 ) ) × ( p 3 − p 1 + t ( o 3 − o 1 ) ) ∣ \frac{1}{2}|(p_2-p_1+t(o_2-o_1))\times(p_3-p_1+t(o_3-o_1))| 21(p2p1+t(o2o1))×(p3p1+t(o3o1)),对于每一段,由于 p 1 , p 2 , p 3 , o 1 , o 2 , o 3 p_1,p_2,p_3,o_1,o_2,o_3 p1,p2,p3,o1,o2,o3 对应的坐标值为常量,那么面积是一个关于 t t t 的二次函数,用三分法寻找到最大值即可。

代码答案

#include<bits/stdc++.h>
#define endl '\n'
using ll = long long;
using namespace std;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
const double eps = 1e-7;
const double PI = acos(-1.0);

int sgn(double x) {
    if(fabs(x) < eps) return 0;
    return x < 0 ? -1 : 1;
}

struct Point {
    double x, y;
    Point(){}
    Point(double x, double y) : x(x), y(y) {}
    bool operator ==(Point p) const {return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;}
    void input(){cin >> x >> y;}
    bool operator <(Point p) const {return sgn(x - p.x) < 0 || (sgn(x - p.x) == 0 && sgn(y - p.y) < 0);}
    // 向量加法
    Point operator +(Point p) const {return Point(x + p.x, y + p.y);}
    // 向量减法
    Point operator -(Point p) const {return Point(x - p.x, y - p.y);}
    // 点积(内积):向量A在向量B上的投影长度乘向量B的模长
    double operator *(Point p) const {return x * p.x + y * p.y;}
    // 数乘
    Point operator *(double k) const {return Point(x * k, y * k);}
    Point operator /(double k) const {return Point(x / k, y / k);}
    // 叉积(外积):向量A与B张成的平行四边形的有向面积
    double operator ^(Point p) const {return x * p.y - y * p.x;}
    // 距离(模长):向量A到向量B的有向距离
    double len(Point p = Point(0, 0)) const {return hypot(x - p.x, y - p.y);}
    // 角度(弧度制):两向量无向夹角[0, π]
    double angle(Point p = Point(1, 0)) const {return acos(*this * p / len() / p.len());}
    // 角度(弧度制):两向量有向夹角[-π, π]
    double dangle(Point p = Point(1, 0)) const {
        double delta = atan2(p.x, p.y) - atan2(x, y);
        while(delta > PI) delta -= 2 * PI;
        while(delta < -PI) delta += 2 * PI;
        return delta;
    }
    // 旋转(弧度制):逆时针旋转theta弧度
    Point rotate(double theta) {return Point(x * cos(theta) - y * sin(theta), y * cos(theta) + x * sin(theta));}
} path1[N], path2[N], path3[N];
ll n1, n2, n3;

double area(Point a, Point b, Point c) {
	return fabs((b - a) ^ (c - a)) / 2;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n1 >> n2 >> n3;
	for(int i = 1; i <= n1; i++) path1[i].input();
	for(int i = 1; i <= n2; i++) path2[i].input();
	for(int i = 1; i <= n3; i++) path3[i].input();
	double t1 = 0, t2 = 0, t3 = 0, ans = 0;
	Point p1 = path1[1], p2 = path2[1], p3 = path3[1];
	Point o1, o2, o3;
	for(int i = 0, j = 0, k = 0; i < n1 || j < n2 || k < n3;) {
		if(sgn(t1) <= 0) {
			if(i < n1) i++;
			if(i == n1) o1 = Point(0, 0), t1 = 0;
			else t1 = path1[i].len(path1[i + 1]), o1 = (path1[i + 1] - path1[i]) / t1;
		}
		if(sgn(t2) <= 0) {
			if(j < n2) j++;
			if(j == n2) o2 = Point(0, 0), t2 = 0;
			else t2 = path2[j].len(path2[j + 1]), o2 = (path2[j + 1] - path2[j]) / t2;
		}
		if(sgn(t3) <= 0) {
			if(k < n3) k++;
			if(k == n3) o3 = Point(0, 0), t3 = 0;
			else t3 = path3[k].len(path3[k + 1]), o3 = (path3[k + 1] - path3[k]) / t3;
		}
		double cost = INF; 
		if(i < n1) cost = min(cost, t1); 
		if(j < n2) cost = min(cost, t2); 
		if(k < n3) cost = min(cost, t3);
		double l = 0, r = cost; int cnt = 0; // 控制循环次数避免精度问题
		while(sgn(r - l) != 0 && ++cnt <= 100) {
			double lm = l + (r - l) / 3, rm = r - (r - l) / 3;
			double s1 = area(p1 + o1 * lm, p2 + o2 * lm, p3 + o3 * lm);
			double s2 = area(p1 + o1 * rm, p2 + o2 * rm, p3 + o3 * rm);
			ans = max({ans, s1, s2});
			if(s1 > s2) r = rm;
			else l = lm;
		}
		p1 = p1 + o1 * cost, p2 = p2 + o2 * cost, p3 = p3 + o3 * cost;
		t1 -= cost, t2 -= cost, t3 -= cost;
	}
	cout << fixed << setprecision(10) << ans << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值