0

I am wondering why my code keeps having this "time limit exceed" on the uva-online judge page (http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2511), it is supposed to execute in 1 second but I don't know which input they use (my code works and do exactly what it supposed).. I am wondering that maybe the while loop of the testCases have something to do, because when I remove it it says wrong answer.. this is my code:

public class Main {

private static final int TAM = 10000; // TAM equals to the posible numbers of houses
private static int[] auxHouses;
private static int nHouses[];
private static int testCases;
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    nHouses = new int[TAM];
    // First we read the input
    // Read the variable of testCases
    testCases = scanner.nextInt();
    while (testCases > 0) {
        float sol = binarySearch(testCases);
        System.out.println(sol);
        testCases--;
    }

}

public static float binarySearch(int tC) {
    int routers = 0, houses = 0;
    int pivot = 0;
    int hi = 0;
    // While for the testCases

    routers = scanner.nextInt();
    houses = scanner.nextInt();

    // Read the numbers of the houses
    for (int i = 0; i < houses; i++) {
        nHouses[i] = scanner.nextInt();
    }

    if (routers >= houses) {
        return 0;
    }
    // First we sort the array
    sortHouses(nHouses, houses);

    // Prepare the variables of the index
    int lo = 0;
    hi = 2 * (nHouses[houses - 1] - nHouses[0] + 1); // 2*(loc[h-1]-loc[0]+1);

    // Now we execute the binary search algorithm
    while (hi > lo) {
        pivot = (lo + hi) / 2;
        int start = nHouses[0];
        int need = 1;
        for (int i = 0; i < houses; i++) {
            if (nHouses[i] > start + pivot) {
                start = nHouses[i];
                need++;
            }
        }
        if (need > routers) {
            lo = pivot + 1;
        } else {
            hi = pivot;
        }
    }
    return (float) hi / 2;
}

public static void sortHouses(int[] nhouses, int length) {

    // First we check if the are actually values on the array
    if (nhouses.length == 0) {
        return;
    }
    // Copy the array of int into an auxiliary variable and the numbers of
    // int in the array
    auxHouses = nhouses;
    int lengthArray = length;
    quickSort(0, lengthArray - 1);

}

public static void quickSort(int low, int high) {
    // System.out.println("Array " + Arrays.toString(auxHouses));
    int i = low, j = high;
    // Get the pivot element from the middle of the list
    int pivot = auxHouses[low + (high - low) / 2];

    // Divide into two lists
    while (i <= j) {
        // If the current value from the left list is smaller then the pivot
        // element then get the next element from the left list
        while (auxHouses[i] < pivot) {
            i++;
        }
        // If the current value from the right list is larger then the pivot
        // element then get the next element from the right list
        while (auxHouses[j] > pivot) {
            j--;
        }

        // If we have found a values in the left list which is larger then
        // the pivot element and if we have found a value in the right list
        // which is smaller then the pivot element then we exchange the
        // values.
        // As we are done we can increase i and j
        if (i <= j) {
            exchange(i, j);
            i++;
            j--;
        }
    }
    // Recursion
    if (low < j)
        quickSort(low, j);
    if (i < high)
        quickSort(i, high);
}

private static void exchange(int i, int j) {
    int temp = auxHouses[i];
    auxHouses[i] = auxHouses[j];
    auxHouses[j] = temp;
}
}

I have it with the quicksort implemented, but if you use Arrays.sort(..) method instead it says the same thing..TLE, what could be doing wrong?

neteot
  • 933
  • 1
  • 12
  • 33
  • Please define "TLE", and indicate what you are referring to when you say "on the uva online judge page". Perhaps provide a preamble describing what you are trying to do (the high-level problem). You have jumped into the middle of something that all of us might not immediately recognize. – ErstwhileIII Feb 24 '14 at 17:26
  • sorry for that, thank you! – neteot Feb 24 '14 at 17:38

0 Answers0