【codevs 2102】石子归并
来源:互联网 发布:淘宝店铺链接在哪复制 编辑:程序博客网 时间:2024/06/06 02:35
题目描述 Description
有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1]。问安排怎样的合并顺序,能够使得总合并代价达到最小。
输入描述 Input Description
第一行一个整数n(n<=100)
第二行n个整数w1,w2…wn (wi <= 100)
输出描述 Output Description
一个整数表示最小合并代价
样例输入 Sample Input
4
4 1 1 4
样例输出 Sample Output
18
区间DP
枚举断点,前缀和优化。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=500+5;int f[maxn][maxn],s[maxn];int n;int main(){ int x; memset(f,0x3f3f3f3f,sizeof(f)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&x); s[i]=s[i-1]+x; } for(int i=1;i<=n;i++) f[i][i]=0; for(int l=1;l<=n;l++) { for(int i=1;i<=n-l;i++) { int j=i+l; 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]); } } int ans=0x3f3f3f3f; ans=f[1][n]; printf("%d\n",ans); return 0;}
阅读全文
0 0
- 【codevs 2102】石子归并
- Codevs 2102 石子归并 2
- CodeVs 2102 石子归并 2
- 【codevs 2102】石子归并2
- codevs.cn:2102 石子归并2
- Codevs 1048 石子归并
- codevs 3002石子归并
- Codevs 1048 石子归并
- 【codevs 1048】石子归并
- 【codevs 1048】石子归并
- codevs 1048 石子归并
- CODEVS 1048石子归并
- codevs 石子归并
- CODEVS 1048 石子归并
- codevs 1048 石子归并
- codevs石子归并 动态规划
- codevs 1048 石子归并 挖个坑
- 石子归并问题(codevs 1048)
- 怎样进行文献调研--整理自学术讲座
- 常用正则表达式
- ionic_选项卡
- 小朋友学C语言(7):自定义函数
- 博客宗旨
- 【codevs 2102】石子归并
- HDU
- 小朋友学C语言(8):条件判断
- 获取正在运行的服务
- ionic_选项卡+侧拉
- webservice jax-rs与spring整合
- 小朋友学C语言(9):a++与++a
- ionic_侧拉
- Ubuntu14.04安装OpenCV2.4.13(ZIP安装)