[bzoj1010][HNOI2008] Toy boxing toy_slope optimization dp

Toy box toy bzoj-1010 HNOI-2008

    The gist of the title: Professor P wants to go to the Olympics, but he can't let go of his toys, so he decides to ship all the toys to Beijing. He compresses with his own compressor, which can turn any item into a pile and put it into a special one-dimensional container. Professor P has N pieces of toys numbered 1...N, and the i-th toy is compressed into a one-dimensional length of Ci. In order to facilitate sorting, Professor P requires that the toy numbers in a one-dimensional container are consecutive. At the same time, if there are multiple toys in a one-dimensional container, a filler of unit length should be added between the two toys. The length will be x=j-i+Sigma(Ck) i<=K<=j The cost of making the container is related to the length of the container. According to the professor's research, if the length of the container is x, the cost of making the container is (XL)^2. where L is a constant. Professor P doesn't care about the number of containers, he can make containers of any length, even more than L. But he wants the cost to be minimal.

    注释 : $ 1 \ le n \ le 5 \ cdot 10 ^ 4 $ , $ 1 \ le L, c_i \ le 10 ^ 7 $。

      Idea: Apparently dp. The state is particularly simple, and so is the transition.

        Status: dp[i] represents the minimum cost of packing the first i.

        转移:$dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j)^2),1\le j \le i-1$。

      Then, it's obvious that you can't get through. Here we introduce an algorithm for optimizing dp: slope optimization.

      That is to say, I put a dp state transition problem of 1D1D type into a straight line, where y=kx+b, b is the current state that needs to be transferred, and k is a constant (here, everything except and The variables related to dp[i] and related to i can be regarded as constants). Let's first calculate all the points from 1 to i-1, from the calculation rules of y and x in the analytical formula of the straight line through your column, we can get i-1 points, and throw these i-1 points into Coordinate System. Then, I designate the i-1 points one by one, and pass a straight line with a slope of k through the designated points. In this way, an intercept will be obtained, and if there is a question, you can know whether it is the largest intercept or the smallest intercept. Then, taking the maximum intercept as an example, we first ensure that all transition equations are positive or negative for k in any case. Then we get the point j that directly transfers to dp[i] (because it is taking the minimum value, here is a solution that satisfies the minimum value), obviously, the meaning of the question must not be satisfied after that, So we can delete it... , and these similar operations can be implemented with... monotonic queues. Specifically, you can directly look at the code...  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=50100;
ll s[N],Q[N],f[N],n,x,head,L,tail,j;
inline double X(ll i)//Return the abscissa of a single point
{
	return s[i];
}
inline double Y(ll i)//Return the ordinate of a single point
{
	return f[i]+(s[i]+L-1)*(s[i]+L-1);
}
inline double Rate(ll i,ll k)//Return the slope of two points
{
	return (Y(k)-Y(i))/(X(k)-X(i));
}
intmain()
{
	scanf("%lld%lld",&n,&L);
	L++;
	head=tail=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&s[i]);
		s[i]+=s[i-1];
	}
	for(int i=1;i<=n;i++)
	{
		s[i]+=i;
	}
	for(int i=1;i<=n;i++)
	{
		while(!(head>=tail)&&Rate(Q[head],Q[head+1])<2*s[i])head++;//维护单调队列
		j=Q[head];f[i]=f[j]+(s[i]-s[j]-L)*(s[i]-s[j]-L);
		while (!(head>=tail)&&Rate(Q[tail-1],Q[tail])>Rate(Q[tail],i))
			tail--;Q[++tail]=i;//Maintain monotonic queue
	}
	printf("%lld\n",f[n]);
	return 0;
}

 

    Due to the accidental stumbling around the world (the total of hand-white errors) , all blogs will not write errors in the future

    Summary: The role of slope optimization is to optimize dp qwq

     I recommend a blog to you, which is super detailed. http://www.cnblogs.com/Paul-Guderian/p/7259491.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324946087&siteId=291194637