Organize Books (book) Title Description
Little a would like to tidy up his room full of books. The books are divided into piles. Each pile of books has a mass of W and a value of V. The task of small A is to synthesize all the books into a pile. Because little a thinks that merging i,j two piles of books requires a force of w[i]-v[i]+w[j]-v[j]. The quality and value of the combined book pile are the sum of the quality and value of the first two stacks of books. That is, after merging i,j two piles of books, w=w[i]+w[j],v=v[i]+v[j]. The merger can only be done between two adjacent stacks of books. The position is unchanged before and after the book is merged. If you merge the l,2 in the 3, then merge the results to 3, then 3, 3 to 6 (1,2,3,6 refers to mass). Please help him to calculate the minimum amount of effort.
Input
Line 1th is an integer n (2≤n≤400).
Line 2~n+l two integers w and V (0<v<w<=1000) per line
Output
Only 1 lines, only one integer, represents the least amount of effort spent.
Sample input
36 59 711 2
Sample output
15
Tips
Sample Description: Merge the first two piles first, then merge the stacks with the remaining piles.
Analysis: Interval DP, simple to change the stone merge that problem is good;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<ext/rope>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=1e3+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;using namespace__gnu_cxx;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,M,DP[MAXN][MAXN],A[MAXN];intMain () {inti,j,k,t; scanf ("%d",&N); Rep (I,1, N) scanf ("%d%d", &a[i],&k), a[i]-=K; for(i=1; i<=n;i++) a[i]+=a[i-1]; Rep (I,1, N) dp[i][1]=0; Rep (J,2, N) { for(i=1; i+j<=n+1; i++) {Dp[i][j]=inf; Rep (K,1, J-1) Dp[i][j]=min (dp[i][j],dp[i][k]+dp[i+k][j-k]+a[i+j-1]-a[i-1]); }} printf ("%d\n", dp[1][n]); //System ("pause"); return 0;}
Organize Books (book)