当前位置:网站首页>(example of dynamic planning) stone merging
(example of dynamic planning) stone merging
2022-07-23 08:45:00 【Salted egg_ dd】
Great ideas for solving problems , I suggest you chew it if you have nothing to do .
Equipped with NN Pile stones in a row , Its number is 1,2,3,…,N1,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 NN 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 44 The heaps of stones are 1 3 5 2, We can merge first 1、21、2 Pile up , The cost is 44, obtain 4 5 2, Merge again 1,21,2 Pile up , The cost is 99, obtain 9 2 , And then merge to get 1111, The total cost is 4+9+11=244+9+11=24;
If the second step is to merge first 2,32,3 Pile up , The price is 77, obtain 4 7, The cost of the last merger is 1111, The total cost is 4+7+11=224+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 NN Represents the number of piles of stones NN.
The second line NN Number , Represents the mass of each pile of stones ( Not more than 10001000).
Output format
Output an integer , The minimum cost .
Data range
1≤N≤3001≤N≤300
sample input :
4
1 3 5 2
sample output :
22#include <bits/stdc++.h>
using namespace std;
const int N=310;
int n;
int s[N];
int f[N][N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&s[i]);
for(int i=1;i<=n;i++) s[i]=s[i-1]+s[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;
for(int k=l;k<=r-1;k++)
f[l][r]=min(f[l][r],f[l][k]+f[k+1][r]+s[r]-s[l-1]);
}
}
printf("%d\n",f[1][n]);
}
边栏推荐
- 【GNN报告】香港科技大学李佳:图异常检测再思考—我们究竟需要怎样的图神经网络?
- [openvx] VX for basic use of objects_ array
- On the stability of common sorting
- 如何防范各类联属欺诈?
- uva1344
- [arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection
- js小游戏奔跑的熊和猫源码
- 图的遍历 ~
- 打印100~200之间的素数
- svn: E000022: Can‘t convert string from ‘UTF-8‘ to native encoding 问题解决
猜你喜欢

我用Flutter开发了一个类似微信可运行小程序的App

C语言编写“Hello World”挑战赛,你会如何作答?

聊聊队列(FIFO)的应用

华为再回应“清理34岁以上员工”传言,程序员如何应对“35岁危机”?

Cases on classes and objects

On the stability of common sorting

【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection

你知道怎么做好接口测试?

TweenMax+SVG皮卡丘变身球

HCIP - - - BGP综合实验
随机推荐
uva1467
[openvx] VX for basic use of objects_ array
XMODEM, ymodem and zmodem protocols are the three most commonly used communication protocols
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
Arcgis js api二次开发——加载国家天地图
沉淀2年的 Jira 自动化经验分享
Golang中iota的正确用法
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)是什么体验?
【wepy2.0】的安装
[openvx] VX for basic use of objects_ node
[openvx] VX for basic use of objects_ reference
Node definition and traversal of binary tree~
冰冰学习笔记:vim工具的基本操作
聊聊队列(FIFO)的应用
Nanoid? Better than UUID
1.学会看懂网页
物联网安装调试员丨让“智慧”生活早日来临
SQL中DDL和DML的基本操作(数据库)
Day011 循环结构中的跳转语句
Installation and configuration of MySQL and Navicat