链接:牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ
A题签到题:
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int y[N];
double cut[N];
double dis(int a, int b) // a, b两点距离
{
int x = b - a;
return sqrt(x * x + (y[a] - y[b]) * (y[a] - y[b]));
}
int main()
{
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> y[i];
}
double sum = 0;
double maxcut = 0;
int del = 0;
for (int i = 1; i <= n; i++)
{
sum += dis(i, i - 1);
cut[i] = dis(i, i - 1) + dis(i + 1, i) - dis(i - 1, i + 1); // 删去这个点能减少的长度
if (cut[i] > maxcut)
{
del = i;
maxcut = cut[i];
}
}
sum += dis(n, n+1);
sum -= maxcut;
cut[del] = 0; // 删去点del后,更新他前后的两个点
cut[del - 1] = dis(del - 1, del - 2) + dis(del + 1, del - 1) - dis(del - 2, del + 1);
cut[del + 1] = dis(del + 2, del + 1) + dis(del + 1, del - 1) - dis(del - 1, del + 2);
maxcut = 0;
for (int i = 1; i <= n; i++)
{
maxcut = max(cut[i], maxcut);
}
sum -= maxcut;
printf("%.16f", sum);
return 0;
}