Job Scheduling with two jobs allowed at a time | CPP program to check if all jobs can be scheduled if two jobs are allowed at a time . ; making a pair of starting and ending time of job ; sorting according to starting time of job ; starting first and second job simultaneously ; Checking if starting time of next new job is greater than ending time of currently scheduled first job ; Checking if starting time of next new job is greater than ending time of ocurrently scheduled second job ; Driver code ; int startin [ ] = { 1 , 2 , 4 } ; starting time of jobs int endin [ ] = { 2 , 3 , 5 } ; ending times of jobs
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkJobs ( int startin [ ] , int endin [ ] , int n ) { vector < pair < int , int > > a ; for ( int i = 0 ; i < n ; i ++ ) a . push_back ( make_pair ( startin [ i ] , endin [ i ] ) ) ; sort ( a . begin ( ) , a . end ( ) ) ; long long tv1 = a [ 0 ] . second , tv2 = a [ 1 ] . second ; for ( int i = 2 ; i < n ; i ++ ) { if ( a [ i ] . first >= tv1 ) { tv1 = tv2 ; tv2 = a [ i ] . second ; } else if ( a [ i ] . first >= tv2 ) tv2 = a [ i ] . second ; else return false ; } return true ; } int main ( ) { int n = sizeof ( startin ) / sizeof ( startin [ 0 ] ) ; cout << checkJobs ( startin , endin , n ) ; return 0 ; }