当前位置:网站首页>Dynamic programming [III] (interval DP) stone merging
Dynamic programming [III] (interval DP) stone merging
2022-06-27 12:02:00 【Wu Yu 4】
Stone merging
link :282. Stone merging - AcWing Question bank
Topic :

Ideas :

Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=310;
int n,a[N],sum[N],f[N][N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
// Prefix sum method
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
// Calculation [l,r] Prefix and formula for :sum[r]-sum[l-1]
// The interval length traverses from small to large
for(int len=2;len<=n;len++)// The length is 1 The time is not to merge , From 2 Start the cycle
{
for(int i=1;i+len-1<=n;i++)
{
int l=i,r=i+len-1;
f[l][r]=1e8;// The minimum value is required here , So the initial value is assigned a larger number
for(int s=l;s<r;s++)// That is, the state transition in the train of thought can be increased from the left to r-1
f[l][r]=min(f[l][r],f[l][s]+f[s+1][r]+sum[r]-sum[l-1]);
}
}
cout<<f[1][n]<<endl;
return 0;
}边栏推荐
- The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
- 0 basic understanding of how e-commerce systems connect with payment channels
- MapReduce实战小案例(自定义排序、二次排序、分组、分区)
- L'utilisation de C language 0 length Array
- Unity Shader学习(二)第一个Shader
- R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
- Wechat applet payment password input
- The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
- Leetcode 177 The nth highest salary (June 26, 2022)
- matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
猜你喜欢

Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022

Redis distributed lock 15 ask, what have you mastered?

Shell script learning notes

Heap heap sort TOPK

Unity shader learning (II) the first shader

Nifi from introduction to practice (nanny level tutorial) - identity authentication

c/s 架构

pull request

Salesforce 容器化 ISV 场景下的软件供应链安全落地实践

56. Core principle of flutter - flutter startup process and rendering pipeline
随机推荐
R语言使用epiDisplay包的poisgof函数对泊松回归(Poisson Regression)执行拟合优度检验、检验是否存在过度离散问题(overdispersion)
最短编辑距离(线性dp写法)
. Net6 access skywalking link tracking complete process
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
.NET6接入Skywalking链路追踪完整流程
Unity Shader学习(一)认识unity shader基本结构
C/s architecture
MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
Popular science of device review: popular science of innovative medical device series - sternum plate products
C# wpf 实现撤销重做功能
C语言0长度数组的妙用
MQTT协议栈原理及交互流程图
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
[tcapulusdb knowledge base] tcapulusdb system user group introduction
MapReduce实战小案例(自定义排序、二次排序、分组、分区)
I.MX6ULL启动方式
QStyle类用法总结(三)
旭日3SDB,安装原版ros
C語言0長度數組的妙用
Excel中输入整数却总是显示小数,如何调整?