例题9-11(uva-1331)

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int INF = 1e9;
const int maxn = 50 + 5;
const double eps = 0.00001;
int n, m;
struct Point {
	int x, y;
	friend Point operator-(const Point& lhs, const Point& rhs) {
		return { rhs.x - lhs.x, rhs.y - lhs.y };
	}
	// 公式: a(x1, y1) * b(x2, y2) = x1 * y2 - x2 * y1;
	friend double operator*(const Point& lhs, const Point& rhs) {
		return std::abs(lhs.x * rhs.y - lhs.y * rhs.x) * 1.0;
	}
}A[maxn];

// 判断顶点i, 顶点j, 顶点k, 组成的三角形是否合法(即内部没有其他顶点)
// 合法就返回面积, 非法返回0
// 判断是否合法:
//  判断是否有一个点在三角形内部, 如果一个点在内部, 三角形的三个端点连接内部这个点, 将面积分为了3部分,
//  判断这3部分相加是否等于原三角形面积. 相等说明有点在内部.

// 叉积算三角形的面积
double area(int i, int j, int k) {
	Point a = A[k] - A[i];
	Point b = A[k] - A[j];
	// 四边形面积除以2便是三角形的面积
	return a * b * 1.0 / 2;
}

double is_valid(int i, int j, int k) {
	for (int p = 1; p <= m; ++p) {
		if (!(p == i || p == j || p == k)) {
			double area_S = area(i, j, k);
			double area_1 = area(i, j, p);
			double area_2 = area(i, k, p);
			double area_3 = area(k, j, p);
			// 非法
			if (std::fabs(area_S - (area_1 + area_2 + area_3)) < eps)
				return 0;
		}
	}
	// 合法
	return area(i, j, k);
}

// dp[i][j]: 表示从i, i + 1, i + 2, ... , j,(看成一个圈, j的下一个元素为i, 即i和j相邻), 这些顶点中所剖分的三角形中, 最大三角形面积的最小值
double dp[maxn][maxn];
double dfs(int L, int R) {
	// 记忆化
	if (dp[L][R] != -1)
		return dp[L][R];
	// 递归的边界
	if (std::abs(R - L) <= 2)
		return dp[L][R] = area(L, L + 1, R);
	double ret = INF;
	for (int k = L + 1; k < R; ++k) {
		if (is_valid(L, R, k)) {
			ret = std::min(ret, std::max(dfs(L, k), std::max(area(L, R, k), dfs(k, R))));
		}
	}
	return dp[L][R] = ret;
}

int main() {
	cin >> n;
	while (n--) {
		// 初始化
		for (int i = 0; i < maxn; ++i)
			std::fill(dp[i], dp[i] + maxn, -1);
		cin >> m;
		for (int i = 1; i <= m; ++i) {
			cin >> A[i].x >> A[i].y;
		}
		cout << fixed << setprecision(1) << dfs(1, m) << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个实现该输出的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> class myclass { public: T operator()(T a, T b) { return a + b; } }; int main() { vector<int> v1 = {1, 2, 3, 4, 5}; vector<int> v2 = {6, 7, 8, 9, 10}; vector<int> v3(5); transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), myclass<int>()); for (auto& x : v3) { cout << x << " "; } cout << endl; vector<double> v4 = {1.1, 2.2, 3.3, 4.4, 5.5}; vector<double> v5 = {6.6, 7.7, 8.8, 9.9, 10.0}; vector<double> v6(5); transform(v4.begin(), v4.end(), v5.begin(), v6.begin(), myclass<double>()); for (auto& x : v6) { cout << x << " "; } cout << endl; return 0; } ``` 在这个示例代码中,我们定义了一个类模板myclass,它有一个模板参数T,并重载了()运算符。在()运算符中,我们将两个参数相加,并返回结果。 在主函数中,我们定义了两个整型向量v1和v2,以及一个整型向量v3。我们使用transform函数将v1和v2中的元素依次相加,并将结果存储在v3中。然后,我们遍历v3中的元素,并将其输出到控制台上。 接着,我们定义了两个双精度浮点型向量v4和v5,以及一个双精度浮点型向量v6。我们再次使用transform函数将v4和v5中的元素依次相加,并将结果存储在v6中。最后,我们遍历v6中的元素,并将其输出到控制台上。 最终,程序输出的结果如下: ``` 7 9 11 13 15 7.7 9.9 12.1 14.3 15.5 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值