Codeforces 311B Cats Transport【斜率優化DP】
阿新 • • 發佈:2018-11-10
題目大意
有一些貓,放在一些位置,人一步移動一個位置
給出每個貓出現的時間,每個人可以自由安排其出發時間,沿途已經出現的貓撿起,貓等待的時間是被減去的時間減去出現的時間
貓可以等人,人不能等貓
現在有P個人,問貓等待的總時間T最小是多少。
思路
首先可以算出要撿起每個貓最早出發的時間是多少\(c_i\)
然後按照這個值排序,每個人撿起的貓一定是在這個值上連續的一段
然後計算出如果一個人選\([l,r]\),等待時間到達每個貓的時間減去到個貓最小的時間,是\(c_r*(r-l+1)-(s_r-s_{l-1})\)
其中s是c的字首和,為什麼呢?因為c是剛好撿起沒個貓的時間,所以\(c_r-c_i\)
然後就變成了\(dp_{i,j}=dp_{k,j-1}+c_i*(i-k)-(s_i-s_k)\)
轉化一下發現\(x'=k,k'=c_i,y'=dp_{k,j-1}+s_k,b'=dp_{i,j}+s_i-i*c_i\)
斜率單調遞增,最小化截距,直接維護下凸殼就可以了
//Author: dream_maker #include<bits/stdc++.h> using namespace std; //---------------------------------------------- //typename typedef long long ll; typedef pair<int, int> pi; //convenient for #define fu(a, b, c) for (int a = b; a <= c; ++a) #define fd(a, b, c) for (int a = b; a >= c; --a) #define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a) //inf of different typename const int INF_of_int = 1e9; const ll INF_of_ll = 1e18; //fast read and write template <typename T> void Read(T &x) { bool w = 1;x = 0; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') w = 0, c = getchar(); while (isdigit(c)) { x = (x<<1) + (x<<3) + c -'0'; c = getchar(); } if (!w) x = -x; } template <typename T> void Write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) Write(x / 10); putchar(x % 10 + '0'); } //---------------------------------------------- //c_i = t_i - d_i //s_i = \sum_{j=1}^i c_j //dp[i][j] = min(dp[k][j - 1] + c_i * (i - k) - (s_i - s_k) const int N = 1e5 + 10; const int K = 1e2 + 10; ll n, m, p; ll d[N], s[N], c[N]; ll dp[N][K]; struct Node {ll x, y;} q[N]; ll operator * (const Node a, const Node b) { return a.x * b.y - a.y * b.x; } Node operator - (const Node a, const Node b) { return (Node){a.x - b.x, a.y - b.y}; } ll calc(Node a, ll pos) { return a.y - s[pos] + c[pos] * (pos - a.x); } int main() { freopen("input.txt", "r", stdin); Read(n), Read(m), Read(p); fu(i, 2, n) Read(d[i]), d[i] += d[i - 1]; fu(i, 1, m) { int t, h; Read(h), Read(t); c[i] = t - d[h]; } sort(c + 1, c + m + 1); fu(i, 1, m) { s[i] = s[i - 1] + c[i]; dp[i][1] = - s[i] + i * c[i]; } int l, r; fu(j, 2, p) { q[l = r = 1] = (Node) {0ll, 0ll}; fu(i, 1, m) { while (l < r && calc(q[l], i) >= calc(q[l + 1], i)) ++l; dp[i][j] = calc(q[l], i); Node now = (Node){i, dp[i][j - 1] + s[i]}; while (l < r && (q[r] - now) * (q[r - 1] - now) > 0) --r; q[++r] = now; } } Write(dp[m][p]); return 0; }