code1319 Toy Packing

Source: Internet
Author: User

A division of DP, but because of the number of partitions arbitrarily, only one-dimensional array can be

Set Dp[i] Indicates the minimum cost of the first I box (any case)

Dp[i]=min (Dp[u]+cost (u+1,i))

But the complexity of the n<=50000,n side is obviously unacceptable.

Set Choice[i] Array save for each I value, the enumeration of the maximum value of the U-f[i]

Hit the table, found choice[] is monotonous does not descend

Each time for a new I, you only need to start from choice[i-1] when enumerating u

Code:

#include <iostream>#include<cstring>#defineSize 50005using namespacestd;intN;intC[size];Long LongSum[size];Long LongL;Long LongDp[size];intChoice[size];Long LongCostintLintR) {    Long Longx=sum[r]-sum[l-1] + RM; return(x-l) * (X-L);}intMain () {Freopen ("1319.in","R", stdin); Memset (DP,0x7f,sizeof(DP)); CIN>>n>>m;  for(intI=1; i<=n;i++) {cin>>C[i]; Sum[i]=sum[i-1]+C[i]; } dp[0]=0; choice[0]=0;  for(intI=1; i<=n;i++){         for(intu=choice[i-1];u<=i-1; u++){            if(Dp[u]+cost (u+1, i) <=Dp[i]) {Dp[i]=dp[u]+cost (u+1, i); Choice[i]=T; } }} cout<<Dp[n];     Fclose (stdin); return 0;} 

As for better nlgn optimization and n slope optimization, let's say ... sleeping

code1319 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.