POJ 1651 Multiplication Puzzle
题目:
Multiplication Puzzle
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.
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
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
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
题意:
就是给出一组排好序列的数,你可以从中去掉数字,每一次去掉的数字就会有一次的得分,得分为去掉的这个数字乘以它前面的那个数字和它后面的那个数字。这样子去数字直到只剩下原数列的第一个数和原数列的最后一个数~
SO~从题目上就可以知道这是一道赤果果的DP╮(╯▽╰)╭
状态转移方程:dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i<k<j)
其中dp[i][j]表示的是:第i个数字到第j个数字中间的所有数字都取完了,所得到的最小分数~,k为i到j中最后取的数字~
代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 #include <math.h> 5 #include <stdio.h> 6 #include <string.h> 7 using namespace std; 8 #define MOD 1000000007 9 int dp[110][110]; 10 11 int main() 12 { 13 int i,j,k; 14 int n; 15 int a[110]; 16 while(scanf("%d",&n)!=EOF) 17 { 18 for(i=1;i<=n;i++) 19 scanf("%d",&a[i]); 20 memset(dp,0,sizeof(dp)); 21 for(i=1;i<=n;i++) 22 dp[i][i+2]=a[i]*a[i+1]*a[i+2]; 23 for(k=4;k<=n;k++){ 24 for(i=1;i+k-1<=n;i++){ 25 dp[i][i+k-1]=10000000; 26 for(j=i+1;j<i+k-1;j++) 27 dp[i][i+k-1]=min(dp[i][i+k-1],dp[i][j]+dp[j][i+k-1]+a[i]*a[j]*a[i+k-1]); 28 } 29 } 30 printf("%d\n",dp[1][n]); 31 32 } 33 return 0; 34 }
//memory:212KB time:0ms
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版