1

In a competitive programming competition, I got the following problem famously known as 'Shoemaker's Problem':

"A shoemaker has N orders from customers that he must execute. The shoemaker can work on only one job each day. For each job i, it takes Ti days for the shoemaker to finish the job, where Ti is an integer and (1 ≤ Ti ≤ 1000). For each day of delay before starting to work for the job i, shoemaker must pay a fine of Si (1 ≤ Si ≤ 10000) rupees. Your task is to help the shoemaker find the sequence in which to complete the jobs so that his total fine is minimized. If multiple solutions are possible, print the one that is lexicographically least (i.e., earliest in dictionary order)."

For the solution of this problem, I tried to minimize the Si/Ti and then arrange them in the array.

My solution in java is as follows:

import java.util.Arrays;
import java.util.Scanner;
class Order implements Comparable<Order>
{

int time;
int fine;
int index;

public static void main(String...s)
{

   Scanner sc= new Scanner(System.in);
   int numOfElements=sc.nextInt();
   Order[] orders= new Order[numOfElements];
   for(int i=0;i<numOfElements;i++)
   {
       Order o=new Order();
       o.time=sc.nextInt();
       o.fine=sc.nextInt();
       o.index=i+1;
       orders[i]=o;
   }
   Arrays.sort(orders);
   for(Order o:orders)
   {
      System.out.println(o.index);
   }
}

@Override
public int compareTo(Order o)
  {
   int b=this.fine*o.time;
   int a =o.fine*this.time;
   return a-b;
  }
}

So after submitting the solution, I got a score of 30 out of 100.

--Now I am using already present Arrays.sort(), which according to the following question asked, uses Mergesort for sorting arrays of objects:

Why does Java's Arrays.sort method use two different sorting algorithms for different types?

My question is it that, did my approach failed on efficiency or my logic would have failed on large input.

Thanks in advance...

Deep
  • 929
  • 2
  • 16
  • 32
  • isn't the basis of comparison Si/Ti? So the task with the maximum value of Si/Ti gets done first and so on... – Debanik Dawn Aug 17 '17 at 19:16
  • @DebanikDawn, yes for comparing Si/Ti (that is a fraction), I have used the usual way of comparing fractions, for example, suppose we have N1/D1 and N2/D2 the larger out of two is based on following N1*D2 or N2*D1. – Deep Aug 17 '17 at 19:23
  • How are we supposed to know how some score is calculated on some other website... But there's probably a 99.99% chance that you would **not** have gotten a significantly better score if Java used some other generic sorting algorithm. – Bernhard Barker Aug 17 '17 at 20:15

0 Answers0