diff --git a/binary-search-for-the-answer/Program.cs b/binary-search-for-the-answer/Program.cs new file mode 100644 index 0000000..1a1ab7d --- /dev/null +++ b/binary-search-for-the-answer/Program.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace ConsoleApplication2 +{ + internal class Program + { + private static List arr = new List(); + private static int N, k; + + private static bool Check(int x) + { + int _k = 1; + int last_cow = arr[0]; + + foreach (var c in arr) + { + if (c - last_cow >= x) + { + _k++; + last_cow = c; + } + } + + return _k >= k; + } + + public static void Main(string[] args) + { + StreamReader reader = new StreamReader("C:/Users/polin/RiderProjects/ConsoleApplication2/ConsoleApplication2/5.in"); + StreamWriter writer = new StreamWriter("C:/Users/polin/RiderProjects/ConsoleApplication2/ConsoleApplication2/test.out"); + + N = int.Parse(reader.ReadLine()); + k = int.Parse(reader.ReadLine()) + 1; + + while (!reader.EndOfStream) + arr.Add(int.Parse(reader.ReadLine())); + + int left = 0; + int right = arr[N-1] - arr[0] + 1; + + while (right - left > 1) + { + int middle = (left + right) / 2; + if (Check(middle)) left = middle; + else right = middle; + } + + writer.WriteLine(left); + + reader.Close(); + writer.Close(); + } + } +} \ No newline at end of file diff --git a/binary-search/Program.cs b/binary-search/Program.cs new file mode 100644 index 0000000..75239e7 --- /dev/null +++ b/binary-search/Program.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using System.Linq; + +namespace ConsoleApplication1 +{ + internal class Program + { + private static int[] arr; + + public static void Main(string[] args) + { + StreamReader reader = new StreamReader("C:/Users/polin/RiderProjects/ConsoleApplication1/5.in"); + StreamWriter writer = new StreamWriter("C:/Users/polin/RiderProjects/ConsoleApplication1/test.out"); + + int N = int.Parse(reader.ReadLine()); + arr = reader.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); + int K = int.Parse(reader.ReadLine()); + + int left = 0; + int right = N - 1; + + while (!reader.EndOfStream) + writer.WriteLine(Search(left, right, int.Parse(reader.ReadLine()))); + //Console.WriteLine(Search(left, right, int.Parse(reader.ReadLine()))); + + reader.Close(); + writer.Close(); + } + + private static int Search(int left, int right, int searchElem) + { + int middle = (left + right) / 2; + if (searchElem == arr[middle]) return middle; + + if (Math.Abs(right - left) <= 1) + { + if (arr[right] == searchElem) return right; + if (arr[left] == searchElem) return left; + else return -1; + } + + if (searchElem > arr[middle]) return Search(middle, right, searchElem); + if (searchElem < arr[middle]) return Search(left, middle, searchElem); + else return -1; + } + } +} \ No newline at end of file diff --git a/priority-queue/Program.cs b/priority-queue/Program.cs new file mode 100644 index 0000000..8339421 --- /dev/null +++ b/priority-queue/Program.cs @@ -0,0 +1,85 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace ConsoleApplication3 +{ + internal class Program + { + private static List heap = new List(); + private static int N; + private static StreamWriter writer; + + public static void Swap(int index1, int index2) + { + int temp = heap[index1]; + heap[index1] = heap[index2]; + heap[index2] = temp; + } + + public static void SiftDown(int i) + { + while (2 * i + 1 < heap.Count) + { + int left = 2 * i + 1; + int right = 2 * i + 2; + int j = left; + + if ((right < heap.Count) && (heap[right] > heap[left])) + j = right; + + if (heap[i] >= heap[j]) + break; + + Swap(i, j); + i = j; + } + } + + public static void SiftUp(int i) + { + while (heap[i] > heap[(i - 1) / 2]) + { + Swap(i, (i - 1) / 2); + i = (i - 1) / 2; + } + } + + public static void Insert(int key) + { + heap.Add(key); + SiftUp(heap.Count - 1); + } + + public static void ExtractMax() + { + writer.WriteLine(heap[0]); + + heap[0] = heap[heap.Count - 1]; + heap.RemoveAt(heap.Count - 1); + SiftDown(0); + } + + public static void Main(string[] args) + { + StreamReader reader = new StreamReader("C:/Users/polin/RiderProjects/ConsoleApplication3/ConsoleApplication3/3.in"); + writer = new StreamWriter("C:/Users/polin/RiderProjects/ConsoleApplication3/ConsoleApplication3/result.out"); + + N = int.Parse(reader.ReadLine()); + + while (!reader.EndOfStream) + { + string elem = reader.ReadLine(); + + if (elem=="GET") + ExtractMax(); + else + Insert(int.Parse(elem)); + } + + reader.Close(); + writer.Close(); + } + } +} \ No newline at end of file