[BZOJ3437] 小P的牧场 && 斜率优化

来源:互联网 发布:联通手机信号测试软件 编辑:程序博客网 时间:2024/05/01 12:25

如果你搞了前面的斜率优化的题 那么这道题应该不算难  考的时候脑残推错了 说不出话来  BZOJ数据显示也是坑 明明100W 后面加个,0 关键是我开1000W会直接T 坑

#include<cstdio>using namespace std;typedef long long LL;const int MAXN = 1000000;LL d[MAXN+10], Q[MAXN+10], Cost[MAXN+10], F, R, S[MAXN+10], T[MAXN+10];int n, m;inline LL Slope_U(int i, int j){return d[i] + T[i] - (d[j] + T[j]);}inline LL Slope_D(int i, int j){return S[i] - S[j];}inline LL read(){     LL x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}return x*f;}int main(){scanf("%d", &n);for(int i = 1; i <= n; i++) Cost[i] = read();for(int i = 1; i <= n; i++) {LL x = read();S[i] = S[i-1] + x;T[i] = T[i-1] + x * i;}F = R = Q[0] = 0;Q[R++] = 0;for(int i = 1; i <= n; i++){while(F < R-1 && Slope_U(Q[F+1], Q[F]) <= Slope_D(Q[F+1], Q[F]) * i)F++;d[i] = d[Q[F]] + Cost[i] + i * (S[i] - S[Q[F]]) - (T[i] - T[Q[F]]); while(F < R-1 && Slope_U(Q[R-1], Q[R-2]) * Slope_D(i, Q[R-1]) >= Slope_U(i, Q[R-1]) * Slope_D(Q[R-1], Q[R-2]))R--;Q[R++] = i;}printf("%lld\n", d[n]);}/*42 4 2 43 1 4  2*/


0 0
原创粉丝点击