zoukankan      html  css  js  c++  java
  • poj 1651 区间dp

    Multiplication Puzzle
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8487   Accepted: 5291

    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

    Source

    Northeastern Europe 2001, Far-Eastern Subregion
     
    题意:给你一组数字,第一个和最后一个数字不可以取出去,其它任意取出去,当你要取出一个数字时,它有一个代价,这个代价就是与它相邻的两个数的乘积,求除了首位两位数字,把其他数字都取出来,它们的代价之和的最小值
     
    题解:dp[i][j] 表示i~j区间内的代价之和的最小值
    考虑,因为首位与末尾不能取出 所以总有中间的一个数被最后取出 所以可以枚举这个最后取出的数,也就是把当前的区间分解了
    不断的分解下去直到某段区间只有两个数。
    另外关于递推 为什么是逆序的
    for(int i=n; i>=1; i--)
               for(int j=i+1; j<=n; j++)
    把i,j输出来可以发现 dp[1][n]是最后计算的 也就是整个的最优,考虑dp的后效性。个人想法
    /******************************
    code by drizzle
    blog: www.cnblogs.com/hsd-/
    ^ ^    ^ ^
     O      O
    ******************************/
    //#include<bits/stdc++.h>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    #include<bitset>
    #include<map>
    //#define ll long long
    #define mod 1000000007
    #define PI acos(-1.0)
    using namespace std;
    int n;
    int a[105];
    int score[105][105];
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1; i<=n; i++)
                scanf("%d",&a[i]);
            memset(score,0,sizeof(score));
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    score[i][j]=mod;
            for(int i=1; i<n; i++)
                score[i][i+1]=0;
            for(int i=n; i>=1; i--)
            {
                for(int j=i+1; j<=n; j++)
                {
                    for(int k=i+1; k<j; k++)
                        score[i][j]=min(score[i][j],score[i][k]+score[k][j]+a[k]*a[i]*a[j]);
                }
            }
            cout<<score[1][n]<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    Redis 客户端
    Redis 数据库
    Redis 对象
    Redis 跳跃表
    Redis 字典
    Redis 链表实现
    Redis 事务
    winform,自定义文本框控件 实现文本框验证加水印功能
    WinForm中,每隔一段时间(参数)调用一次函数(使用定时器)
    GMap.Net 实现离线地图
  • 原文地址:https://www.cnblogs.com/hsd-/p/5716793.html
Copyright © 2011-2022 走看看