Pagini recente » Cod sursa (job #1237253) | Cod sursa (job #716537) | Borderou de evaluare (job #158045) | Cod sursa (job #1748668) | Cod sursa (job #2695302)
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
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 List<Integer> list;
public CautBin(List<Integer> list) {
this.list = list;
}
public int biggestIndexWithValue(int value) {
int idx = -1;
int lo = 0;
int hi = list.size() - 1;
while (lo <= hi) {
int mid = lo + ((hi - lo) / 2);
if (list.get(mid) == value) {
idx = mid;
lo = mid + 1;
} else if (list.get(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 = list.size() - 1;
while (lo <= hi) {
int mid = lo + ((hi - lo) / 2);
if (list.get(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 = list.size() - 1;
while (lo <= hi) {
int mid = lo + ((hi - lo) / 2);
if (list.get(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();
List<Integer> list = new ArrayList<>();
for (int idx = 0; idx < numOfElements; ++idx) {
list.add(fileParser.getNextInt());
}
CautBin cautBin = new CautBin(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());
}
}
}