当前位置:网站首页>Interval DP chain stone merging
Interval DP chain stone merging
2022-07-23 21:36:00 【Duktig_ seven】
Section DP- Stone merging
Problem description
N A row of stones , The number is 1,2,…,n. Each pile of stones has a pile of weight , Now combine all the stones into one pile . The cost of merging two piles of stones into one pile is the sum of the weight of the two piles of stones , The weight of the combined pile of stones is the sum of the weight of the two piles of stones . Try to find the minimum cost of merging all the stones into one pile .
Input format
Enter a number in the first line N, Represents the number of piles of stones ; Second line input N It's an integer , respectively N The weight of the stones .
Output format
The output will N The minimum cost of merging stones into a pile
Data range
1<=N<=30
sample input
4
1 3 5 2
sample output
22
Their thinking
take N The process of merging stones into a pile , In each step, two piles of stones are combined into one pile , Reduce the number of stone piles by one ; Two piles of stones merged each time , It may be from the beginning N Made up of several piles of stones .
- remember [l,r] citing (l,l+1,l+2,…,r) Merge into a pile of stones ; Each pile of stones is obtained by merging two piles of stones , be [l,r] The stone may be made of [l,k] and [k+1,r](l<=k<r) Two piles of stones are combined to get .
- remember f[l,r] In order to get [l,r] The smallest price paid for this pile of stones ; Ruoshi [l,r] from [l,k] and [k+1,r] Two piles are merged to get , be obtain [l,r] The price paid by this pile of stones by obtain [l,k] The price paid by this pile of stones and obtain [k+1,r] This pile of stones and take [l,k] and [k+1,r] The cost of merging two piles of stones into one , So we get the recurrence formula :
f[l,r]=min(f[l,k]+f[k+1,r]+s[r]-s[l-1]),(l<r)
f[l,r]=0,(l == r)
among ,s[i] For the former i The prefix of the weight of the rockfill and ,(l <= k < r)
- initial value : arbitrarily i∈[1,N],f[l][l]=0, The rest is positive infinity
- The goal is :f[1][n]
Implementation code
#include<iostream>
#include<algorithm>
using namespace std;
const int N=310;
int n;
int w[N],f[N][N];
int s[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>w[i];
// Find the prefix and
for(int i=1;i<=n;i++) s[i]=s[i-1]+w[i];
for(int len=2;len<=n;len++)
for(int i=1;i+len-1 <= n;i++){
int l=i,r=i+len-1;
f[l][r]=1e8;// Make a difference k Under the condition of f[l][r] The minimum value of , So I don't want his initial value to be affected , So set it to positive infinity
for(int k=i;k<r;k++)// enumeration [l,r-1] Different intervals within the range
f[l][r]=min(f[l][r],f[l][k]+f[k+1][r]+s[r]-s[l-1]);
}
// The output result is the interval [1,n] Value
cout<<f[1][n]<<endl;
return 0;
}
边栏推荐
- Openlayers instance advanced view positioning advanced view positioning
- &9 nodemon自动重启工具
- C - documents
- [create birthday card application]
- Qt桌面白板工具其一(解决曲线不平滑的问题——贝塞尔曲线)
- Deep learning - NLP classic papers, courses, papers and other resources sorting and sharing
- 剑指 Offer II 115. 重建序列 : 拓扑排序构造题
- 初识js(适合新手的编程)
- High numbers | calculation of triple integral 1 | high numbers | handwritten notes
- 2022.7.22 JS object
猜你喜欢
随机推荐
1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS
Synchro esp32c3 Hardware Configuration Information serial port Print Output
Openlayers instances advanced mapbox vector tiles advanced mapbox vector maps
MySql的DDL和DML和DQL的基本语法
Vite3 learning records
[wechat applet] do you know about applet development?
合宙ESP32C3硬件配置信息串口打印輸出
Proof of green Tao theorem (2): generalization of von Neumann theorem
Chapter1 data cleaning
Unity—3D数学-Vector3
Scala programming (intermediate advanced experimental application)
How to introduce your project experience in the interview
合宙ESP32C3硬件配置信息串口打印输出
DBSCAN点云聚类
Jianzhi offer II 115. reconstruction sequence: topological sorting construction problem
[attack and defense world web] difficulty four-star 12 point advanced question: confusion1
Basic syntax of MySQL DDL and DML and DQL
VLAN comprehensive experiment
宇树A1机器狗手势控制
Edge cloud | 1. overview








