# 正文

## 时间复杂度：$O(t^2n)$

$f_i$表示最后一段的右端点$i$，对于每一个$i$我们需要找到转移到$f_i$的决策。

## 时间复杂度：$O(t^2)$

$f_i=min(f_j+\sum^{}_{j<tk\leq i}i-t_k)$
$\sum$可以发现可以用前缀和优化。

$\sum^{}_{j<tk\leq i}i-t_k=\sum^{}_{j<tk\leq i}i-\sum^{}_{j<tk\leq i}t_k=(psum_i-psum_j)\times i-(tsum_i-tsum_j)$

#include <bits/stdc++.h>
using namespace std;
namespace IOstream {
#define gc getchar
template <typename T>
x = 0; T fl = 1; char c = 0;
for (; c < '0' || c > '9'; c = gc()) if (c == '-') fl = -1;
for (; c >= '0' && c <= '9'; c = gc()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= fl;
}
#undef gc
} using namespace IOstream;
const int N = 4e6 + 506;
const int inf = 0x3f3f3f3f;
int psum[N], tsum[N], f[N];
int n, m, t;
int main() {
if (m == 1) { puts("0"); return 0; }
for (int i = 1, x; i <= n; i ++) {
psum[x] ++; tsum[x] += x;
}
for (int i = 0; i < t + m; i ++) {
tsum[i] += tsum[i - 1];
psum[i] += psum[i - 1];
}
memset(f, inf, sizeof(f));
for (int i = 0; i < t + m; i ++) {
f[i] = psum[i] * i - tsum[i];
for (int j = 0; j + m <= i; j ++) {
f[i] = min(f[i], f[j] + (psum[i] - psum[j]) * i - (tsum[i] - tsum[j]));
}
}
int ans = inf;
for (int i = t; i < t + m; i ++) ans = min(ans, f[i]);
cout << ans << endl;
return 0;
}

## 时间复杂度：$O(t)$

$f_i=min(f_j+\sum^{}_{j<tk\leq i}i-t_k)$

$\underline{f_j+tsum_j}_y=\underline{i}_k\times \underline{psum_i}_x+\underline{(f_i+psum_j\times i-tsum_i)}_b$

$y=kx+b$

#include <bits/stdc++.h>
using namespace std;
namespace IOstream {
#define gc getchar
template <typename T>
x = 0; T fl = 1; char c = 0;
for (; c < '0' || c > '9'; c = gc()) if (c == '-') fl = -1;
for (; c >= '0' && c <= '9'; c = gc()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= fl;
}
#undef gc
} using namespace IOstream;
typedef double db;
const int N = 4e6 + 506;
const int inf = 0x3f3f3f3f;
int psum[N], tsum[N], f[N], q[N << 1];
// psum记录的是人数前缀和，tsum表示总时间的前缀和
int n, m, T;
db Y(int i) { return 1.0 * (- f[i] - tsum[i]); }
db X(int i) { return 1.0 * (- psum[i]); }
db slope(int i, int j) { return (Y(i) - Y(j)) / (psum[i] == psum[j] ? 1e-9 : (X(i) - X(j))); }
int main() {
if (m == 1) { puts("0"); return 0; }
for (int i = 1, x; i <= n; i ++) {
psum[x] ++; tsum[x] += x;
}
for (int i = 0; i < T + m; i ++) {
tsum[i] += tsum[i - 1];
psum[i] += psum[i - 1];
}
int h = 1, t = 0;
for (int i = 0; i < T + m; i ++) {
if (i >= m) {
while (h < t && slope(q[t - 1], q[t]) >= slope(q[t], i - m)) t --;
q[++ t] = i - m;
}
while (h < t && slope(q[h], q[h + 1]) <= i) h ++;
f[i] = psum[i] * i - tsum[i];
int j = q[h];
if (h <= t) f[i] = min(f[i], f[j] + (psum[i] - psum[j]) * i - (tsum[i] - tsum[j]));
}
int ans = inf;
for (int i = T; i < T + m; i ++) ans = min(ans, f[i]);
cout << ans << endl;
return 0;
}
posted @ 2019-04-30 13:05 chhokmah 阅读(...) 评论(...) 编辑 收藏