(POJ
来源:互联网 发布:apache ant zip.jar 编辑:程序博客网 时间:2024/06/03 16:45
(POJ - 1651)Multiplication Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10623 Accepted: 6626
Description
The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.
Input
The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.
Output
Output must contain a single integer - the minimal score.
Sample Input
6
10 1 50 50 20 5
Sample Output
3650
题目大意:给出一串数字,每次删掉一个数字的代价是这个数字和左右两个数字的乘积,首尾数字不能删也不用删,问删掉这串数字的代价最小是多少。
思路:这与csu石子合并那题经典区间dp类似,只是这里的代价变成了三个数的乘积。附上链接:石子归并
设f[i][j]表示区间[i,j]所代表的最小代价,在区间[i,j]中枚举最后一个删掉的元素a[k],则易得状态转移方程为
递推写法:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=105;int a[maxn],f[maxn][maxn];int main(){ int n; while(~scanf("%d",&n)) { memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) scanf("%d",a+i); for(int l=2;l<n;l++) for(int i=1;i+l<=n;i++) { int j=i+l; f[i][j]=INF; for(int k=i+1;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k][j]+a[i]*a[k]*a[j]); } printf("%d\n",f[1][n]); } return 0;}
记忆化搜索写法:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=105;const int INF=0x3f3f3f3f;int a[maxn];int f[maxn][maxn];int dfs(int i,int j){ if(f[i][j]!=-1) return f[i][j]; if(j==i+1) return 0; int ans=INF; for(int k=i+1;k<j;k++) ans=min(ans,dfs(i,k)+dfs(k,j)+a[i]*a[k]*a[j]); f[i][j]=ans; return ans;}int main(){ int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",a+i); memset(f,-1,sizeof(f)); printf("%d\n",dfs(1,n)); } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- tangram.js实现js的类的方式
- 关于weblogic连接池爆满的原因和处理
- Vegas的安装激活教程
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第2讲(四大界面之间的跳转与玩家飞机的移动)
- hdu 1525 Euclid's Game
- (POJ
- [JAVA] 生成MD5 java.security.MessageDigest BUG
- SQLServer Stuff函数的用法
- HttpServletRequest对象
- Mybatis 插入与批量插入以及多参数批量删除
- android studio 源码关联
- 442. Find All Duplicates in an Array
- 政府安全资讯精选 2017年第四期:聚焦美国网络安全新动态
- Spring之OpenSessionInView详解及用法