当前位置:网站首页>Interval DP (gravel consolidation)
Interval DP (gravel consolidation)
2022-07-05 15:35:00 【Xuanhong Zhou】
I finally posted an article , The computer was repaired yesterday
Paste the code directly , Because I'm going to play football
/* Section DP Interval dynamic programming is an extension of linear dynamic programming , It's dividing problems in stages , It has a lot to do with the order in which elements appear in the phase and which elements are merged from the previous phase The key word is merging , That is, the interval of a problem can be divided into two parts and merged , Then each part of the can be further decomposed Thinking of this topic : Note that only two adjacent heaps can be merged The interval [i,j] The stones in the merge , The last merger is bound to merge the left and right piles . Therefore, we can know the consolidation interval [i,j] The minimum value of is to merge the minimum value of the left pile plus the minimum value of the right pile, and then add the value of this merge But how to decide where to divide the left and right piles , Then you can only traverse . Divide into two piles, and then continue to divide according to the above ideas . Similar to the idea of partition When writing code , First write short intervals and then gradually merge them into large intervals min(i,j)=min(i,k)+min(k+1,j)+s[j]-s[i-1]; Section dp Common templates : Traversal interval length Traverse the left endpoint Traversal classification */
#include<iostream>
using namespace std;
const int M=310;
int N;
int stones[M];
int dp[M][M];
int s[M];// Prefixes and arrays
int main(){
cin>>N;
for(int i=1;i<=N;i++){
cin>>stones[i];
s[i]=s[i-1]+stones[i];
}
for(int len=2;len<=N;len++){
for(int i=1;i+len-1<=N;i++){
int j=i+len-1;
dp[i][j]=1e8;
for(int k=i;k<j;k++){
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+s[j]-s[i-1]);
}
}
}
cout<<dp[1][N]<<endl;
return 0;
}
边栏推荐
猜你喜欢
![P1451 calculate the number of cells / 1329: [example 8.2] cells](/img/c4/c62f3464608dbd6cf776c2cd7f07f3.png)
P1451 calculate the number of cells / 1329: [example 8.2] cells

Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)

Bugku's Eval

Fr exercise topic --- comprehensive question

Database learning - Database Security

Detailed explanation of C language branch statements

I spring and autumn blasting-1

MySQL 巨坑:update 更新慎用影响行数做判断!!!

Mysql---- function

CSRF, XSS science popularization and defense
随机推荐
Write a go program with vscode in one article
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
Crud of MySQL
OSI seven layer model
Brief introduction of machine learning framework
如何将 DevSecOps 引入企业?
Redis distributed lock principle and its implementation with PHP (2)
Redis' transaction mechanism
Detailed explanation of QT creator breakpoint debugger
Detailed explanation of C language branch statements
I spring and autumn blasting-2
Ctfshow web entry explosion
力扣今日题-729. 我的日程安排表 I
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
【简记】解决IDE golang 代码飘红报错
Number protection AXB function! (essence)
R 熵权法计算权重及综合得分
12 MySQL interview questions that you must chew through to enter Alibaba
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Explanation report of the explosion