标签:\n cst clu ios 装箱 har 思路 style ||
思路:这道题的原始dp方程貌似都给在题里了。。。纯粹按模板敲个斜率优化就A过去了。。
#include<cstdio> #include<iostream> using namespace std; const int maxn = 50010; inline long long qread(){ register int ch = getchar(); register long long x = 0; while(ch < ‘0‘ || ch > ‘9‘) ch = getchar(); while(ch >= ‘0‘ && ch <= ‘9‘) x = (x << 3) + (x << 1) + ch - 48, ch = getchar(); return x; } long long C[maxn]; long long sum[maxn]; long long f[maxn]; int n; long long L; long long qu[maxn << 5]; int l = 1, r = 1; inline long long fy(int x){ return f[x] + (x + sum[x]) * (x + sum[x]); } inline long long fx(int x){ return x + sum[x]; } int main(void){ scanf("%d", &n); L = qread(); for(int i = 1; i <= n; ++i){ C[i] = qread(); sum[i] = sum[i - 1] + C[i]; } for(int i = 1; i <= n; ++i){ while(l < r && (fy(qu[l + 1]) - fy(qu[l])) < ((i + sum[i] - 1 - L) << 1) * (fx(qu[l + 1]) - fx(qu[l]))) ++l; f[i] = f[qu[l]] + (i - qu[l] - 1 + sum[i] - sum[qu[l]] - L) * (i - qu[l] - 1 + sum[i] - sum[qu[l]] - L); while(l < r && (fy(qu[r]) - fy(qu[r - 1])) * (fx(i) - fx(qu[r])) > (fy(i) - fy(qu[r])) * (fx(qu[r]) - fx(qu[r - 1]))) --r; qu[++r] = i; } printf("%lld\n", f[n]); }
标签:\n cst clu ios 装箱 har 思路 style ||
原文地址:https://www.cnblogs.com/junk-yao-blog/p/9531264.html