Cod sursa(job #2695303)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 12 ianuarie 2021 14:00:26
Problema Cautare binara Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 4.32 kb
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

class FileParser {

    private char[] fileContent;

    private int currentFileContentIndex;

    public FileParser(String filename) {
        this.currentFileContentIndex = -1;

        try {
            fileContent = new String(Files.readAllBytes(Paths.get(filename))).toCharArray();
            currentFileContentIndex = 0;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    int getNextInt() {
        if (currentFileContentIndex == -1 || currentFileContentIndex >= fileContent.length) {
            System.out.println("Reached end of file");
            return 0;
        }

        int idx = currentFileContentIndex;

        while (idx < fileContent.length
                && (fileContent[idx] == ' '
                    || fileContent[idx] == '\r'
                    || fileContent[idx] == '\n')) {
            ++idx;
        }

        int numberSign = 1;
        if (idx < fileContent.length && fileContent[idx] == '-') {
            numberSign = -1;
            ++idx;
        }

        int number = 0;
        while (idx < fileContent.length && Character.isDigit(fileContent[idx])) {
            number = number * 10 + fileContent[idx] - '0';
            ++idx;
        }

        currentFileContentIndex = idx;

        return number * numberSign;
    }
}

class CautBin {
    private int numOfElements;
    private int[] list;

    public CautBin(int numOfElements, int[] list) {
        this.numOfElements = numOfElements;
        this.list = list;
    }

    public int biggestIndexWithValue(int value) {
        int idx = -1;

        int lo = 0;
        int hi = numOfElements - 1;

        while (lo <= hi) {
            int mid = lo + ((hi - lo) / 2);
            if (list[mid] == value) {
                idx = mid;
                lo = mid + 1;
            } else if (list[mid] < value) {
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }

        return idx + 1;
    }

    public int biggestIndexWithValueLessOrEqual(int value) {
        int idx = -1;

        int lo = 0;
        int hi = numOfElements - 1;

        while (lo <= hi) {
            int mid = lo + ((hi - lo) / 2);
            if (list[mid] <= value) {
                idx = mid;
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }

        return idx + 1;
    }

    public int lowestIndexWithValueHighOrEqual(int value) {
        int idx = -1;

        int lo = 0;
        int hi = numOfElements - 1;

        while (lo <= hi) {
            int mid = lo + ((hi - lo) / 2);
            if (list[mid] >= value) {
                idx = mid;
                hi = mid - 1;
            } else {
                lo = mid + 1;
            }
        }

        return idx + 1;
    }
}

class Main {

    private final static String INPUT_FILE = "cautbin.in";
    private final static String OUTPUT_FILE = "cautbin.out";

    public static void main(String[] args) {

        FileParser fileParser = new FileParser(INPUT_FILE);

        int numOfElements = fileParser.getNextInt();
        int[] list = new int[numOfElements];
        for (int idx = 0; idx < numOfElements; ++idx) {
            list[idx] = fileParser.getNextInt();
        }

        CautBin cautBin = new CautBin(numOfElements, list);

        StringBuilder output = new StringBuilder();

        int numOfQueries = fileParser.getNextInt();
        for (int query = 0; query < numOfQueries; ++query) {
            int queryType = fileParser.getNextInt();
            int value = fileParser.getNextInt();
            if (queryType == 0) {
                output.append(cautBin.biggestIndexWithValue(value)).append("\n");
            } else if (queryType == 1) {
                output.append(cautBin.biggestIndexWithValueLessOrEqual(value)).append("\n");
                System.out.println(cautBin.biggestIndexWithValueLessOrEqual(value));
            } else if (queryType == 2) {
                output.append(cautBin.lowestIndexWithValueHighOrEqual(value)).append("\n");
            }
        }

        try {
            Path path = Paths.get(OUTPUT_FILE);
            Files.write(path, output.toString().getBytes());
        }  catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}