Sort the array based on the arrival times
Arrays . sort ( ar , 1 , n , new Comparator < util > ( ) { public int compare ( util a , util b ) { if ( a . at == b . at ) return a . id - b . id ; return a . at - b . at ; } } ) ; for ( int i = 1 ; i <= n ; i ++ ) mp [ ar [ i ] . id ] = i ;