这个题目要求的是最多的三角形,其实只要所有点都连了基本上就是最大了,我们要考虑的主要是最小
想想要想最大的话每个点都一定会连,而且一定是从左向右,因为不能相交嘛,那么就想办法使总长度最小
首先毫无疑问的是第一个点一定与第一个点相连,然后看程序中的while循环,下面的步骤要么是x++,要
么是y++,那么就根据下一步的链接情况来判断,要没是x与y+1连接,要么是x+1,与y连接,两种情况
选择最优,最后考虑结束的特殊情况!
#include<cstdio> #include<iostream> #include<cstring> #include<iostream> #include<cmath> using namespace std; double a[100001]; double b[100001]; int main() { int t; int c = 1; scanf_s("%d", &t); while (t--) { double y1, y2; scanf_s("%lf%lf", &y1, &y2); double l = (y1 - y2)*(y1 - y2); int n, m; scanf_s("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf_s("%lf", &a[i]); } for (int i = 0; i < m; i++) scanf_s("%lf", &b[i]); int z1 = 0, z2 = 0; double ans = 0; while (z1 < n && z2 < m) { ans += sqrt(l + (a[z1] - b[z2])*(a[z1] - b[z2])); if (z1 == n - 1) z2++; else if (z2 == m - 1) z1++; else { if (fabs(a[z1] - b[z2 + 1]) < fabs(b[z2] - a[z1 + 1])) z2++; else z1++; } } printf("Case #%d: %.2f\n", c++, ans); } }