"Tyvj P1014" multiplication game

Source: Internet
Author: User

Time: 1000ms/Space: 131072kib/java class name: Main background Taiyuan Cheng-Cheng Middle School 2nd time simulation Fourth Road description multiplication game is performed on a line of cards. Each card consists of a positive integer. In each move, the player takes out a card, the score is multiplied by its number to the left and the right, so it is not allowed to take the 1th Zhang and last 1 cards. After the last move, there are only two cards left.
Your goal is to make the score and the smallest.
For example, if the number is 10 1 50 20 5, take 1, 20, 50, the total score is 10*1*50+50*20*5+10*50*5=8000
And take 50, 20, 1, the total score is 1*50*20+1*20*5+10*1*5=1150. Input format the first line of the input file includes the number of cards (3<=n<=100), the second line contains n 1-100 integers, separated by a space. Output format output file has only one number: Minimum score test Sample 1 input
6
1 5
Output
3650
TYVJ evaluation machine is not.  I have to get the test data. This problem is obviously a dynamic programming problem, and it is the interval motion regulation.  So how to establish a State transfer.  First, it is easy to think that the large interval is from the community between the introduction, then there will be an optimal sub-structure. Then, no matter how the community between the big interval can not affect the answer.  Then the state transfer equation is in the open.  Set F[1][n-2] is the answer. F[I][J] = Min{f[i][k-1] + f[k + 1][j] + a[i-1] * a[k] * a[j + 1] | I < K < J} But we have to preprocess F[i][i] and F[i][i + 1] Case and put f[i][j] | The value of J < i is set to 0.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6 using namespacestd;7 8 Long Longf[ the][ the];9 inta[ the];Ten intN; One  A intMain () - { -scanf"%d", &n); the      for(inti =0; I < n; ++i) -scanf"%d", &a[i]); -Memset (F,0x7f,sizeof(f)); -f[0][0] = F[n-1][n-1] =0; +      for(inti =1; I < n-1; ++i) F[i][i] = a[i-1] * A[i] * a[i +1]; -      for(inti =1; I < n-2; ++i) +F[i][i +1] = min (F[i][i] + f[i +1][i +1]/a[i] * a[i-1], F[i +1][i +1] + f[i][i]/a[i +1] * a[i +2]); A      for(inti =1; I < n; ++i) at          for(intj =0; J < I; ++j) -F[I][J] =0; -      for(inti = n-2; i >0; --i) -          for(intj = i +1; J < N-1; ++j) -              for(intK = i; K <= J; ++k) -F[i][j] = min (f[i][j], f[i][k-1] + f[k +1][J] + a[i-1] * A[k] * a[j +1]); incout << f[1][n-2] <<Endl; -     //System ("pause"); to     return 0; +}

"Tyvj P1014" multiplication game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.