SPOJ Mixtures Interval DP

Source: Internet
Author: User

Harry Potter had n mixtures in front of him, arranged in a row. Each mixture has one of the different colors (colors having numbers from 0 to 99).

He wants to mix all these mixtures together. At each step, he's going to take the mixtures that stand next to each other and mix them together, and put the resulting Mixture in their place.

When mixing mixtures of colors A and B, the resulting mixture would have the color (a+b) mod 100.

Also, there'll is some smoke in the process. The amount of smoke generated when mixing-mixtures of colors A and B is a*b.

Find out what's the minimum amount of smoke that Harry can get when mixing all the mixtures together.


There'll is a number of test cases in the input.

The first line of all test case would contain N, the number of mixtures, 1 <= n <= 100.

The second line would contain n integers between 0 and 99-the initial colors of the mixtures.


For each test case, output the minimum amount of smoke.

Input:218 19340Output:3422400

In the second test case, there is both possibilities:

    • First Mix (smoke:2400), getting 0, then Mix 0 and (smoke:0); Total amount of smoke is 2400
    • First mix and smoke:1200, getting, then Mix (smoke:3200); Total amount of smoke is 4400

The first scenario is a much better-to proceed.


This is a stupid interval DP, there is no difficulty, pre-processing the interval and can

See the code:

#include <iostream> #include <cstring> #include <algorithm> #define MAX_N 105#define INF 0x3f3f3f3fusing namespace Std;int dp[max_n][max_n];int sum[max_n];int n;int a[max_n];int Main () {Cin.sync_with_stdio (f    Alse);                while (CIN >> N) {for (int i = 0, I <= N; i++) for (int j = 0; J <= N; j + +)        DP[I][J] = INF;        memset (sum, 0, sizeof (sum));            for (int i = 1; I <= n; i++) {cin >> a[i];            Dp[i][i] = 0;        Sum[i] = Sum[i-1] + a[i]; } for (int i = 1; I <= n; i++.) for (int j = 1; j + I <= N, j + +) for (int k = j; K & Lt j + i;                                                                k++) Dp[j][j + i] = min (dp[j][j + i], Dp[j][k] + dp[k + 1][j + i] +                                                                ((Sum[k]-sum[j-1])% 100) *        ((Sum[j + i]-sum[k]) (100)); cout << dp[1][n] << endl } return 0;}


SPOJ Mixtures Interval DP

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.