题解
转移方程与我的上一篇题解一样 : $S\times sumC_j + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$。
分离成:$S\times sumC_j + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$
不同的是, 时间可能为 负数(出题人解释:不要把时间看的这么狭义。
所以$sumT_i$不是递增。
所以我们不能在队首弹出斜率比 $sumT_i$小的数, 只能用一个数据结构来维护并查询, 我当然是用了好玩的set
但是队尾还是需要维护下凸壳。
有一个坑点:用叉积来弹出队尾可能爆longlong, 开double可以过QAQ
代码
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#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;
#define ll long long
typedef pair<double, ll>P; const ll N = 1e6;
const ll inf = 1e18;
const double eps = 1e-; ll S, sumT[N], sumC[N], q[N], n;
ll f[N]; set<P>st; ll read() {
ll X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} ll cross(ll a, ll b, ll c) { //叉积
ll ax = sumC[a], bx = sumC[b], cx = sumC[c];
ll ay = f[a], by = f[b], cy = f[c];
ll x = bx - ax, y = by - ay, xx = cx - ax, yy = cy - ay;
if(fabs(1LL * x * yy - 1LL * xx * y) < eps) return ;
return (long double)x * yy > (long double)xx * y;
} double calc(ll a, ll b) {
ll ax = sumC[a], bx = sumC[b];
ll ay = f[a], by = f[b];
if(bx - ax == ) return by > ay ? inf : -inf;
else return 1.0 * (by - ay) / (bx - ax);
} int main()
{
n = rd; S = rd;
rep(i, , n) {
ll t = rd, c = rd;
sumT[i] = sumT[i - ] + t;
sumC[i] = sumC[i - ] + c;
}
ll l = , r = ;
q[] = f[] = ;
set<P>::iterator it;
st.insert(P(-inf, ));
rep(i, , n) {
double k = S + sumT[i];
P fd = P(k, );
it = st.lower_bound(fd);//查找最小的斜率比sumT[i]大的
it--;
ll p = (*it).second;
f[i] = f[p] + 1LL * sumT[i] * (sumC[i] - sumC[p]) + 1LL * S * (sumC[n] - sumC[p]);
while(l < r && cross(q[r - ], q[r], i) == ) {
st.erase(P(calc(q[r - ], q[r]), q[r]));
r--;
}
q[++r] = i;
st.insert(P(calc(q[r - ], q[r]), i));
}
printf("%lld\n", f[n]);
fclose(stdin); fclose(stdout);
}