BZOJ1010: [HNOI2008] Toys Toy packing (optimization slope dp)

Description
  Professor P to go to the Olympics, but he Buxia his toys, so he decided to put all the toys shipped to Beijing. Using his own compressor for pressure
reduction, which may be any article into a pile, and then into a special container in one dimension. Professor P has the number N 1 ... N toys, toys after the i-th
compressed into a length dimension Ci. In order to facilitate sorting, Professor P request number in a one-dimensional toy container is continuous. And if a one-dimensional accommodating
a plurality of toy vessel, then between two toy unit length to add a filler, in the form if said i-th to j-th toy toy into a
container, then length of the container will be x = j-i + Sigma ( Ck) i <= K <= j length of the container production costs related to the container, according to research professor,
if the length of the container is x, its production cost (XL) ^ 2. where L is a constant. Do not care about the number of containers P Professor, he can create a container of any length
, and even more than L. But he wants to minimize costs.

Input
  first line of input two integers N, L. The next line N input Ci.1 <= N <= 50000,1 < = L, Ci <= 10 ^ 7

Output
  Output Minimum Cost

Sample Input
5 4

3

4

2

1

4
Sample Output
1
HINT
Source

参考题解:https://blog.csdn.net/qq_38944163/article/details/91369545
思路:
dp[i] = min{dp[j] + sum[i] - sum[j] + i - j - 1 - L}
令a[i] = sum[i] + i; b[i] = sum[i] + i - L - 1
x(i) = b[i], y(i) = dp[i] + b[i] * b[i]。

Finally deformed
dp [j] + b [j ] * b [j] = 2 * a [i] * b [j] + dp [i] -. A [i] * a [i]
i.e. y (j ) = 2 * a [i] * x (j) + dp [i] - a [i] * a [i].
So that the slope k is equal 2 * a [i], the intercept b = dp [i] - a [i] * a [i].

The purpose is to dp [i] minimal, similar to linear programming, you have a straight line, and a bunch of points (previously counted the points j), then j is to find a point to minimize the intercept.

Such is the case with monotonous queue maintains a convex hull point down (inverted-U), only the outermost layer of the corresponding convex hull makes sense, there's a point to choose the minimum guaranteed slope.

The first two teams slope of less than 2 * a [i], then remove the head of the queue, because the straight line certainly will not find a team first. The remaining point is that the point of intersection with the straight line

q [r] q and the slope of [r-1] is larger than q [I] and q [r] slope, here into the recess means, and certainly less than r is selected from this point, the tail out.

Meaning lower maintenance convex hull that is as small as possible candidates for the intercept point only between.

#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
const int maxn = 5e4 + 7;
int q[maxn];
ll n,L;
ll sum[maxn],dp[maxn];

double a(ll i){return sum[i] + i;}
double b(ll i){return sum[i] + i + L + 1;}
double x(ll i){return b(i);}
double y(ll i){return dp[i] + b(i) * b(i);}
double slope(ll i,ll j){return (y(i) - y(j)) / (x(i) - x(j));}

void IN()
{
    scanf("%lld%lld",&n,&L);
    for(int i = 1;i <= n;i++)scanf("%lld",&sum[i]);
    for(int i = 1;i <= n;i++)sum[i] += sum[i - 1];
}

void DP()
{
    int l = 1,r = 1;
    for(int i = 1;i <= n;i++)
    {
        while(l <= r && slope(q[l],q[l + 1]) < 2 * a(i))l++;
        int j = q[l];
        dp[i] = dp[j] + (a(i) - b(j)) * (a(i) - b(j));
        while(l <= r && slope(q[r],q[r - 1]) > slope(i,q[r]))r--;
        q[++r] = i;
    }
    printf("%lld\n",dp[n]);
}

int main()
{
    IN();
    DP();
    return 0;
}

Published 676 original articles · won praise 18 · views 30000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104113256