题目链接:http://poj.org/problem?id=1651
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5000 | Accepted: 2988 |
Description
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
Output
Sample Input
6 10 1 50 50 20 5
Sample Output
3650
Source
区间DP
dp[0][n-1]表示答案。
求解dp[i][j]的时候,就是枚举[i+1,j-1]中最后删除的元素。
dp[i][j]=min(a[k]*a[i]*a[j]+dp[i][k]+dp[k][j]) i<k<j
代码:
//============================================================================ // Name : POJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; const int MAXN=110; const int INF=0x3f3f3f3f; int a[MAXN]; int dp[MAXN][MAXN]; int solve(int i,int j) { if(dp[i][j]!=INF)return dp[i][j]; if(j==i+1)return dp[i][j]=0; for(int k=i+1;k<j;k++) dp[i][j]=min(dp[i][j],a[k]*a[i]*a[j]+solve(i,k)+solve(k,j)); return dp[i][j]; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; while(scanf("%d",&n)==1) { for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j]=INF; printf("%d\n",solve(0,n-1)); } return 0; }