Two Sawmills(锯木厂选址)

http://main.edu.pl/en/archive/ceoi/2004/two


斜率优化DP,应该说是第一道斜率优化DP了,推公式的时候各种坑,还是参照了hzq神牛的思路,细节方面稍有不同,

为了思维方便,我先将给出的序列翻转了,也就是把从山顶到山下的点顺序边成了从山下到山顶,编号从1开始,第一个点即为海拔最低的伐木场,所以共有n+1个点,w[i]表示第i个点的重量,dist[i]表示第i个点到第一个点的距离,dp[i]表示把第二个伐木场建到第i个点的最优解

则有 

         dp[i] = min(S[1, j-1]+S[j, i-1]+S[i, n+1])    1

其中  S[l, r] = ∑w[i]*(dist[i]-dist[i])  (l <= i <= r)  2 

再设sw[i] = ∑ w[j] (1 <= j <= i),                         3

        swd[i] = ∑ w[j]*dist[j] (1 <= j <= i)             4

则有 S[l, r] = swd[r]-swd[l-1]-(sw[r]-sw[l-1])*dist[l];

带入1式后化简可得

dp[i] = min(swd[n+1]-swd[0]-(sw[n+1]-sw[i-1])*dist[i]-(sw[i-1]-sw[j-1])*dist[j]-(sw[j-1]-sw[0])*dist[1])

由于dist[1]等于0,所以

dp[i] = min(-(sw[i-1]-sw[j-1])*dist[j])+swd[n+1]-swd[0]-(sw[n+1]-sw[i-1])*dist[i]

以为后面的部分是与j无关的常量,所以不会影响决策,可以忽略掉

所以要求解只剩min((-sw[i-1])*dist[j]+sw[j-1]*dist[j])了

由于-sw[i-1]在第i轮决策中可以看成常数记为a,用x表示dist[j],y表示sw[j-1]*dist[j],

则优化目标G = min(-ax+y),设x*,y*为最优解(注意最优解实际上只有j决定)

则有G = -ax*+y*,移项后可得y* = ax*+G,由于a > 0所以,这相当于一条斜率已知的直线向上移动直到与之前任意一个点(x, y) 想切,此时的到的G是最优解,不难发现最优决策点构成一条下凸线,而且由于a单调递增,而下凸线的斜率也是单调递增的,所以可以舍弃当前最优决策之前的所有点,因为这些点都不可能成为之后决策的最优点了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
typedef long double LF;

const int MAXN(20010);
const int MAXM(100010);
const int MAXE(100010);
const int MAXK(6);
const int HSIZE(31313);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(20100403);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> T ABS(const T &a){return a < 0? -a: a;}

LL X[MAXN], Y[MAXN];
int que[MAXN];
int front, back;
LL dist[MAXN], sw[MAXN];

int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		++n;  //一共n+1个点,下标1从开始
		LL swd = 0;
		for(int i = n; i >= 2; --i)
			scanf("%I64d%I64d", sw+i, dist+i);
		for(int i = 1; i <= n; ++i)
		{
			dist[i] += dist[i-1];
			swd += dist[i]*sw[i];
			sw[i] += sw[i-1];
		}
		for(int i = 1; i <= n; ++i)
		{
			X[i] = dist[i];
			Y[i] = sw[i-1]*dist[i];
		}
		LL ans = 1e11;
		front = 0;
		back = -1;
		que[++back] = 1;
		que[++back] = 2; 
		for(int i = 3; i <= n; ++i)
		{
			while(back-front > 0 && (-sw[i-1])*X[que[front+1]]+Y[que[front+1]] <= (-sw[i-1])*(X[que[front]])+Y[que[front]]) ++front;
			checkmin(ans, swd-(sw[n]-sw[i-1])*dist[i]+((-sw[i-1])*(X[que[front]])+Y[que[front]]));
			while(back-front > 0 && (Y[i]-Y[que[back-1]])*(X[i]-X[que[back]]) >= (Y[i]-Y[que[back]])*(X[i]-X[que[back-1]])) --back;
			que[++back] = i;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值