Hnoi 2004 Brick Dynamic Planning

Source: Internet
Author: User

Test Instructions:N-layer bricks are placed in a groove, the top layer has n blocks, the second layer has n-1 blocks, ..., and the bottom layer has only one brick. The bricks on layer I are numbered from left to right and 1,2,......,i, and block J of Layer I has a value a[i,j] (a[i,j]<=50). Here is an example of a 5-storey brick:

If you want to knock off the first layer of block J brick, if i=1, you can directly knock it off, if i>1, you must first knock off the i-1 layer of the section J and j+1 block bricks. Your task is to knock off (m<=500) bricks from a heap of bricks with N (n<=50) so that the value of the bricks knocked out is the largest sum. Method:Dynamic planning parsing:Ignore the 76 value in the template diagram. Test this problem, the first 1 hours of basic ratio, the feeling is to use dynamic planning but did not find the right method. Then began to put the brain hole, began to rotate the picture, found to rotate to a magical figure, the problem is changed? For example, let's look at a sample

So we can not find any problems, so we have to change the angle.

We can find that the premise of taking 8 is to take a line of 2 and go to its front 2 to take the third line to the far right of the 2 premise is to take the line all and the top row, and all the elements in front of him. And so on, a law obviously appears, we may wish to set f[i][j][k] to indicate that line I take the former J and take a total of k points. The reason why this is set is to take a line must be a prefix. How to update it? is from the previous row fetch P update, wherein p>=j-1, this is obvious, because if the current line you want to fetch to the first J, then the last line of the j-1 must be taken, but also to take the j-1 before the prefix need to take, the equation of the motion of the column. One thing to note, because the enumeration J may be zero, that is, p may be-1, here you need to judge, (because here hang 5 points good pit!) ) Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N -using namespace STD;intA[n][n];intSum[n][n];intf[n][n][n*Ten];intN,m;intMain () {scanf("%d%d", &n,&m); for(intj=1; j<=n;j++) { for(inti=j;i<=n;i++) {scanf("%d", &a[i][j]); }    } for(intI=1; i<=n;i++) for(intj=1; j<=i;j++) sum[i][j]=sum[i][j-1]+A[I][J];intans=-1;memset(f,-1,sizeof(f)); for(intI=1; i<=n;i++) f[i][0][0]=0, f[i][1][1]=a[i][1]; for(intI=1; i<=n;i++) { for(intj=0; j<=i;j++) { for(intk=0; k<=m;k++) {if(j<=k) { for(intp=j-1>=0? J-1:0;p <=i-1;p + +) {if(f[i-1][p][k-j]!=-1) {F[i][j][k]=max (f[i][j][k],f[i-1][P][K-J]+SUM[I][J]); }                    }                }if(k==m) Ans=max (Ans,f[i][j][k]); }        }    }printf("%d\n", ans);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hnoi 2004 Brick Dynamic Planning

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.