当前位置:网站首页>Analysis of stone merging
Analysis of stone merging
2022-06-27 21:50:00 【A Fei algorithm】
282. Stone merging
Equipped with N Pile stones in a row , Its number is 1,2,3,…,N.
Every pile of stones has a certain quality , It can be described as an integer , Now we're going to put this N The stones are combined into a pile .
Only two adjacent stacks can be merged at a time , The price of the merger is the sum of the masses of the two piles of stones , After merging, the stones adjacent to the two piles will be adjacent to the new pile , Because the order of selection is different when merging , The total cost of the merger is also different .
For example, there are 4 The heaps of stones are 1 3 5 2, We can merge first 1、2 Pile up , The cost is 4, obtain 4 5 2, Merge again 1,2 Pile up , The cost is 9, obtain 9 2 , And then merge to get 11, The total cost is 4+9+11=24;
If the second step is to merge first 2,3 Pile up , The price is 7, obtain 4 7, The cost of the last merger is 11, The total cost is 4+7+11=22.
The problem is : Find a reasonable way , Minimize the total cost , The minimum cost of output .
Input format
Number one on the first line N Represents the number of piles of stones N.
The second line N Number , Represents the mass of each pile of stones ( Not more than 1000).
Output format
Output an integer , The minimum cost .
Data range
1≤N≤300
sample input :
4
1 3 5 2
sample output :
22
Method 1: Section DP
- Section DP The classic question of , f [ i ] [ j ] f[i][j] f[i][j] Express the [ i . . . j ] [i...j] [i...j] The minimum cost of merging stones into a pile
- Dynamic transfer equation
- f [ i ] [ j ] = m i n ( f [ i ] [ j ] , f [ i ] [ k ] + f [ k + 1 ] [ j ] + s u m [ i . . . j ] ) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+sum[i...j]) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+sum[i...j])
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] stones = new int[n];
int[] sum = new int[n + 1];
for (int i = 1; i <= n; i++) {
stones[i - 1] = in.nextInt();
sum[i] = sum[i - 1] + stones[i - 1];
}
int[][] f = new int[n + 1][n + 1];
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
f[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
f[i][j] = Math.min(f[i][j], f[i][k] + f[k + 1][j] + sum[j] - sum[i - 1]);
}
}
}
System.out.println(f[1][n]);
}
边栏推荐
猜你喜欢

PCIE知识点-008:PCIE switch的结构

Go从入门到实战—— 多路选择和超时控制(笔记)

100 important knowledge points that SQL must master: filtering data

Bit. Store: long bear market, stable stacking products may become the main theme

GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析

Go from introduction to actual combat - context and task cancellation (notes)

MYSQL和MongoDB的分析
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]

Codeforces Round #723 (Div. 2)

集合代码练习
随机推荐
Golang 使用正则来匹配出子字符串函数
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
linux下安装oracle11g 静默安装教程
100 important knowledge points that SQL must master: sorting and retrieving data
Simulink导出FMU模型文件方法
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
根据自定义excel标题模板快速excel导出
Go from introduction to practice -- coordination mechanism (note)
matlab查找某一行或者某一列在矩阵中的位置
Little known MySQL import data
Xiao Wang's interview training task
[LeetCode]30. 串联所有单词的子串
win11桌面出現“了解此圖片”如何删除
GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
GBase 8a OLAP函数group by grouping sets的使用样例
100 important knowledge points that SQL must master: creating calculation fields
Go从入门到实战——CSP并发机制(笔记)
Go从入门到实战——Context与任务取消(笔记)
Go from introduction to actual combat - panic and recover (notes)
Tiktok's interest in e-commerce has hit the traffic ceiling?