"Slope" Toy packing

Source: Internet
Author: User

Test instructions

There are n toys to be boxed, the length of each toy is c[i], stipulated in the packing, must be in strict accordance with the order given, and any two toys in the same box must and only one unit length, in other words, if you want to load a box numbered I~j toys, The length of the box must be only l=j-i+sigma[c[k]], the cost of each box with a length of L is P = (l-l) 2, where l is a given constant. You are now required to pack all your toys at the least cost, and the number of boxes does not matter.

Ideas:

Dp[i]=min (dp[j]+ (sum[i]-sum[j]+i-j-1-l) ^2) (J<i)

Make F[i]=sum[i]+i,c=1+l

Then Dp[i]=min (dp[j]+ (f[i]-f[j]-c) ^2)

1. Proving monotonicity of decisions

K-decision-making and J-decision based on state I, i.e.

dp[k]+ (f[i]-f[k]-c) ^2<=dp[j]+ (f[i]-dp[j]-c) ^2

For a post-state t,dp[t]=dp[i]+v;

To prove dp[k]+ (f[t]-f[k]-c) ^2<=dp[j]+ (f[t]-f[j]-c) ^2

As long as the card

dp[k]+ (f[i]+v-f[k]-c) ^2<=dp[j]+ (f[i]+v-f[j]-c) ^2

As long as the card

dp[k]+ (f[i]-f[k]-c) ^2+2*v* (f[i]-f[k]-c) +v^2<=dp[j]+ (f[i]-f[j]-c) ^2+2*v* (f[i]-f[j]-c) +v^2

As long as the card

2*v* (f[i]-f[k]-c) <=2*v* (f[i]-f[j]-c)

That is f[k]>=f[j] (obviously)

Proof complete.

2. Calculate the slope equation

Because dp[k]+ (f[i]-f[k]-c) ^2<=dp[j]+ (f[i]-f[j]-c) ^2

Expand

dp[k]+f[i]^2-2*f[i]* (f[k]+c) + (f[k]+c) ^2<=dp[j]+f[i]^2-2*f[i]* (f[j]+c) + (f[j]+c) ^2

That

dp[k]-2*f[i]* (f[k]+c) + (f[k]+c) ^2<=dp[j]-2*f[i]* (f[j]+c) + (f[j]+c) ^2

i.e. (dp[k]+ (f[k]+c) ^2-dp[j]-(f[j]+c) ^2)/2* (F[k]-f[j]) <=f[i]

F[i] is monotonically increasing, we use the queue to maintain a lower convex hull, each time we take out the team head as a decision

When I join decision I, the tail of the team is q[r], the previous one is q[r-1]

When the slope (q[r],i) < slope (Q[r-1],q[r]) is met, it is clear that the end of the team is not valid and will eject (turn from)

Code:

1#include <map>2#include <Set>3#include <ctime>4#include <cstdio>5#include <cstring>6#include <vector>7#include <cstdlib>8#include <iostream>9#include <algorithm>Ten #defineINF 1000000000 One #definell Long Long A using namespacestd; - ll Read () - { thell x=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; + } - intN,l,l,r; + intc[50005],q[50005]; All s[50005],f[50005],c; at DoubleSlopintJintk) - { -     return(f[k]-f[j]+ (s[k]+c) * (s[k]+c)-(s[j]+c) * (S[J]+C))/(2.0* (s[k]-s[j])); - } - voidDP () - { inL=1; r=0; q[++r]=0; -      for(intI=1; i<=n;i++) to     { +          while(L<r&&slop (q[l],q[l+1]) <=s[i]) l++; -         intt=Q[l]; thef[i]=f[t]+ (s[i]-s[t]-c) * (s[i]-s[t]-C); *          while(L<r&&slop (Q[r],i) <slop (q[r-1],Q[R]) r--; $q[++r]=i;Panax Notoginseng     } - } the intMain () + { AN=read (); L=read (); c=l+1; the      for(intI=1; i<=n;i++) c[i]=read (); +      for(intI=1; i<=n;i++) s[i]=s[i-1]+C[i]; -      for(intI=1; i<=n;i++) s[i]+=i; $ DP (); $printf"%lld\n", F[n]); -     return 0; -}
View Code

"Slope" Toy packing

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.