Minimum cost to process m tasks where switching costs | C ++ Program to find out minimum cost to process m tasks ; Function to find out the farthest position where one of the currently ongoing tasks will rehappen . ; Iterate form last to current position and find where the task will happen next . ; Find out maximum of all these positions and it is the farthest position . ; Function to find out minimum cost to process all the tasks ; freqarr [ i ] [ j ] denotes the frequency of type j task after position i like in array { 1 , 2 , 1 , 3 , 2 , 1 } frequency of type 1 task after position 0 is 2. So , for this array freqarr [ 0 ] [ 1 ] = 2. Here , i can range in between 0 to m - 1 and j can range in between 0 to m ( though there is not any type 0 task ) . ; Fill up the freqarr vector from last to first . After m - 1 th position frequency of all type of tasks will be 0. Then at m - 2 th position only frequency of arr [ m - 1 ] type task will be increased by 1. Again , in m - 3 th position only frequency of type arr [ m - 2 ] task will be increased by 1 and so on . ; isRunning [ i ] denotes whether type i task is currently running in one of the cores . At the beginning no tasks are running so all values are false . ; cost denotes total cost to assign tasks ; truecount denotes number of occupied cores ; iterate through each task and find the total cost . ; ele denotes type of task . ; Case 1 : if same type of task is currently running cost for this is 0. ; Case 2 : same type of task is not currently running . ; Subcase 1 : if there is at least one free core then assign this task to that core at a cost of 1 unit . ; Subcase 2 : No core is free ; set minimum frequency to a big number ; set index of minimum frequency task to 0. ; find the minimum frequency task type ( miniind ) and frequency ( mini ) . ; If minimum frequency is zero then just stop the task and start the present task in that core . Cost for this is 1 unit . ; If minimum frequency is nonzero then find the farthest position where one of the ongoing task will rehappen . Stop that task and start present task in that core . ; find out the farthest position using find function ; return total cost ; Driver Program ; Test case 1 ; Test case 2 ; Test case 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( vector < int > arr , int pos , int m , vector < bool > isRunning ) { vector < int > d ( m + 1 , 0 ) ; for ( int i = m ; i > pos ; i -- ) { if ( isRunning [ arr [ i ] ] ) d [ arr [ i ] ] = i ; } int maxipos = 0 ; for ( int ele : d ) maxipos = max ( ele , maxipos ) ; return maxipos ; } int mincost ( int n , int m , vector < int > arr ) { vector < vector < int > > freqarr ( m ) ; vector < int > newvec ( m + 1 , 0 ) ; freqarr [ m - 1 ] = newvec ; for ( int i = m - 2 ; i >= 0 ; i -- ) { vector < int > nv ; nv = freqarr [ i + 1 ] ; nv [ arr [ i + 1 ] ] += 1 ; freqarr [ i ] = nv ; } vector < bool > isRunning ( m + 1 , false ) ; int cost = 0 ; int truecount = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int ele = arr [ i ] ; if ( isRunning [ ele ] == true ) continue ; else { if ( truecount < n ) { cost += 1 ; truecount += 1 ; isRunning [ ele ] = true ; } else { int mini = 100000 ; int miniind = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( isRunning [ j ] && mini > freqarr [ i ] [ j ] ) { mini = freqarr [ i ] [ j ] ; miniind = j ; } } if ( mini == 0 ) { isRunning [ miniind ] = false ; isRunning [ ele ] = true ; cost += 1 ; } else { int farpos = find ( arr , i , m , isRunning ) ; isRunning [ arr [ farpos ] ] = false ; isRunning [ ele ] = true ; cost += 1 ; } } } } return cost ; } int main ( ) { int n1 = 3 ; int m1 = 6 ; vector < int > arr1 { 1 , 2 , 1 , 3 , 4 , 1 } ; cout << mincost ( n1 , m1 , arr1 ) << endl ; int n2 = 2 ; int m2 = 6 ; vector < int > arr2 { 1 , 2 , 1 , 3 , 2 , 1 } ; cout << mincost ( n2 , m2 , arr2 ) << endl ; int n3 = 3 ; int m3 = 31 ; vector < int > arr3 { 7 , 11 , 17 , 10 , 7 , 10 , 2 , 9 , 2 , 18 , 8 , 10 , 20 , 10 , 3 , 20 , 17 , 17 , 17 , 1 , 15 , 10 , 8 , 3 , 3 , 18 , 13 , 2 , 10 , 10 , 11 } ; cout << mincost ( n3 , m3 , arr3 ) << endl ; return 0 ; }