Two Sawmills(鋸木廠選址)
斜率優化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所以,這相當於一條斜率已知的直線向上移動直到與之前任意一個點
#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;
}