diff --git a/binary-search-for-the-answer/BinarySearchApp.java b/binary-search-for-the-answer/BinarySearchApp.java new file mode 100644 index 0000000..9dcc686 --- /dev/null +++ b/binary-search-for-the-answer/BinarySearchApp.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class BinarySearchApp { + + private int[] array; + + private boolean Condition(int x, int k) { + int lines = 1; + int last_line = array[0]; + for (int i = 0; i < array.length; i++) { + if (array[i] - last_line >= x) { + lines++; + last_line = array[i]; + } + } + if (lines>k) return true; + else return false; + } + + public int Solution(int k) { + int left = 0; + int right = array[array.length-1]-array[0]+1; + while (right - left > 1) { + int mid = (left + right) / 2; + if(Condition(mid, k)) { + left = mid; + } else { + right = mid; + } + } + return left; + } + + public static void main(String[] args) { + BinarySearchApp binSearchForAnswer = new BinarySearchApp(); + Scanner scanner = new Scanner(System.in); + int N = Integer.parseInt(scanner.nextLine()); + int k = Integer.parseInt(scanner.nextLine()); + + binSearchForAnswer.array = new int[N]; + for (int i = 0;i < N; i++) { + binSearchForAnswer.array[i] = Integer.parseInt(scanner.nextLine()); + } + System.out.println(binSearchForAnswer.Solution(k)); + } +} diff --git a/binary-search-tree/BinaryTree b/binary-search-tree/BinaryTree new file mode 100644 index 0000000..ac7fc6d --- /dev/null +++ b/binary-search-tree/BinaryTree @@ -0,0 +1,140 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class BinaryTree { + + Node root; + + static class Node { + int value; + Node left; + Node right; + + Node(int value) { + this.value = value; + right = null; + left = null; + } + } + + public static void inTests(String fileIn, ArrayList input) throws IOException { + + Scanner sc = new Scanner(new File(fileIn)); + + while (sc.hasNextInt()) { + int str = sc.nextInt(); + input.add(str); + } + sc.close(); + } + + + public static void outTests_min_after(String fileOut, ArrayList output_contains, ArrayList output_min_after) throws IOException { + + FileWriter out = new FileWriter(fileOut); + + for (int i = 0; i < output_contains.size(); i++) { + out.write(String.format("%s %s\n", output_contains.get(i), output_min_after.get(i))); + } + out.close(); + } + public static void outTests_contains(String fileOut, ArrayList output) throws IOException { + + FileWriter out = new FileWriter(fileOut); + + for (String sign : output) out.write(String.format("%s\n", sign)); + out.close(); + } + + + public static void split(ArrayList input, ArrayList tree) { + + int N = input.get(0); + for (int i = 0; i < N; i++) tree.add(i, input.get(i + 1)); + + } + + + private Node addRecursive(Node current, int value, ArrayList output) { + if (current == null) { + output.add("-"); + return new Node(value); + } + + if (value < current.value) { + if (current.left != null) { + current.left = addRecursive(current.left, value, output); + } else { + current.left = new Node(value); + output.add("-"); + } + } else if (value > current.value) { + if (current.right != null) { + current.right = addRecursive(current.right, value, output); + } else { + current.right = new Node(value); + output.add("-"); + } + } else { + output.add("+"); + return current; + } + + return current; + } + + public void addElement(int value, ArrayList output) { + root = addRecursive(root, value, output); + } + + /////////Поиск следующего элемента + public void findNext(int value, ArrayList output) { + Node current = root; + Node next = null; + while (current != null) { + if (value >= current.value) { + current = current.right; + } else { + next = current; + current = current.left; + } + } + if (next != null) { + output.add(String.valueOf(next.value)); + } else { + output.add("-"); + } + } + + private static void createBinaryTree(ArrayList tree, ArrayList output, ArrayList output_min_after) { + BinaryTree bt = new BinaryTree(); + + for (Integer integer : tree) { + bt.addElement(integer, output); + bt.findNext(integer, output_min_after); + } + } + + public static void main(String[] args) { + + for (int i = 1; i <= 4; i++) { + ArrayList input = new ArrayList<>(); + ArrayList tree = new ArrayList<>(); + ArrayList output_contains = new ArrayList<>(); + ArrayList output_min_after = new ArrayList<>(); + + try { + inTests(String.format("%d.in", i), input); + split(input, tree); + createBinaryTree(tree, output_contains, output_min_after); + outTests_contains(String.format("ans%d.contains.out", i), output_contains); + outTests_min_after(String.format("ans%d.min-after.out", i), output_contains, output_min_after); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/binary-search/Main b/binary-search/Main new file mode 100644 index 0000000..b767c85 --- /dev/null +++ b/binary-search/Main @@ -0,0 +1,75 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Main { + + public static void InTests(String fileIn, ArrayList input) throws IOException { + + Scanner sc = new Scanner(new File(fileIn)); + + while (sc.hasNextInt()) { + int str = sc.nextInt(); + input.add(str); + } + sc.close(); + } + + + public static void outTests(String fileOut, ArrayList output) throws IOException { + + FileWriter out = new FileWriter(fileOut); + + for (Integer integer : output) out.write(String.format("%d\n", integer)); + out.close(); + } + public static void split(ArrayList input, ArrayList arr, ArrayList x_i) { + + int N = input.get(0); + for (int i = 0; i arr, ArrayList x_i, ArrayList output) { + + for (int i = 0; i x_i.get(i)) { + right = mid - 1; + } else left = mid + 1; + mid = (left + right) / 2; + } + if (left <= right) { + output.add(i, mid); + } else output.add(i, -1); + } + } + + public static void main(String[] args) { + + for (int i = 1; i <= 5; i++) { + ArrayList input = new ArrayList<>(); + ArrayList arr = new ArrayList<>(); + ArrayList x_i = new ArrayList<>(); + ArrayList output = new ArrayList<>(); + + try { + InTests(String.format("%d.in", i), input); + split(input, arr, x_i); + search(arr, x_i, output); + OutTests(String.format("ans%d.out", i), output); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/coins-system/coinSystem b/coins-system/coinSystem new file mode 100644 index 0000000..783ab67 --- /dev/null +++ b/coins-system/coinSystem @@ -0,0 +1,113 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class Mavenproject4 { + + public static boolean InTests (String fileIn, ArrayList input) throws IOException { + + File file = new File(fileIn); + if (file.exists()) { + Scanner sc = new Scanner(file); + while (sc.hasNextInt()) { + int str = sc.nextInt(); + input.add(str); + } + sc.close(); + return true; + } else return false; + } + + + public static void outTests(String fileOut, ArrayList output) throws IOException { + + FileWriter out = new FileWriter(fileOut); + + for (Integer integer : output) out.write(String.format("%d\n", integer)); + out.close(); + } + + + + public static void split(ArrayList input, ArrayList nominal, int k, ArrayList nominalSort) { + + for (int i = 0; i < k; i++) { + nominal.add(i, input.get(i + 1)); + nominalSort.add(i, input.get(i + 1)); + } + + } + + + + public static void findCoins(ArrayList nominalSort, int sum, ArrayList coinsMin, ArrayList coinsUsed) { + int coinCount; + + Collections.sort(nominalSort); + for (int i = 0; i <= sum + 1; i++) { + coinCount = i; + int newCoin = nominalSort.get(0); + + for (int j : nominalSort) { + if (j > i) { + continue; + } + if (coinsMin.get(i - j) + 1 < coinCount) { + coinCount = coinsMin.get(i - j) + 1; + newCoin = j; + } + } + coinsMin.add(i, coinCount); + coinsUsed.add(i, newCoin); + } + } + + + public static void countCoins(ArrayList nominal, int sum, ArrayList coinsMin, ArrayList coinsUsed, ArrayList output) { + + int coin = sum; + //Минимальное количество монет + output.set(0, coinsMin.get(sum)); + //Подсчет использованных монет + while (coin > 0) { + if (nominal.contains(coinsUsed.get(coin))) { + int position = nominal.indexOf(coinsUsed.get(coin)) + 1; + output.set(position, output.get(position) + 1); + coin = coin - coinsUsed.get(coin); + } + } + if (coin < 0) { + output.clear(); + output.add(-1); + } + } + + + + public static void main(String[] args) { + + for (int i = 1; i <= 26; i++) { + ArrayList input = new ArrayList<>(); + ArrayList nominal = new ArrayList<>(); + ArrayList coinsMin = new ArrayList<>(); + ArrayList coinsUsed = new ArrayList<>(); + ArrayList nominalSort = new ArrayList<>(); + try { + if (InTests(String.format("%d.in", i), input)) { + int k = input.get(0); + int N = input.get(input.size() - 1); + ArrayList output = new ArrayList<>(Collections.nCopies(k + 1, 0)); + split(input, nominal, k, nominalSort); + findCoins(nominalSort, N, coinsMin, coinsUsed); + countCoins(nominal, N, coinsMin, coinsUsed, output); + outTests(String.format("out/ans%d.full-answer.out", i), output); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } +}