题解

  将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$

  把方程进行分离, 得到 $S\times sumC_j  + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$。

   将等号左边看成纵坐标, $sumC_j$看成横坐标, $sumT_i$为斜率来进行斜率优化。

  由于 $sumT_i$是递增的, 即斜率是递增的, 维护一个单调队列, 第一个斜率大于$sumT_i$的端点就为决策点

  斜率优化dp还是很套路的

代码

  

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define rd read()
#define rep(i,a,b) for( int i = (a); i <= (b); ++i )
#define per(i,a,b) for( int i = (a); i >= (b); --i )
using namespace std; const int N = 1e4 + 1e3; int n, m, sumT[N], sumC[N], S, f[N], q[N]; int cross(int a, int b, int c) { //点积
int ax = sumC[a], bx = sumC[b], cx = sumC[c];
int ay = f[a], by = f[b], cy = f[c];
int x = bx - ax, xx = cx - ax, y = by - ay, yy = cy - ay;
return x * yy - xx * y;
}// 向量ab, ac double calk(int a, int b) {
int ax = sumC[a], bx = sumC[b], ay = f[a], by = f[b];
return 1.0 * (by - ay) / (bx - ax);
} int read() {
int X = , p = ;char c = getchar();
for(; c > '' || c < ''; c = getchar() ) if( c == '-' ) p = -;
for(; c >= '' && c <= ''; c = getchar() ) X = X * + c - '';
return X * p;
} int main()
{
n = rd; S = rd;
rep(i, , n) {
int t = rd, c = rd;
sumT[i] = sumT[i - ] + t;
sumC[i] = sumC[i - ] + c;
}
memset(f, 0x3f, sizeof(f));
int l = , r = ;
q[] = f[] = ;
rep(i, , n) {
while(l < r && calk(q[l], q[l + ]) <= S + sumT[i]) l++;
if(l <= r) f[i] = f[q[l]] + sumT[i] * (sumC[i] - sumC[q[l]]) + S * (sumC[n] - sumC[q[l]]);
while(l < r && cross(q[r - ], q[r], i) <= ) r--;
q[++r] = i;
}
printf("%d\n",f[n]);
}
05-11 20:52