UVA10304---(Interval dp)

Source: Internet
Author: User

The first thought was to enumerate the root nodes and then memorize the search. The results of tle, and finally a glance at the puzzle moment understand. Alas, the mind is too limited.

Since the data is arranged from small to large, a binary sort tree can be formed naturally.

The dp[i][j] is the minimum value of the BST that can be composed of the elements of the interval [i,j], then the result of the large interval is related to the root node and the results between the cells, it is obvious that the interval DP,

Transfer equation lap dp[i][j] = Min{dp[i][k-1] + dp[k+1][j] + sum (i,j)-a[k]} sum (I,J) is the interval and, because when the two trees are connected to the left and right

On the root node, the height of itself is increased by 1, so each element is evaluated more than once, and the last root node is subtracted from the root node because the number of layers is 0.

#include <cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>#include<cmath>#include<sstream>#include<queue>#include<stack>#defineINF 530600414#defineN 201314using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;Const intMAXN =255;intN;intA[MAXN];intDP[MAXN][MAXN];intSUM[MAXN];intMain () {//freopen ("in", "R", stdin);     while(~SCANF ("%d",&N)) {sum[0] =0;  for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); Sum[i]= sum[i-1] +A[i]; }        //sort (a+1,a+n+1);        if(n = =1) dp[1][n] =0; Else{memset (DP,0,sizeof(0)); //for (int i = 1; I <= n; ++i) dp[i][i] = A[i];             for(intL =2; L <= N; ++l) for(intSt =1; st+l-1<= N; ++St) {                    inted = st+l-1; Dp[st][ed]=INF; intCNT = sum[ed]-sum[st-1];  for(intK = st; K <= Ed; ++k) dp[st][ed]= Min (dp[st][ed],dp[st][k-1] + dp[k+1][ed] + CNT-A[k]); }} printf ("%d\n", dp[1][n]); }}

UVA10304---(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.