Bzoj 1010 toy (quadrilateral inequality optimization DP) (hnoi 2008)

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

 

Question: omitted.

Train of Thought: From the question, w (I, j) = (J-I + sum {C [I. J]}-l) ^ 2

According to the statement in the black book (algorithm art and informatics competition), to satisfy the Quadrilateral inequality, you only need:

① When J is not changed, f (I) = W (I, j + 1)-W (I, j) decreases monotonically.

② When I is not changed, F (j) = W (I + 1, J)-W (I, j) decreases monotonically.

When and only when ① ② is set up, W complies with the Quadrilateral inequality (this is a good verification in this question, so we will not write it ).

At this time, the decision is monotonous (although I don't know why ......), The solution can be referred to "1D/1D Dynamic Planning Optimization preliminary". My solution is based on this.

 

In addition, there is slope optimization writing (although I will not), you can refer to the http://blog.sina.com.cn/s/blog_5f5353cc0100jx41.html

But when I run such a set of data (randomly generated ):

50 56913588467214 9692279 8158876 7023812 4301767 4313397 2378849 778746 2288987 3052905 7228328 6855441 4280591 1058424 889808 530342 9432467 761829 3969985 3193649 8070892 711348 5821773 3062537 6891494 5695393 3803579 6418375 1578074 2363659 6477728 533247 6025318 8151154 2442635 1113076 506233 3594564 8277434 1584861 2276145 4442621 7105356 8729441 7283702 1250929 5133264 9233541 5267813 2889179

The answer is different from the result 136165306871056 that my program ran out. A little visual, the above slope optimization code is out of bounds (I don't know how to change it ......)

The limit of w (I, j) can be as high as (50000-1 + 50000*10 ^ 7-0) ^ 2, it exceeds the 64-bit integer range (although it does not seem to have such big data ).

 

After the code is modified, there should be no cross-border issues (to avoid multiplication of two large numbers, you can refer to the check function in the Code ).

PS: The answer will certainly not be greater than 50000*(10 ^ 7-0) ^ 2 = 5*10 ^ 18 (that is, a container for every toy), a little smaller than 2 ^ 63-1.

 

Code (288 ms ):

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 typedef long long LL; 7  8 const int MAXN = 50010; 9 10 LL sum[MAXN], dp[MAXN];11 int c[MAXN], n, L;12 13 LL sqrt_cost(int i, int j) {14     return j - i + sum[j] - sum[i - 1] - L;15 }16 17 LL cost(int i, int j) {18     LL res = sqrt_cost(i, j);19     return res * res;20 }21 /*22 bool check(int pos, int a, int b) {23     return dp[a] + cost(a + 1, pos) >= dp[b] + cost(b + 1, pos);24 }25 */26 bool check(int pos, int a, int b) {27     LL aa = dp[a], bb = sqrt_cost(a + 1, pos), cc = dp[b], dd = sqrt_cost(b + 1, pos);28     return (cc - aa - 1) / (bb - dd) + 1 <= bb + dd;29 }30 31 int binary_search(int st, int ed, int a, int b) {32     int l = st, r = ed;33     while(l < r) {34         int mid = (l + r) >> 1;35         if(!check(mid, a, b)) l = mid + 1;36         else r = mid;37     }38     return l;39 }40 41 struct Node {42     int pos, best;43     Node() {}44     Node(int pos, int best): pos(pos), best(best) {}45 } stk[MAXN];46 int top;47 48 LL solve() {49     int v = 1;50     stk[++top] = Node(1, 0);51     for(int i = 1; i <= n; ++i) {52         if(v < top && stk[v + 1].pos <= i) ++v;53         int p = stk[v].best;54         dp[i] = dp[p] + cost(p + 1, i);55 56         while(v < top && check(stk[top].pos, stk[top].best, i))57             --top;58         int r = stk[top + 1].pos ? stk[top + 1].pos + 1 : n + 1;59         int t = binary_search(max(stk[top].pos, i) + 1, r, stk[top].best, i);60         if(t <= n) stk[++top] = Node(t, i);61     }62     return dp[n];63 }64 65 int main() {66     scanf("%d%d", &n, &L);67     for(int i = 1; i <= n; ++i) scanf("%d", &c[i]);68     for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + c[i];69     cout<<solve()<<endl;70 }
View code

 

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.