# 题面在这里

## 题意

$n$个牧场，自西向东呈一字形排列，需要控制这些牧场，

## 数据范围

$1\le n\le10^5, 0 <a_i,b_i\le10^4$

## sol

$f[i]$表示在第$i$个牧场建立一个控制站并控制之前所有牧场的最小费用，有

$f[i]=min_{j=0}^{i-1}(f[j]+\sum_{k=j+1}^{i}b[k](i-k)+a[i])$

$c[i]=\sum_{j=1}^{i}(b[j]\times j)$$d[i]=\sum_{j=1}^{i}b[j]$，有

$=min_{j=0}^{i-1}(f[j]-\sum_{k=j+1}^{i}(b[k]\times k)+i\times\sum_{k=j+1}^{i}b[k]+a[i])$

$=min_{j=0}^{i-1}(f[j]-(c[i]-c[j])+i\times(d[i]-d[j])+a[i])$

$=min_{j=0}^{i-1}(f[j]+c[j]-i\times d[j])+a[i]+i\times d[i]-c[i]$

## 代码

#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define mp make_pair
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const dd eps=1e-10;
const int mod=1e8;
const int N=1000010;
RG ll data=0,w=1;RG char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
return data*w;
}

il void file(){
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}

ll n,a[N],b[N],c[N],d[N],f[N];
struct node{ll x,y;}Q[N];ll L=1,R;
il void insert(node q){
while(L<R&&(Q[R].y-Q[R-1].y)*(q.x-Q[R].x)>=(q.y-Q[R].y)*(Q[R].x-Q[R-1].x))R--;
Q[++R]=q;
}
il ll query(ll k){
while(L<R&&k*(Q[L+1].x-Q[L].x)>=Q[L+1].y-Q[L].y)L++;
return Q[L].y-k*Q[L].x;
}

int main()
{
for(RG int i=1;i<=n;i++){
}

insert((node){0,0});
for(RG int i=1;i<=n;i++){
f[i]=query(i)+a[i]+i*d[i]-c[i];
insert((node){d[i],f[i]+c[i]});
}

printf("%lld\n",f[n]);

return 0;
}


posted @ 2018-03-26 20:41  cjfdf  阅读(91)  评论(0编辑  收藏  举报