Mixtures | CodeChef Solution

Hello coders, today we are going to solve Mixtures CodeChef Solution whose Problem Code is MIXTURES.

Mixtures

Task

Harry 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 Format

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 Format

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

Example

Sample Input

2
18 19
3
40 60 20

Sample Output

342
2400

Explanation

In the second test case, there are two possibilities:

  • first mix 40 and 60 (smoke: 2400), getting 0, then mix 0 and 20 (smoke: 0); total amount of smoke is 2400
  • first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400

The first scenario is the correct approach since it minimizes the amount of smoke produced.

Solution – Mixtures

C++

#include<bits/stdc++.h>
using namespace std;
//long long prearr[1000];
long long arr[1000];
long long storearr[1000][1000];
long long sum( int l,int m)
{ 	
    //cout<<"arr ans=: "<<prearr[m]-prearr[l-1]<<endl;
	return (arr[m]-arr[l-1])%100 ;
}

long long finding( int i,int j)
{
	if(storearr[i][j]!=-1){
		cout<<"whats inside the array ! = "<<storearr[i][j]<<endl;
		cout<<"saays alreaady prestnt  "<<endl;
		return storearr[i][j];
	}
	else{
		if (i>=j){
			cout<<"says i>j..."<<endl;
			return 0;
		}
		else{
			for(int k=i;k<=j-1;k++){
				cout<<"indide main block: "<<endl;
				storearr[i][j]=INT_MAX;
				//cout<<"indide main block: "<<endl;
				long long q = min(storearr[i][j],finding(i,k)+finding(k+1,j)+(sum(i,k)*sum(k+1,j)));
				cout<<"q value: "<<q<<endl;
				storearr[i][j]=q;
			}
		}
	}
	return storearr[i][j];
}

int main()
{
	while(true)
	{
		int n;
		long long ans;
		cin>>n;
		//int n = p-1;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				storearr[i][j]=-1;
			}
		}
	    
	    for(int i=0;i<n;i++){
	    	cin>>arr[i];
		}
	
		for(int i=0;i<n-1;i++)
		{
			arr[i+1]=arr[i+1]+arr[i]; 
		}
		cout<<"whats inside the array ! = "<<storearr[0][n-1]<<endl;
		ans = finding(0,n-1);
		cout<<ans<<endl;
				
		
	}
	return 0;
	
}

Python

import sys

def mix(arr, n):
    dp = [[0 for x in range(n)] for y in range(n)]
    val = [[0 for x in range(n)] for y in range(n)]

    for i in range(n):
        val[i][i] = arr[i]

    for L in range(2, n+1):
        for i in range(0, n-L+1):
            j = i+L-1
            dp[i][j] = sys.maxsize

            for k in range(i, j):
                cost = dp[i][k]+dp[k+1][j]+val[i][k]*val[k+1][j]
                newMix = (val[i][k]+val[k+1][j]) % 100

                if cost < dp[i][j]:
                    dp[i][j] = cost
                    val[i][j] = newMix

    return dp[0][n-1]
    
    
try:    
    while(True):
        n = int(input())
        arr = list(map(int,input().split()))
        print(mix(arr, n))
except:
    pass

Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

class Test
{
    public static void main(String[]args)throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        while(br.ready()){
            int n=Integer.parseInt(br.readLine());
            StringTokenizer st=new StringTokenizer(br.readLine());
            long[][]smoke=new long[n][n];
            int[][]color=new int[n][n];
            for(int i=0;i<n;i++)
                color[i][i]=Integer.parseInt(st.nextToken());
            for(int i=1;i<n;i++){
                for(int j=0;j<n-i;j++){
                        long mins=Long.MAX_VALUE;
                        int minc=0;
                        int ii=j;
                        int jj=j+i;
                        for(int k=ii;k<jj;k++){
                            int a=color[ii][k];
                            int b=color[k+1][jj];
                            long s=((long)(a*b))+smoke[ii][k]+smoke[k+1][jj];
                            if(s<mins){
                                mins=s;
                                minc=(a+b)%100;
                            }
                        }
                        smoke[ii][jj]=mins;
                        color[ii][jj]=minc;
                    }
                }
            System.out.println(smoke[0][n-1]);
        }
    }
}

Disclaimer: The above Problem (Mixtures) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *