1096: [ZJOI2007]仓库建设

http://www.cnblogs.com/iwtwiioi/p/4108902.html

这个很详细了。。。。。。。

斜率优化第一题

今天头晕 就偷懒不写题解了。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;


char c;
inline void read(long long &a)
{
	a=0;do c=getchar();while(c<'0'||c>'9');
	while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}
struct Node{long long x,y;inline friend Node operator -(Node a,Node b){return (Node){a.x-b.x,a.y-b.y};}};
Node Stack[1000001];
long long Sum_P[1000001];
long long Sum_To[1000001];
long long Dis[1000001];
long long P[1000001],Build_Cost[1000001];
long long n,m;
long long Con;
long long f[1000001];
inline long long Cross(Node a,Node b)
{
	return a.x*b.y-a.y*b.x;
}
inline long long Cross(Node S,Node E1,Node E2)
{
	return Cross(E1-S,E2-S);
}

int main()
{
	long long i,j,k,l;
	read(n);
	for(i=1;i<=n;i++)
	  {
	    read(Dis[i]),read(P[i]),read(Build_Cost[i]);
	    Sum_P[i]=Sum_P[i-1]+P[i];
	    Sum_To[i]=Sum_To[i-1]+Sum_P[i-1]*(Dis[i]-Dis[i-1]);
	  }
	  i=1;
	Stack[++Con]=(Node){0,0};
	long long tempk;
	Node Temp;
	j=1;
	for(i=1;i<=n;i++)
	 {
	 	tempk=Dis[i];
		while(j^Con)
		     if(Stack[j+1].y-Stack[j].y < tempk*(Stack[j+1].x-Stack[j].x))j++;
		     else break;
           f[i]=Stack[j].y-tempk*Stack[j].x+Build_Cost[i]+Sum_To[i];
	   f[i]=min(f[i],f[i-1]+Build_Cost[i]);
	   Temp=(Node){Sum_P[i-1],Sum_P[i-1]*Dis[i]+f[i-1]-Sum_To[i]};
	   while(j^Con)
	     if(Cross(Stack[Con-1],Stack[Con],Temp)<=0)Con--;
	     else break;
	  Stack[++Con]=Temp;
	 }
	 printf("%lld\n",f[n]);
	return 0;
}


你可能感兴趣的:(dp,bzoj)