Skip to main content
  1. Posts/

poj 1651 Multiplication Puzzle (区间dp)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

poj 1651题目链接

题意:n个数,删掉a[i]的得分是a[i]*a[i-1]*a[i+1],两个端点的不允许删。问删完n-2个数得到的最小分数是多少。

思路:能想到设计状态dp[i][j]表示区间[i,j]的最小分数。然后就没思路了。 T T

参考了几篇题解,思路大概是,枚举最后剩下的那个数。

**对于一个区间(l, r),如果最后删除的是k位置的数的话,将得到a[l]a[k]a[r]分,而要得到这个情况的前提是吧区间(l, k) 和(k, r)的中间数字删掉所以的转移方程是

**DP(l, r) = DP(l, k) + DP(k, r) + a[l]a[k]a[r];

以及。。。初始化又想错了。。。

要注意。。初始化可能没办法一次完成。。。这道题的初始化就是分情况的。。。

对于区间长度不够的。。初始化为0.。

区间长度为3的。。初始化为a[i]*a[i+1]*a[i+2]。。

其他的初始化为正无穷。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月25日 星期一 22时44分55秒
 4File Name :code/poj/1651.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int n;
35int a[N];
36int dp[N][N];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42	ios::sync_with_stdio(false);
43	cin>>n;
44	for ( int i = 1 ; i <= n ; i++) cin>>a[i];
45
46	ms(dp,0x3f);
47
48	for ( int l = 1 ; l <= n ; l++)
49	    for ( int i = 1; i <= n-l ; i++)
50	    {
51		int j = i + l;
52		if (l==1) dp[i][j]=0;  //分类初始化。。。
53		else
54		if (l==2)
55		    dp[i][j] = a[i]*a[i+1]*a[i+2];
56		else
57		for ( int k = i+1 ; k < j ; k++)
58		    dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[j]*a[k]); //枚举最后剩下的数。
59	    }
60
61	cout<<dp[1][n]<<endl;
62
63  #ifndef ONLINE_JUDGE
64  fclose(stdin);
65  #endif
66    return 0;
67}

Related

light oj 1422 - Halloween Costumes (区间dp)

·2 mins
light oj 1422 题目链接 题意: 按顺序去参加舞会。每个舞会对衣服都有要求。可以连续穿好多件衣服。需要时候就脱下来,但是一旦脱下来,这件衣服就报废了。问最少需要几件衣服。

poj 2955 Brackets(区间dp....括号匹配。。。人生第一道区间dp)

·2 mins
poj2955题目链接 题意:给出若干括号,问最大匹配数是多少。 思路:没有思路。我知道这是dp。。。然后其他就什么都不知道了。。。转移方程? 完全没思路。。知道了转移方程。。。。嗯,还是不会。。。边界怎么写?状态怎么推?循环顺序? 循环次序?我一点思路都没有。。。。。

hdu 1114 - Piggy-Bank (完全背包)

·2 mins
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.