[Dynamic Planning] packing toys

Source: Internet
Author: User
Professor descriptionp is going to see the Olympics, but he can't leave his toys, so he decided to transport all the toys to Beijing. He uses his own compress to compress, which can convert any item into a pile and put it in a special one-dimensional container. Professor P has the number 1... N pieces of N toys, I pieces of toys after compression into a one-dimensional length CI. to facilitate sorting, Professor P asked that the number of toys in a one-dimensional container be continuous. At the same time, if a one-dimensional container contains multiple toys, a unit-length padding should be added between the two toys, in the form of placing the I toys into the J toys in a container, the container length will be X = J-I + sigma (CK) I <= k <= j the cost of creating a container is related to the length of the container. According to Professor's research, if the length of the container is X, the cost of making the container is (X-L) ^ 2. L is a constant. Professor P does not care about the number of containers. He can make containers of the specified length, or even exceed L. But he wants the minimum fee. the first line of input is two integers n, L. enter ci.1 <= n <= 434214, 1 <= L, CI <= 10 ^ 7output output minimum cost sample input5 sample output1 in the next n rows

Set the S array as the prefix and make G [I] = I + s [I].

Simple equation: F [I] = max {f [J] + sqr (G [I]-G [J]-l-len )}.
Set J to the current optimal decision, and K to the right of J. through simplification, the slope inequality can be obtained:
F [J]-f [k]
------- + G [J] + G [k] + 2len + 2> = 2G [I]
G [J]-G [k]
Let the left side of the inequality be H (j, k ).

You only need to use a dual-end queue to maintain a decision sequence with an increasing h value (lower convex function.
Accode:

#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>const char fi[] = "toy.in";const char fo[] = "toy.out";const int maxN = 50010;const int MAX = 0x3f3f3f3f;const int MIN = ~MAX;typedef long long int64;int64 F[maxN], g[maxN], s[maxN], len;int q[maxN], n, f, r;void init_file(){    freopen(fi, "r", stdin);    freopen(fo, "w", stdout);    return;}inline int getint(){    int res = 0; char tmp;    while (!(isdigit(tmp = getchar())));    do res = (res << 3) + (res << 1) + tmp - '0';    while ((isdigit(tmp = getchar())));    return res;}void readdata(){    n = getint(); len = getint();    for (int i = 1; i < n + 1; ++i)    {        (s[i] = getint()) += s[i - 1];        g[i] = i + s[i];    }    return;}#define sqr(x) ((x) * (x))#define check(j, k, i) \(F[j] - F[k] + (g[j] + g[k] + \(len << 1) + 2) * (g[j] - g[k]) \<= (g[i] * (g[j] - g[k]) << 1))#define cmp(j, k, i) \((F[j] - F[k] + (g[j] + g[k] + \(len << 1) + 2) * (g[j] - g[k])) \* (g[k] - g[i]) \<= (F[k] - F[i] + (g[k] + g[i] + \(len << 1) + 2) * (g[k] - g[i])) \* (g[j] - g[k]))void work(){    f = 0, r = 1; //    for (int i = 1; i < n + 1; ++i)    {        while (f < r - 1 && !check(q[f], q[f + 1], i)) ++f;        F[i] = F[q[f]] + sqr(g[i] - g[q[f]] - 1 - len);        while (f < r - 1 && !cmp(q[r - 2], q[r - 1], i)) --r;        q[r++] = i;    }    printf("%I64d\n", F[n]);    return;}int main(){    init_file();    readdata();    work();    return 0;}#undef sqr#undef check#undef cmp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.