日常訓練 20170708 貝加爾湖畔baikal

題面:
  有 N (1N105) 個村莊, 第 i 個村莊有一個權值 Ai 。定義 Bi=A1+A2+.....+Ai 。保證 BN=0
  你需要從任意一個村莊出發,然後遍歷任意多個村莊,之後回到出發的村莊。
  我們認爲你從村莊 i 到村莊 j 的收益是 (AiAj)×Bi×Bj2×Ai×Aj
  從起點開始的每個點到下一個點的過程中, 如果 B 值有變化, B 值必須先變大再變小。
  你想要最大化你的總收益。
題解:
   (AiAj)×Bi×Bj2×Ai×Aj=Ai×Bi×Bj2×Ai×AjAj×Bi×Bj2×Ai×Aj=Bi×Bj2×AjBi×Bj2×Ai=12×(Bi×BjAjBj×BiAi)
   最後式子是個叉積形式,表示兩點與原點的三角形的面積,而且條件保證答案是一個簡單環,所以對所有點做一遍凸包計算面積即可。

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章