3437: 小P的牧场

来源:互联网 发布:知乎 青宇是真的吗 编辑:程序博客网 时间:2024/05/01 05:42

3437: 小P的牧场

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 896  Solved: 513
[Submit][Status][Discuss]

Description


 背景

    小P是个特么喜欢玩MC的孩纸。。。

 描述

    小P在MC里有n个牧场,自西向东呈一字形排列(自西向东用1…n编号),于是他就烦恼了:为了控制这n个牧场,他需要在某些牧场上面建立控制站,每个牧场上只能建立一个控制站,每个控制站控制的牧场是它所在的牧场一直到它西边第一个控制站的所有牧场(它西边第一个控制站所在的牧场不被控制)(如果它西边不存在控制站,那么它控制西边所有的牧场),每个牧场被控制都需要一定的花费(毕竟在控制站到牧场间修建道路是需要资源的嘛~),而且该花费等于它到控制它的控制站之间的牧场数目(不包括自身,但包括控制站所在牧场)乘上该牧场的放养量,在第i个牧场建立控制站的花费是ai,每个牧场i的放养量是bi,理所当然,小P需要总花费最小,但是小P的智商有点不够用了,所以这个最小总花费就由你来算出啦。

Input

    第一行一个整数 n 表示牧场数目

    第二行包括n个整数,第i个整数表示ai

    第三行包括n个整数,第i个整数表示bi

 

Output

   只有一行,包括一个整数,表示最小花费

Sample Input

4
2424
3142

Sample Output


9

样例解释

选取牧场1,3,4建立控制站,最小费用为2+(2+1*1)+4=9。

数据范围与约定


对于100%的数据,1<=n<=1000000,0<ai,bi<=10000

HINT

Source

KpmCup#0 By Greens

[Submit][Status][Discuss]

权当复习斜率优化dp咯。。
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<bitset>#include<algorithm>#include<cstring>#include<map>#include<stack>#include<set>#include<cmath>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 1E6 + 10;typedef long long LL;struct Vector{LL x,y; Vector(){}Vector(LL x,LL y): x(x),y(y){}};int n,head,tail,q[maxn];LL a[maxn],b[maxn],c[maxn],f[maxn];Vector vec(int t,int k){return Vector(b[t] - b[k],f[t] + c[t] - f[k] - c[k]);}LL Cross(Vector v1,Vector v2) {return v1.x*v2.y - v2.x*v1.y;}bool Judge(int k,int t,LL i){return (f[t] + c[t] - f[k] - c[k]) <= i*(b[t] - b[k]);}LL Cal(int k,LL i){return f[k] + i*(b[i-1] - b[k]) - (c[i-1] - c[k]) + a[i];}int getint(){char ch = getchar();int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9')ret = ret*10 + ch - '0',ch = getchar();return ret;}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifn = getint();for (int i = 1; i <= n; i++) a[i] = getint();for (int i = 1; i <= n; i++) {b[i] = getint(); c[i] = 1LL*i*b[i];b[i] += b[i-1]; c[i] += c[i-1];}q[head = tail = 1] = 0;for (int i = 1; i <= n; i++) {while (tail > head && Judge(q[head],q[head+1],i)) ++head;f[i] = Cal(q[head],i);while (tail > head && Cross(vec(q[tail],q[tail-1]),vec(i,q[tail])) <= 0) --tail;q[++tail] = i;}cout << f[n];return 0;}

0 0
原创粉丝点击