当前位置:网站首页>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;
}
边栏推荐
- Why cluster chat server introduces load balancer
- The common interfaces of Alipay are uniformly encapsulated and can be used directly for payment parameters (applicable to H5, PC, APP)
- 一时跳槽一时爽,一直跳槽一直爽?
- Jianzhi offer II 115. reconstruction sequence: topological sorting construction problem
- 分布式能源的不确定性——风速测试(Matlab代码实现)
- The total ranking of blogs is 918
- Identify some positions in the parenthesis sequence
- googletest
- Openlayers instance accessible map accessible map
- LeetCode_ 376_ Wobble sequence
猜你喜欢
随机推荐
Compare kernelshap and treeshap based on speed, complexity and other factors
Chapter1 数据清洗
Chapter 2 Regression
Yushu A1 robot dog gesture control
First acquaintance with JS (programming suitable for beginners)
At 12 o'clock on July 23, 2022, the deviation from the top of the line of love life hour appeared, maintaining a downward trend and waiting for the rebound signal.
博客总排名为918
集群聊天服务器:Model数据层的框架设计和数据库代码的封装
比较关注证券公司究竟哪个佣金最低?请问网上开户安全么?
[wechat applet] do you know about applet development?
集群聊天服務器:數據庫錶的設計
Is it safe to open a mobile stock account?
How to implement desktop lyrics in pyqt
启牛是什么?请问一下手机开户股票开户安全吗?
googletest
How to use cesium knockout?
Uncertainty of distributed energy - wind speed test (realized by matlab code)
2022.7.22 js对象
寻找消失的类名
MySQL数据库索引








