Monday, September 17, 2018

Mixtures

  • Problem Description

    "Potter has n mixtures in front of him, arranged in a row.Each mixture has one of 100 different colors (colors have numbers from 0 to 99).

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

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

    Also, there will be some smoke in the process. The amount of smoke generated when mixing two mixtures of colors a and b is a*b.

    Find out what is the minimum amount of smoke that Harry can get when mixing all the ixtures together.

    Input

    There will be a number of test cases in the input.

    The first line of each test case will contain n, the number of mixtures, 1 <= n <= 100.

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

    Output

    For each test case, output the minimum amount of smoke.
  • CODING ARENA
  • #include<stdio.h>
     
    int main()
    {
    int num_mixtures, i, j, k;
    while (scanf("%d", &num_mixtures) != EOF)
    {
    int mix[num_mixtures];
    for (i=0; i<num_mixtures; i++)
    {
    scanf("%d", &mix[i]);
    }
    int smoke[num_mixtures][num_mixtures];
    int color[num_mixtures][num_mixtures];
     
    int sum = 0;
     
    for (i=0; i<num_mixtures; i++)
    {
    sum = color[i][i] = mix[i];
    for (j=i+1; j<num_mixtures; j++)
    {
    sum += mix[j];
    color[i][j] = sum % 100;
    }
    }
     
    for (i=0; i<num_mixtures; i++)
    smoke[i][i] = 0;
     
    int l, curr_val;
     
    for (l=2; l<=num_mixtures; l++)
    {
    for (i = 0; i<=num_mixtures-l+1; i++)
    {
    j = i+l-1;
    smoke[i][j] = 0x7fffffff;
    for (k=i; k<j; k++)
    {
    curr_val = smoke[i][k] + smoke[k+1][j] + color[i][k] * color[k+1][j];
    if (curr_val < smoke[i][j])
    smoke[i][j] = curr_val;
    }
    }
    }
    printf("%d\n", smoke[0][num_mixtures-1]);
    }
    return 0;
  • Test Case 1

    Input (stdin)
    5
    
    18 20 45 76 89
    
    4 
    
    40 50 20 99
    
    
    Expected Output
    6029
    
    4790
  • Test Case 2

    Input (stdin)
    2
    
    18 20
    
    3 
    
    40 50 20
    
    
    Expected Output
    360
    
    3800

No comments:

Post a Comment