P5785斜率优化DP求调,全WA了qwq,感谢
  • 板块学术版
  • 楼主zhanglh
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/8/20 23:53
  • 上次更新2023/11/3 02:20:55
查看原帖
P5785斜率优化DP求调,全WA了qwq,感谢
726862
zhanglh楼主2023/8/20 23:53
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 300010;

int n, s;
LL t[N], c[N];
LL f[N];
int q[N];

int main()
{
    scanf("%d%d", &n, &s);
    
    for (int i = 1; i <= n; i ++ )
    {
        scanf("%lld%lld", &t[i], &c[i]);
        t[i] += t[i - 1];
        c[i] += c[i - 1];
    }
    
    int hh = 0, tt = 0;
    for (int i = 1; i <= n; i ++ )
    {
        while (hh < tt && (f[q[hh + 1]] - f[q[hh]]) <= (t[i] * s) * (c[q[hh + 1]] - c[q[hh]])) hh ++ ;
        int j = q[hh];
        f[i] = f[j] - (t[i] + s) * c[j] + c[i] * t[i] + c[n] * s;
        while (hh < tt && (f[q[tt]] - f[q[tt - 1]]) * (c[i] * c[q[tt]]) >= (f[i] - f[q[tt]]) * (c[q[tt]] - c[q[tt - 1]])) tt -- ;
        q[ ++ tt] = i;
    }
    
    printf("%lld\n", f[n]);
    
    return 0;
}
2023/8/20 23:53
加载中...