当前位置:网站首页>[interval DP] stone consolidation
[interval DP] stone consolidation
2022-06-28 06:26:00 【Nathan Qian】
subject
282. Stone merging - AcWing Question bank
AcWing 282. Stone merging ( Section DP Detailed analysis of template questions ) - AcWing
explain
- State means :f(i,j) It means that you will i To j A collection of schemes combined into a pile , The attribute is min
- State calculation :i<j when ,f(i,j)=min(f(i,k)+f(k+1,j)+s[j]-s[i-1] i=j when ,f(i,i)=0
- The first layer of the loop is the enumeration interval length , The second dimension is fixed i The location of , Ensure that each state is calculated to
- The final answer is (1~n) Total interval of , namely f(1,n)
Code segment
Section DP Templates
for (int i = 1; i <= n; i++) {
dp[i][i] = Initial value
}
for (int len = 2; len <= n; len++) // Interval length
for (int i = 1; i + len - 1 <= n; i++) { // Enumeration starting point
int j = i + len - 1; // Interval end point
for (int k = i; k < j; k++) { // Enumerate split points , Construct the state transition equation
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j] + w[i][j]);
}
}
#include<iostream>
using namespace std;
const int N=310;
int a[N],s[N],f[N][N],n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
s[i]=a[i]+s[i-1];
}
for(int i=1;i<=n;i++)
f[i][i]=0;
for(int len=2;len<=n;len++)
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
f[i][j]=1e9;
for(int k=i;k<j;k++)
{
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+s[j]-s[i-1]);
}
}
cout<<f[1][n]<<endl;
}边栏推荐
- 4. use MySQL shell to install and deploy Mgr clusters | explain Mgr in simple terms
- 整型提昇和大小端字節序
- sql及list去重操作
- ImportError: cannot import name 'ensure_dir_exists'的可解决办法
- Promotion intégrale et ordre des octets de fin de taille
- Uni app wechat applet sharing function
- 使用SSM框架,配置多个数据库连接
- RN7302三相电量检测(基于STM32单片机)
- 不会还有人只会用forEach遍历数组吧?
- Openharmony gnawing paper growth plan -- json-rpc
猜你喜欢

How to open UMD, KMD log and dump diagrams in CAMX architecture

三极管驱动无刷电机

MySQL(一)——安装

How the third-party libraries in cocoapod reference local header files

socke. IO long connection enables push, version control, and real-time active user statistics

Some habits of it veterans in the workplace

Parsing ng template with let total in NZ Pagination

Development trend of mobile advertising: Leveraging stock and fine marketing

AutoCAD C polyline small acute angle detection

整型提升和大小端字节序
随机推荐
AttributeError: 'callable_iterator' object has no attribute 'next'
death_ satan/hyperf-validate
三极管驱动无刷电机
选拔赛题目代码
异常处理(一)——空指针和数组索引越界
Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv
D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
Freeswitch sets the maximum call duration
Freeswitch使用originate转dialplan
报错--解决core-js/modules/es.error.cause.js报错
基本类型和包装类的区别
Working principle of es9023 audio decoding chip
freeswitch使用mod_shout模块播放mp3
socke.io長連接實現推送、版本控制、實時活躍用戶量統計
Install and manage multiple versions of PHP under mac
Singleton singleton mode
Difficulty calculation of Ethereum classic
Triode driven brushless motor
Idea automatically adds comments when creating classes
AutoCAD C# 多段線小銳角檢測