zoukankan      html  css  js  c++  java
  • POJ

    Problem Describe

    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
    10150 + 50205 + 10505 = 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
    15020 + 1205 + 1015 = 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

    题意 : 给出N个元素,第一个和第n个不能选,每次选择第k个,获得点数 a[k]=a[k-1]*a[k]*a[k+1],选到只剩第一个和最后一个为止。求最小点数

    思路 : 区间dp ,令 dp[i][j]代表区间 i -> j 的最小值 那么对于区间 i -> j
    dp[i][j] = min(dp[i][j] ,dp[i][k] + dp[k][j] + v[i] * v[j] * v[k] );

    AC code :

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    const int maxn = 1e3+50;
    const int inf = 0x3f3f3f3f;
    
    ll dp[maxn][maxn] ,v[maxn] ;
    int n ;
    
    int main() {
    	while(~scanf("%d",&n)) {
    		for (int i = 1;i<=n;i++) scanf("%lld",&v[i]);
    		memset(dp ,0 ,sizeof(dp) );
    		for (int i = 1 ; i <= n - 2 ; i ++) {
    			dp[i][i+2] = v[i] * v[i+1] * v[i+2] ;
    		}
    		for (int l = 4 ; l <= n ; l ++ ) {
    			for (int i = 1 ; i <= ( n - l + 1 ) ; i ++ ) {
    				int j = i + l - 1 ;
    				dp[i][j] = inf ;
    				for (int k = i + 1 ;k <= j - 1 ; k ++ ) {
    					dp[i][j] = min(dp[i][j] ,dp[i][k] + dp[k][j] + v[i] * v[j] * v[k] ) ;
    				}
    			}
    		}
    		printf("%lld
    ",dp[1][n]);
    	}	
    	return 0;
    }
    
  • 相关阅读:
    iOS 与 惯性滚动
    前端性能优化--为什么DOM操作慢?
    React虚拟DOM浅析
    DOM性能瓶颈与Javascript性能优化
    React 组件性能优化
    重绘与回流——影响浏览器加载速度
    移动前端开发之viewport的深入理解
    [转] 前后端分离开发模式的 mock 平台预研
    [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读
    关联规则( Association Rules)之频繁模式树(FP-Tree)
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745938.html
Copyright © 2011-2022 走看看