【斜率优化】[BZOJ3437] 小P的牧场
2014-12-15 13:06
309 查看
模板题,不用多说,只不过本地测,cin 2.6s,scanf 0.6s过。确实数据大了还是少用cin.
#include<cstdio> #include<algorithm> #include<cmath> #include<iostream> #include<cstring> #define MAXN 1000000 using namespace std; #define gx(i) (sum[i]) #define gy(i) (d[i] + sumt[i]) typedef long long LL;//!!!! int a[MAXN +10]; int b[MAXN +10]; int que[MAXN +10]; LL sumt[MAXN +10];//t[i] = b[i] * i; LL sum[MAXN +10]; LL d[MAXN +10]; LL get_d(LL i,LL j) { return d[j] + i * (sum[i] - sum[j]) - (sumt[i] - sumt[j]) + a[i]; } int main() { //freopen("farm.in","r",stdin); //freopen("farm.out","w",stdout); LL i,f,r,x1,x2,y1,y2,n; cin >> n; for(i = 1;i <= n;i++) scanf("%d",&a[i]); for(i = 1;i <= n;i++) scanf("%d",&b[i]); for(i = 1;i <= n;i++) { sumt[i] = sumt[i - 1] + b[i] * i; sum[i] = sum[i - 1] + b[i]; } f = r = que[0] = 0; for(i = 1;i <= n;i++) { LL tmp = i - 1; while(f < r) { x1 = gx(que[r]) - gx(que[r-1]); y1 = gy(que[r]) - gy(que[r-1]); x2 = gx(tmp) - gx(que[r]); y2 = gy(tmp) - gy(que[r]); if(y1 * x2 >= y2 * x1) r--; else break; } que[++r] = tmp; while(f < r && get_d(i,que[f]) > get_d(i,que[f + 1])) f++; d[i] = get_d(i,que[f]); } cout << d << endl; //printf("%d\n",d ); } /* 4 2 4 2 4 3 1 4 2 */
相关文章推荐
- bzoj 3437: 小P的牧场【斜率优化】
- bzoj3437小P的牧场(斜率优化)
- 【BZOJ3437】小P的牧场 斜率优化
- 【BZOJ-3437】小P的牧场 DP + 斜率优化
- BZOJ3437 小P的牧场 斜率优化
- BZOJ 3437 小P的牧场(斜率优化DP)
- BZOJ 3437: 小P的牧场 斜率优化DP
- 【BZOJ3437】小P的牧场 斜率优化 动态规划
- 【bzoj3437】小P的牧场 斜率优化dp
- [BZOJ3437]小P的牧场(斜率优化dp)
- [bzoj3437][dp+斜率优化]小P的牧场
- 【bzoj3437】【小p的牧场】【斜率优化dp】
- 【bzoj3437】小P的牧场(斜率优化dp)
- BZOJ 3437: 小P的牧场 斜率优化dp
- bzoj 3437: 小P的牧场 -- 斜率优化
- BZOJ 3437 小P的牧场 斜率优化DP
- bzoj 3437: 小P的牧场(斜率优化DP)
- [BZOJ 3437]小P的牧场:DP斜率优化
- 【动态规划24】bzoj3437小P的牧场(dp+斜率优化)
- [BZOJ3437] 小P的牧场 && 斜率优化