2018.2.20 寒假作业 A - Multiplication Puzzle

题目:


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


6

10 1 50 50 20 5

Sample Output


3650



题目大意:

一个整数序列包含N个1~100的整数(3<=N<=100),从中取出一个数并和相邻两边的整数相乘,依次进行下去直到只剩下首尾两个数为止,求最终的得到的和的最小值。两边的数不能取,且不重复选取。

思路:

dp[i][j]表示将第I个和第j个之间的数取完得到的和,那么由k表示的数决定的子序列的宽度,依次递增宽度,并且移动子序列,即改变i的值,然后对i到i+k之间的元素进行遍历,比较dp[i][i+k]和先选取a[i]和a[j]之间的数,a[j]和a[i+k]之间的数,最后只剩下a[j],这一过程中的得到的和dp[i][j]+dp[j][i+k]+a[i]*a[i+k]*a[j]与原来的和的大小。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<cstdlib>
#include<ctype.h>
using namespace std;
typedef long long ll;
#define pi 3.14159265358979323846264338327
#define E 2.71828182846
#define INF 0x3f3f3f3f
#define maxn 555555

int a[110];
int dp[110][100];

int main() {
	int T;
	while (scanf("%d", &T) != EOF) {
		int i, j, l;
		for (i = 1; i <= T; i++) {
			scanf("%d", &a[i]);
		}
		memset(dp, 0, sizeof(dp));
		for (l = 2; l < T; l++) {
			for (i = 2; i + l <= T + 1; i++) {
				j = i + l - 1;
				dp[i][j] = INF;
				for (int k = i; k < j; k++) {
					dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + a[i - 1] * a[k] * a[j]);
				}
			}
		}
		printf("%d\n", dp[2][T]);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/ineedyou/p/8456058.html

时间: 2024-11-09 09:50:20

2018.2.20 寒假作业 A - Multiplication Puzzle的相关文章

2017级面向对象程序设计寒假作业2

Deadline:2018.02.11 22:00 pm 第一次的寒假作业,开启了大家的寒假生活,转眼回到了"小学生作文题"的感觉.不知道是否也通过这次作业,将你的回忆时光拉回到了几年前或十年前,沉思回忆代替了纷扰的知识.很多同学回忆了小学.中学的课任老师,或书法.绘画的老师.无论他们是严厉还是宽容,是鸡汤还是淡定,都或多或少的影响或潜移默化改变着你.在经过一次时光超越后,书归正传,我们开启编码的作业. 一.Pintia小作业 注册pintia,填写昵称为"fzu+学号&qu

POJ1651——Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: 3964 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

洛谷 P2717 寒假作业

题目背景 zzs和zzy正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有n项寒假作业.zzy给每项寒假作业都定义了一个疲劳值Ai,表示抄这个作业所要花的精力.zzs现在想要知道,有多少组连续的寒假作业的疲劳值的平均值不小于k? 简单地说,给定n个正整数A1,A2,A3,...,An,求出有多少个连续的子序列的平均值不小于k. 输入输出格式 输入格式: 第一行两个正整数,n和k. 第二行到第n+1行,每行一个正整数Ai. 输出格式: 一个非负整数. 输入输出样例 输入样例#1: 3

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ 1651:Multiplication Puzzle 矩阵相乘式DP

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7118   Accepted: 4385 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

洛谷P2717 寒假作业

题目:https://www.luogu.org/problemnew/show/2717 题目背景 zzs和zzy正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有n项寒假作业.zzy给每项寒假作业都定义了一个疲劳值Ai,表示抄这个作业所要花的精力.zzs现在想要知道,有多少组连续的寒假作业的疲劳值的平均值不小于k? 简单地说,给定n个正整数A1,A2,A3,...,An,求出有多少个连续的子序列的平均值不小于k. 输入输出格式 输入格式: 第一行两个正整数,n和k. 第二行到第

2018.02.04(补作业系列)

2018.02.04 补作业系列 1.合并石子 思路: 核心代码: 状态转移方程&解析:s[i]表示前i堆石子的数量总和,f[i][j]表示把第i堆石子到第j堆石子合并成一堆的最优值. 1 for ( i = n-1 ; i >= 1 ; i-- ){ 2 for ( j = i+1 ; j <= n ; j++ ){ 3 for ( k = i ; k <= j-1 ; k++ ){ 4 f[i][j] = min ( f[i][j] , f[i][k] + f[k+1][j]