Interval DP Primer

Source: Internet
Author: User

First we need to know how the interval is done with DP, so let's look at the template.

1  for(inti =1; I <= N; i++) {//number of enumeration intervals2      for(intj =1; J <= can enumerate to the largest POS; J + +){3         intp = i + J-1;//represents the coordinates of the maximum value that can be reached at the moment4         if(P > N) Break;5          for(intK = J; K <= P; k++){6Dp[j][p] = min or Max (dp[j][p], dp[j][k] + dp[k +][p] +J to P Val);7         }8     }9}
View Code

① Stone Problem

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737

This is a very obvious template problem.

You can use sum[] in advance to maintain intervals, or you can maintain them in a tree-like array.

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = $+5;Const intINF =0x3f3f3f3f;intDP[MAXN][MAXN];intA[MAXN];intSUM[MAXN];intN;intMain () { while(SCANF ("%d", &n) = =1){         for(inti =1; I <= N; i++) scanf ("%d", A +i); Memset (DP,0,sizeof(DP)); memset (SUM,0,sizeof(sum));  for(inti =1; I <= N; i++) {Sum[i]= Sum[i-1] +A[i]; }         for(inti =2; I <= N; i++){             for(intj =1; J <= N-i +1; J + +){                intp = j + I-1; if(P > N) Break; DP[J][P]=inf;  for(intK = J; K <= P; k++) {Dp[j][p]= Min (Dp[j][p], dp[j][k] + dp[k +1][P] + sum[p]-sum[j-1]); }}} printf ("%d\n", dp[1][n]); }    return 0;}
View Code

Ii

Interval DP Primer

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.