CF311B. Cats Transport

给定$d_i$山丘的位置,以及每个小猫在$i$个山丘$t_i$结束游玩。等待铲屎官接送。

安排每个铲屎官出发的时间,最小化猫子们等待的时间之和。

把猫正好需要铲屎官的时间计算出来,排个序,问题就变成把数组分为 $p$ 段,每段的代价是所有数与该段最大值的差值之和,求最小代价。

显然斜率优化形式

滚动数组,复杂度为$O(np)$

代码
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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
ll sum[N], dp[2][N], a[N], d[N];
// db X(int x)
// {
// return x;
// }
// db Y(int x)
// {
// return sum[x] + dp[x];
// }
db slope(int t, int i, int j)
{
return 1.00 * (sum[i] - sum[j] + dp[t][i] - dp[t][j]) / (i - j);
}
int q[N];

int main()
{
int n = read(), m = read(), p = read();
for (int i = 2; i <= n; i++)
d[i] = read(), d[i] += d[i - 1];
for (int i = 1; i <= m; i++)
{
int x = read(), t = read();
a[i] = t - d[x];
}
sort(1 + a, 1 + a + m);
for (int i = 1; i <= m; i++)
sum[i] = sum[i - 1] + a[i];
for (int i = 1; i <= m; i++)
dp[1][i] = i * a[i] - sum[i];

for (int t = 2; t <= p; t++)
{
int head = 1, tail = 1;
int last = (t & 1) ^ 1;

for (int i = 1; i <= m; i++)
{

while (head < tail && slope(last, q[head + 1], q[head]) < a[i])
head++;
int k = q[head];

dp[t & 1][i] = dp[last][k] + (i - k) * a[i] - (sum[i] - sum[k]);
while (head < tail && slope(last, i, q[tail]) < slope(last, q[tail - 1], q[tail]))
tail--;
q[++tail] = i;
}
}
printf("%lld\n", dp[p & 1][m]);
}