LOJ 2483: 洛谷 P4655: 「CEOI2017」Building Bridges

题意简述：

$n$ 个数，每个数有高度 $h_i$ 和价格 $w_i$ 两个属性。

题解：

\begin{aligned}\mathrm{f}[j]+h_j^2-s_j-2h_i\times h_j&\Leftrightarrow\mathrm{f}[k]+h_k^2-s_k-2h_i\times h_k\\mathrm{f}[k]+h_k^2-s_k)-(\mathrm{f}[j]+h_j^2-s_j)&\Leftrightarrow 2h_i\times(h_k-h_j)\end{aligned} \(x 坐标是 $h_i$$y$ 坐标是 $\mathrm{f}[i]+h_i^2-s_i$。假设 $x_j<x_k$，则决策 $j$$k$ 优当且仅当：

\begin{aligned}\mathrm{f}[j]+h_j^2-s_j-2h_i\times h_j&<\mathrm{f}[k]+h_k^2-s_k-2h_i\times h_k\\y_k-y_j&>2h_i\times(x_k-x_j)\\\frac{y_k-y_j}{x_k-x_j}&>2h_i\end{aligned}

#include <cstdio>
#include <cstring>
#include <algorithm>

typedef long long LL;
const int MN = 100005;

int N, p[MN], tmp[MN];
LL h[MN], w[MN], f[MN], X[MN], Y[MN];

inline double Slope(int i, int j) {
if (X[i] == X[j]) return 1e50 * (Y[j] - Y[i]);
return (double)(Y[j] - Y[i]) / (X[j] - X[i]);
}

int que[MN], l, r;
void Solve(int lb, int rb) {
if (lb == rb) { Y[lb] += f[lb]; return ; }
int mid = (lb + rb) >> 1;
Solve(lb, mid);
for (int i = lb; i <= rb; ++i) p[i] = i;
std::sort(p + lb, p + rb + 1, [](int i, int j) { return h[i] < h[j]; });
l = 1, r = 0;
for (int i = lb; i <= rb; ++i) if (p[i] <= mid) {
while (l < r && Slope(que[r - 1], que[r]) >= Slope(que[r], p[i])) --r;
que[++r] = p[i];
}
for (int i = lb; i <= rb; ++i) if (p[i] > mid) {
while (l < r && Slope(que[l], que[l + 1]) <= 2 * h[p[i]]) ++l;
f[p[i]] = std::min(f[p[i]], f[que[l]] + (h[p[i]] - h[que[l]]) * (h[p[i]] - h[que[l]]) + w[p[i] - 1] - w[que[l]]);
}
Solve(mid + 1, rb);
}

int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%lld", &h[i]);
for (int i = 1; i <= N; ++i) scanf("%lld", &w[i]), w[i] += w[i - 1];
for (int i = 1; i <= N; ++i) p[i] = i, X[i] = h[i], Y[i] = h[i] * h[i] - w[i];
memset(f, 0x7f, sizeof f);
f[1] = 0, Solve(1, N);
printf("%lld\n", f[N]);
return 0;
}

posted @ 2019-03-21 23:23  粉兔  阅读(293)  评论(0编辑  收藏