当前位置:网站首页>区间DP AcWing 282. 石子合并
区间DP AcWing 282. 石子合并
2022-07-02 09:43:00 【T_Y_F666】
区间DP AcWing 282. 石子合并
原题链接
算法标签
动态规划 区间DP
思路
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 505;
int a[N], s[N], f[N][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 1, n+1){
a[i]=read();
}
rep(i, 1, n+1){
a[i]+=a[i-1];
}
// 石子堆长度 若石子堆长度为1 f[1][1]初始值0,符合题意
rep(len, 2, n+1){
// 石子堆起点
for(int i=1; i+len-1<=n; ++i){
// 石子堆左右端点
int l=i, r=i+len-1;
f[l][r]=1e8;
// 石子分割点
rep(k, l, r){
f[l][r]=min(f[l][r], f[l][k]+f[k][r]+s[r]-s[l-1]);
}
}
}
printf("%lld", f[1][n]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Deep Copy Event bus
- Calculate the maximum path sum of binary tree
- arcgis js 4. Add pictures to x map
- Introduction to CPU instruction set
- CDA数据分析——Excel数据处理的常见知识点归纳
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- Addition, deletion, modification and query of MySQL table (Advanced)
- From scratch, develop a web office suite (3): mouse events
- Test shift left and right
- drools动态增加、修改、删除规则
猜你喜欢
刷题---二叉树--2
倍增 LCA(最近公共祖先)
Tas (file d'attente prioritaire)
Initial JDBC programming
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Deep Copy Event bus
BOM DOM
"As a junior college student, I found out how difficult it is to counter attack after graduation."
WSL 2 will not be installed yet? It's enough to read this article
随机推荐
Docker-compose配置Mysql,Redis,MongoDB
Writing method of then part in drools
Leetcode - Sword finger offer 51 Reverse pairs in an array
Drools terminates the execution of other rules after executing one rule
LeetCode—剑指 Offer 59 - I、59 - II
mysql表的增删改查(进阶)
倍增 LCA(最近公共祖先)
Heap (priority queue)
Leetcode - Sword finger offer 59 - I, 59 - II
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Addition, deletion, modification and query of MySQL table (Advanced)
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
IPhone 6 plus is listed in Apple's "retro products" list
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
堆(優先級隊列)
Fastdateformat why thread safe
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Docker compose configuration mysql, redis, mongodb
刷题---二叉树--2