当前位置:网站首页>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;
}
边栏推荐
- Detailed explanation of C language branch statements
- 2.3 learning content
- Lesson 4 knowledge summary
- 你童年的快乐,都是被它承包了
- Calculate weight and comprehensive score by R entropy weight method
- Appium自动化测试基础 — APPium基础操作API(二)
- Example of lvgl display picture
- Bugku easy_ nbt
- Advanced level of static and extern
- mapper. Comments in XML files
猜你喜欢
随机推荐
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
Huiyuan, 30, is going to have a new owner
F. Min cost string problem solving Report
漫画:程序员不是修电脑的!
Bugku easy_ nbt
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
Brief introduction of machine learning framework
1330: [example 8.3] minimum steps
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
Bugku cyberpunk
Number protection AXB function! (essence)
Object. defineProperty() - VS - new Proxy()
Severlet learning foundation
Ctfshow web entry explosion
JS topic - console log()
记录一下树莓派搭建环境中遇到的坑。。。
Bugku's steganography
MySQL之CRUD
Anti shake and throttling
可转债打新在哪里操作开户是更安全可靠的呢









