[Bzoj1010] packing of toys

Source: Internet
Author: User
Description

Professor P wants to see the Olympics, but he can't keep 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.

Input

Enter two integers n in the first line, L. Then enter ci.1 <= n <=, 1 <= L, CI <= 10 ^ 7

Output

Minimum output cost

Sample input5 4
3
4
2
1
4
Sample output1
[Analysis]

First, the transfer equation for dynamic planning is directly derived: F [I] = f [J] + (sum [I]-sum [J]-C) ^ 2;

It is found that it will obviously time out, and the acceleration slope is optimized by the equation.

Set two vertices, K, J (k <= J), and then create a table to verify the monotonicity ..)

1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <cmath> 5 # include <cstring> 6 # include <algorithm> 7 # include <vector> 8 # include <queue> 9 # include <map> 10 // # define local11 # define ll long long12 const int maxn = 50000 + 5; 13 const int INF = 0x7fffffff; 14 using namespace STD; 15 ll sum [maxn], data [maxn], C; 16 ll f [maxn], Q [maxn]; 17 18 ll g (int K, Int J) 19 {20 // G Function 21 return f [k] + (Sum [k] + C) * (sum [k] + C)-f [J]-(sum [J] + C) * (sum [J] + C); 22} 23 ll s (int K, Int J) {return 2 * (sum [k]-sum [J]);} 24 25 int main () 26 {27 int N; 28 # ifdef local 29 freopen ("data.txt", "r", stdin); 30 freopen ("out.txt ", "W", stdout); 31 # endif32 sum [0] = 0; 33 scanf ("% d % LLD", & N, & C ); 34 For (INT I = 1; I <= N; I ++) 35 {36 scanf ("% LLD", & Data [I]); 37 sum [I] = sum [I-1] + data [I]; 38} 39 // plus spaces in the middle of each toy 40 for (INT I = 1; I <= N; I ++) sum [I] + = I; 4 1 C ++; 42 int L = 0, r = 0; 43 f [0] = 0; Q [R ++] = 0; 44 for (INT I = 1; I <= N; I ++) 45 {46 While (L <R-1 & G (Q [L + 1], Q [l]) <= sum [I] * s (Q [L + 1], Q [l]) l ++; 47 F [I] = f [Q [l] + (sum [I]-sum [Q [l]-C) * (sum [I]-sum [Q [l]-c); 48 Q [R ++] = I; 49 for (Int J = R-2; j> L; J --) 50 {51 LL x, y, z; 52 z = Q [J + 1]; y = Q [J]; X = Q [J-1]; 53 If (! (G (Y, x) * s (z, Y) <g (z, Y) * s (Y, X ))) Q [J] = Q [-- R]; 54 else break; 55 56} 57} 58 printf ("% LLD \ n", F [N]); 59 return 0; 60}

 

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.