Luckyleaves's Blog

stay hungry,stay foolish greedy and lucky

P4655 [CEOI2017]Building Bridges

精度永远是这么的难调。

$O(n^2)$的暴力dp几乎是送的,不讲。

sum[i]表示前iw的和,f[j]表示以当前点为终点的最优答案。

所以答案就是:$f(i)= \min (f(j)+sum(i)-sum(j)-w(i)+(h(i)-h(j))^2)$。

推导一波:
$$
\begin{aligned}
f(i) - sum + w(i) - h(i)^2 = \min(f(j)-sum(j)-2h(i)h(j)+h(j)^2)
\end{aligned}
$$
令右边这堆东西为 p(j)

那么当p(j)<p(k)时:
$$
\begin{aligned}
p(j)&<p(k) \\
f(j)-sum(j)-2h(i) h(j)+h(j)^2 &< f(k)-sum(k)-2h(i)h(k)+h(k)^2\\
f(j)-sum(j)+h(j)^2-(f(k)-sum(k)+h(k)^2)&<(2h(j)-2h(k))h(i) \\
\frac{f(j)-sum(j)+h(j)^2-(f(k)-sum(k)+h(k)^2)}{2h(j)-2h(k)}&<h(i)
\end{aligned}
$$
这就是很经典的斜率优化$\frac{ \Delta y}{\Delta x}=k$的形式,注意h(i)并不单增,使用cdq分治解决。

注意精度,凸包形式,斜率不存在的情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
template <class T>
inline void read(T &res)
{
res = 0; bool flag = 0;
char c = getchar();
while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();}
while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
if(flag) res = -res;
}
template <class T, class ...Arg>
inline void read(T &res, Arg &...com){ read(res), read(com...);}
template <class T>
void out(T res)
{
if(res > 9) out(res / 10);
putchar(res % 10 + '0');
}
template <class T>
inline void write(T res)
{
if(res < 0) putchar('-'), res = -res;
out(res), putchar(' ');
}
const int N = 1e5 + 5;
const LL Inf = 1e18;
const double esp = 1e-8;
inline int com(double x, double y) // here
{
if(fabs(x - y) <= esp) return 0;
if(x < y) return -1;
return 1;
}
int n, m;
struct Node{
LL h, sum, f, w, id;
bool operator< (Node b)
{
return h < b.h;
}
}a[N];
int hh, tt, q[N];
inline double get_k(int x, int y)
{
LL x1 = 2 * a[x].h, x2 = 2 * a[y].h;
LL y1 = a[x].f - a[x].sum + a[x].h * a[x].h, y2 = a[y].f - a[y].sum + a[y].h * a[y].h;
if(x1 == x2) return y1 < y2 ? Inf : -Inf; // here
return (double)(y2 - y1) / (double)(x2 - x1);
}
inline void add(int x)
{
while(hh < tt && com(get_k(q[tt], x), get_k(q[tt - 1], q[tt])) <= 0) tt --; // here
q[++ tt] = x;
}
inline LL answer(int x, int y)
{
return (a[x].h - a[y].h) * (a[x].h - a[y].h) + a[x].sum - a[y].sum - a[x].w + a[y].f;
}
inline bool cmp_id(Node x, Node y)
{
return x.id < y.id;
}
void cdq(int l, int r)
{
if(l == r) return ;
int mid = l + r >> 1;
cdq(l, mid);
sort(a + l, a + mid + 1), sort(a + mid + 1, a + r + 1);
hh = 1, tt = 0;
for(int i = l;i <= mid;i ++) add(i);
for(int i = mid + 1;i <= r;i ++)
{
while(hh < tt && com(a[i].h, get_k(q[hh], q[hh + 1])) >= 0) hh ++;
a[i].f = min(a[i].f, answer(i, q[hh]));
}
sort(a + mid + 1, a + r + 1, cmp_id);
cdq(mid + 1, r);
}
int main()
{
read(n);
for(int i = 1;i <= n;i ++) read(a[i].h), a[i].f = Inf, a[i].id = i;
for(int i = 1;i <= n;i ++) read(a[i].w), a[i].sum = a[i - 1].sum + a[i].w;
a[1].f = 0;
cdq(1, n);
write(a[n].f);
return 0;
}