当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

12 MySQL interview questions that you must chew through to enter Alibaba

Summary of the third class

Ctfshow web entry explosion

Creation and use of thymeleaf template

I spring and autumn blasting-2

Good article inventory

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

DVWA range clearance tutorial

I spring and autumn blasting-1

CSRF, XSS science popularization and defense
随机推荐
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
Go language programming specification combing summary
I include of spring and Autumn
CODING DevSecOps 助力金融企业跑出数字加速度
你童年的快乐,都是被它承包了
Anti shake and throttling
Summary of the second lesson
P6183 [USACO10MAR] The Rock Game S
30岁汇源,要换新主人了
Ctfshow web entry information collection
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
P1451 求细胞数量/1329:【例8.2】细胞
Talk about your understanding of microservices (PHP interview theory question)
1330:【例8.3】最少步数
可转债打新在哪里操作开户是更安全可靠的呢
【簡記】解决IDE golang 代碼飄紅報錯
Redis distributed lock principle and its implementation with PHP (1)
Crud of MySQL
Value series solution report
Nine hours, nine people, nine doors problem solving Report