"Hihocoder1636" Pangu and stones (interval dp)

Source: Internet
Author: User

Test instructions: N heap of stones, each can be combined with a continuous length from L to R of a number of stones for 1 heap, the cost of the total number of selected stones, to consolidate n heap into 1 heaps of the minimum total cost, no solution output 0

Idea: Dp[i][j][k] represents the minimum cost of merging the interval I to j into K-Heap

\[initial conditions dp[i][j][j-i+1]=0 \]

\[dp[i][j][k]=min (Dp[i][x][y-1]+dp[x+1][j][1]+s[j]-s[i-1] (k=1,i<=x<=j-1,l<=y<=r) \]

\[dp[i][j][k]=min (dp[i][x][k-1]+dp[x+1][j][1] (k>=2,i<=x<=j-1) \]

1#include <cstdio>2#include <cstring>3#include <string>4#include <cmath>5#include <iostream>6#include <algorithm>7#include <map>8#include <Set>9#include <queue>Ten#include <vector> One using namespacestd; AtypedefLong Longll; -typedef unsignedint UINT; -typedef unsignedLong Longull; thetypedef pair<int,int>PII; -typedef vector<int>VI; - #defineFi first - #defineSe Second + #defineMP Make_pair - #defineN 150 + #defineM 6100000 A #defineEPS 1e-8 at #definePi ACOs (-1) - #defineOO 1e9 -  - ll Dp[n][n][n],a[n],s[n]; -      - intMain () in { -     //freopen ("hihocoder1636.in", "R", stdin); to     //freopen ("Hihocoder1636.out", "w", stdout); +     intN,l,r; -      while(SCANF ("%d%d%d", &n,&l,&r)! =EOF) the     { *s[0]=0; $          for(intI=1; i<=n;i++) Panax Notoginseng         { -scanf"%lld",&a[i]); thes[i]=s[i-1]+A[i]; +         } AMemset (DP,0x3f,sizeof(DP)); the          for(intI=1; i<=n;i++)  +           for(intj=i;j<=n;j++) dp[i][j][j-i+1]=0; -          for(intlen=2; len<=n;len++) $           for(intI=1; i<=n-len+1; i++) $          { -              intj=i+len-1; -               for(intx=i;x<=j-1; x + +) the                for(inty=l;y<=r;y++)  -dp[i][j][1]=min (dp[i][j][1],dp[i][x][y-1]+dp[x+1][j][1]+s[j]-s[i-1]);Wuyi               for(intk=2; k<=len;k++) the                for(intx=i;x<=j-1; x + +)  -Dp[i][j][k]=min (dp[i][j][k],dp[i][x][k-1]+dp[x+1][j][1]); Wu          } -         if(dp[1][n][1]>oo) printf ("0\n"); About          Elseprintf"%lld\n", dp[1][n][1]); $     }  -     return 0; -}

"Hihocoder1636" Pangu and stones (interval dp)

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.