Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2: Master Alchemist After defeating Mateo in their last competition and

ID: 3740464 • Letter: P

Question

Problem 2: Master Alchemist After defeating Mateo in their last competition and proving that he is indeed the Master Alchemist of the realm, Varian sets his sights on mastering some new spells that will vastly increase his power and ensure that no one can ever again challenge his claim to the title of Master Alchemist. In order to cast these spells, Varian needs to build some very special machines to help him Varian wants to cast a total of n spells, with each spell needing a specific type of machine k to be used when the spell is being cast. In order to be most effective, each spell needs to be cast at a very specific time, s and will take d minutes to complete. Varian knows that some of his spells use the same type of machine, and that in total he only needs m unique types of machines. He doesn't want to build more machines than he needs so he plans to reuse machines after spells are cast. How many of each type of machine does Varian need in order to cast all his spells on time. Input Format The first line contains two space separated integers n, and m respectively. The following n lines each contain three space separated integers s, dand k respectively Constraints 1-mc100 Output Format Output m lines, each i of which contains a single integer indicating how many of the th machine Varian will need. Sample Input 0 6 2 021

Explanation / Answer

import java.util.*;

public class bigone {

public static void main(String args[]){

Scanner console=new Scanner(System.in);

int n=console.nextInt();

int m=console.nextInt();

int min[]=new int[n]; //minute array (s values)

int time[]=new int[n]; //time array (d values)

int type[]=new int[n]; //type array(k values)

for(int i=0;i<n;i++) //Storing s d k in 3 different arrays

{

min[i]=console.nextInt();

time[i]=console.nextInt();

type[i]=console.nextInt();

}

//initializing result array

int res[]=new int[m];

for(int i=0;i<m;i++)

res[i]=1;

//sorting all three arrays with reference to minute array

for(int i=0;i<n;i++)

{

for(int j=0;j<n-1;j++)

{

if(min[j]>min[j+1])

{

int te1=min[j];

int te2=time[j];

int te3=type[j];

min[j]=min[j+1];

time[j]=time[j+1];

type[j]=type[j+1];

min[j+1]=te1;

time[j+1]=te2;

type[j+1]=te3;

}

}

}

//sorting all three arrays with reference to type array

for(int i=0;i<n;i++)

{

for(int j=0;j<n-1;j++)

{

if(type[j]>type[j+1])

{

int te1=min[j];

int te2=time[j];

int te3=type[j];

min[j]=min[j+1];

time[j]=time[j+1];

type[j]=type[j+1];

min[j+1]=te1;

time[j+1]=te2;

type[j+1]=te3;

}

}

}

// If you want to check sorting uncomment below lines

/*for(int i=0;i<n;i++)

{

System.out.printf("%d %d %d ",min[i],time[i],type[i]);

}*/

int typ=type[0];

int k=0;

//checking how many machines are requrired of each type

for(int i=0;i<n-1;i++)

{

if(type[i]==typ && type[i+1]==typ)

{

if(min[i]+time[i]>min[i+1])

{

res[k]++;

k++;

}

}

else

{

typ=type[i+1];

}

}

for(int i=0;i<m;i++)

System.out.println(res[i]);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote