记录编号 235144 评测结果 AAAAAAAAAAAAA
题目名称 [CEOI2004]锯木厂选址 最终得分 100
用户昵称 Gravatar一個人的雨 是否通过 通过
代码语言 C++ 运行时间 0.034 s
提交时间 2016-03-10 11:25:43 内存使用 1.07 MiB
显示代码纯文本
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=20000+10;
int opt[maxn],n,m,h=1,t=0;
LL w[maxn],d[maxn],q[maxn];
LL dp[maxn],ans=0x7fffffff,sum[maxn];

int main(){
	freopen("two.in","r",stdin);
	freopen("two.out","w",stdout);
	scanf("%d",&n);
	w[0]=d[0]=0;
	for (int i=1;i<=n;++i){
		int x,y; scanf("%d%d",&x,&y);
		d[i+1]=d[i]+y;
		sum[i]=sum[i-1]+1LL*x*d[i];
		w[i]=w[i-1]+x; 
	}
	dp[0]=0; q[++t]=0;
	for (int i=1;i<=n;++i){
		while (h+1<=t&&(w[q[h+1]]*d[q[h+1]]-w[q[h]]*d[q[h]])<d[i]*(w[q[h+1]]-w[q[h]])) h++;
		int x=q[h]; dp[i]=w[x]*d[x]+w[i]*d[i]-w[x]*d[i]+w[n]*d[n+1]-w[i]*d[n+1]-sum[n];
		q[++t]=i; ans=min(ans,dp[i]);
		for (int j=t-1;j>h;--j){
			int a=q[j+1],b=q[j],c=q[j-1];
			if ((w[a]*d[a]-w[b]*d[b])*(w[b]-w[c])<(w[b]*d[b]-w[c]*d[c])*(w[a]-w[b])) q[j]=q[t--];
			else break;
		}
	}
	printf("%lld",ans);
}