当前位置:网站首页>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]);
}
边栏推荐
- Simulink导出FMU模型文件方法
- 100 important knowledge points that SQL must master: combining where clauses
- MYSQL和MongoDB的分析
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
- [Sword Offer II]剑指 Offer II 029. 排序的循环链表
- [LeetCode]508. The most frequent subtree elements and
- 图解基于AQS队列实现的CountDownLatch和CyclicBarrier
- List of language weaknesses --cwe, a website worth learning
- GBase 8a OLAP分析函数 cume_dist的使用样例
- STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
猜你喜欢

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

Codeforces Global Round 14

Go from introduction to practice - Interface (notes)

Burp suite遇到的常见问题
![[LeetCode]动态规划解拆分整数I[Silver Fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[LeetCode]动态规划解拆分整数I[Silver Fox]

语言弱点列表--CWE,一个值得学习的网站

01-Golang-环境搭建

关于异常处理的知识整理
随机推荐
Special training of guessing game
JVM memory structure when creating objects
SQL必需掌握的100个重要知识点:IN 操作符
Go从入门到实战——共享内存并发机制(笔记)
SQL必需掌握的100个重要知识点:过滤数据
100 important knowledge points that SQL must master: combining where clauses
Go从入门到实战——Panic和recover(笔记)
The difference between scrum and Kanban
Express e stack - small items in array
Go从入门到实战——行为的定义和实现(笔记)
神奇的POI读取excel模板文件报错
[leetcode] dynamic programming solution split integer i[silver fox]
快速excel导出
Special tutorial - Captain selection game
我想我要开始写我自己的博客了。
[LeetCode]30. 串联所有单词的子串
Method of reading file contents by Excel
Array assignment
SQL必需掌握的100个重要知识点:排序检索数据
集合代码练习