題面:
有
你需要從任意一個村莊出發,然後遍歷任意多個村莊,之後回到出發的村莊。
我們認爲你從村莊
從起點開始的每個點到下一個點的過程中, 如果
你想要最大化你的總收益。
題解:
最後式子是個叉積形式,表示兩點與原點的三角形的面積,而且條件保證答案是一個簡單環,所以對所有點做一遍凸包計算面積即可。
#include<bits/stdc++.h>
const int N = 1e5 + 10;
const double eps = 1e-7;
template <typename T> void read(T &x) {
x = 0; T f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f *= -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
struct rec{double x, y;} a[N], t[N];
bool cmp(const rec &a, const rec &b) {return a.x < b.x;};
rec operator - (const rec &a, const rec &b) {return (rec) {a.x - b.x, a.y - b.y};};
double operator * (const rec &a, const rec &b) {return a.x * b.y - a.y * b.x;};
int n, b[N], cnt;
double ans;
int main() {
freopen("baikal.in", "r", stdin);
freopen("baikal.out", "w", stdout);
read(n);
for (int i = 1; i <= n; i++) read(b[i]);
for (int i = 1; i <= n; i++)
a[i].x = a[i - 1].x + b[i],
a[i].y = a[i].x / b[i];
std::sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
t[++cnt] = a[i];
while (cnt > 2 && (t[cnt] - t[cnt - 2]) * (t[cnt - 1] - t[cnt - 2]) > -eps)
t[cnt - 1] = t[cnt], cnt--;
}
for (int i = 1; i < cnt; i++) ans += t[i] * t[i + 1];
cnt = 0;
for (int i = 1; i <= n; i++) {
t[++cnt] = a[i];
while (cnt > 2 && (t[cnt] - t[cnt - 2]) * (t[cnt - 1] - t[cnt - 2]) < eps)
t[cnt - 1] = t[cnt], cnt--;
}
for (int i = cnt; i > 1; i--) ans += t[i] * t[i - 1];
printf("%.5f\n", ans / 2);
return 0;
}