diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..3a63858 --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static int[] Merge(int[] a, int[] b) + { + int idxA = 0; + int idxB = 0; + int k = 0; + int[] c = new int[a.Length + b.Length]; + while (k < c.Length) + { + if (idxB == b.Length || (idxA < a.Length && a[idxA] < b[idxB])) + { + c[k] = a[idxA]; + idxA++; + } + else + { + c[k] = b[idxB]; + idxB++; + } + k++; + } + return c; + } + public static int[] Mergee(int[] v, int d, int s) + { + if (s - d == 1) + { + int[] res = new int[1]; + res[0] = v[d]; + return res; + } + + int a = (d + s) / 2; + + int[] left = Mergee(v, d, a); + int[] right = Mergee(v, a, s); + + int[] sort = Merge(left, right); + + Console.WriteLine("{0} {1} {2} {3}", d + 1, s, sort[0], sort[^1]); + + return Merge(left, right); + } + + public static void Main() + { + int n = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(v[i]); + } + + int[] sortM = Mergee(arr, 0, n); + + Console.WriteLine("{0}", string.Join(" ", sortM)); + } + } +} + + +//int[] Merge(int[] a, int[] b) + diff --git a/CourseApp/Module2/NumberOfDifferent.cs b/CourseApp/Module2/NumberOfDifferent.cs new file mode 100644 index 0000000..dc09b43 --- /dev/null +++ b/CourseApp/Module2/NumberOfDifferent.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class NumberOfDifferent + { + private static long count = 1; + + public static void Main() + { + int x = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[x]; + int i; + for (i = 0; i < x; i++) + { + arr[i] = int.Parse(v[i]); + } + Sort(arr, 0, x - 1); + + for (i = 1; i < x; i++) + { + if (arr[i - 1] != arr[i]) + { + count += 1; + } + } + Console.WriteLine("{0}", count); + } + + public static int Conditions(int[] arr, int left, int right) + { + int n = arr[left]; + int i = left - 1; + int j = right + 1; + + while (true) + { + do + { + i++; + } + while (arr[i] < n); + + do + { + j--; + } + while (arr[j] > n); + + if (i >= j) + { + return j; + } + + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + public static void Sort(int[] arr, int left, int right) + { + if (left < right) + { + int num = Number(arr, left, right); + + Sort(arr, left, num); + Sort(arr, num + 1, right); + } + } + + } +} diff --git "a/CourseApp/Module2/Numbe\320\272OfInversions.cs" "b/CourseApp/Module2/Numbe\320\272OfInversions.cs" new file mode 100644 index 0000000..df5a794 --- /dev/null +++ "b/CourseApp/Module2/Numbe\320\272OfInversions.cs" @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class NumbeкOfInversions + { + private static long count = 0; + public static void Main() + { + int x = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[x]; + for (int i = 0; i < x; i++) + { + arr[i] = int.Parse(v[i]); + } + int[] sortt = Mergee(arr, 0, x); + Console.WriteLine(count); + } + public static int[] Merge(int[] a, int[] b) + { + int idxA = 0; + int idxB = 0; + int c; + int[] k = new int[a.Length + b.Length]; + for (c = 0; c < k.Length; c++) + { + if (idxA == a.Length) + { + k[c] = b[idxB]; + idxB++; + } + else if (idxB == b.Length || a[idxA] <= b[idxB]) + { + k[c] = a[idxA]; + idxA++; + } + else + { + k[c] = b[idxB]; + idxB++; + count += a.Length - idxA; + } + } + + return k; + } + + public static int[] Mergee(int[] v, int l, int p) + { + if (p - l == 1) + { + int[] res = new int[1]; + res[0] = v[l]; + return res; + } + + int ser = (l + p) / 2; + int[] left = Mergee(v, l, ser); + int[] right = Mergee(v, ser, p); + int[] sort = Merge(left, right); + + return sort; + } + } +} + diff --git a/CourseApp/Module2/Sorting.cs b/CourseApp/Module2/Sorting.cs new file mode 100644 index 0000000..b2a3a15 --- /dev/null +++ b/CourseApp/Module2/Sorting.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class Sorting + { + public static void Main() + { + int quantity = int.Parse(Console.ReadLine()); + int[,] two = new int[quantity, 2]; + + for (int i = 0; i < quantity; i++) + { + string line = Console.ReadLine(); + string[] v = line.Split(' '); + two[i, 0] = int.Parse(v[0]); + two[i, 1] = int.Parse(v[1]); + } + for (int i = 0; i < (two.Length / 2) - 1; i++) + { + for (int j = 0; j < (two.Length / 2) - i - 1; j++) + { + if (two[j, 1] < two[j + 1, 1]) + { + (two[j, 1], two[j + 1, 1]) = (two[j + 1, 1], two[j, 1]); + (two[j, 0], two[j + 1, 0]) = (two[j + 1, 0], two[j, 0]); + } + else if (two[j, 1] == two[j + 1, 1]) + { + if (two[j, 0] > two[j + 1, 0]) + { + (two[j, 0], two[j + 1, 0]) = (two[j + 1, 0], two[j, 0]); + (two[j, 1], two[j + 1, 1]) = (two[j + 1, 1], two[j, 1]); + } + } + } + } + + for (int i = 0; i < quantity; i++) + { + Console.WriteLine("{0} {1}", two[i, 0], two[i, 1]); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/SortingProcess.cs b/CourseApp/Module2/SortingProcess.cs new file mode 100644 index 0000000..e68a5dd --- /dev/null +++ b/CourseApp/Module2/SortingProcess.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module2 +{ + public class SortingProcess + { + public static void Main() + { + int numbers = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] elements = new int[numbers]; + for (int i = 0; i < numbers; i++) + { + elements[i] = int.Parse(v[i]); + } + + bool swap = false; + for (int i = 0; i < elements.Length - 1; i++) + { + for (int j = 0; j < elements.Length - i - 1; j++) + { + if (elements[j] > elements[j + 1]) + { + (elements[j], elements[j + 1]) = (elements[j + 1], elements[j]); + string conclusion = string.Join(" ", elements); + Console.WriteLine(conclusion); + swap = true; + } + } + } + + if (swap == false) + { + Console.WriteLine(0); + } + } + } +} diff --git a/CourseApp/Module2/Warehouse.cs b/CourseApp/Module2/Warehouse.cs new file mode 100644 index 0000000..5ff33e5 --- /dev/null +++ b/CourseApp/Module2/Warehouse.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module2 +{ + public class Warehouse + { + public static void Main() + { + int p = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] products = new int[p]; + + for (int i = 0; i < p; i++) + { + products[i] = int.Parse(v[i]); + } + int n = int.Parse(Console.ReadLine()); + line = Console.ReadLine(); + v = line.Split(' '); + int[] booking = new int[n]; + for (int i = 0; i < n; i++) + { + booking[i] = int.Parse(v[i]); + } + + Sort(booking, products, p); + } + public static void Sort(int[] booking, int[] products, int k) + { + int[] c = new int[k + 1]; + for (int i = 0; i < booking.Length; i++) + { + c[booking[i]]++; + } + int x = 0; + int m = 0; + int[] new_o = new int[k]; + string[] arr = new string[k]; + for (int i = 0; i < c.Length; i++) + { + if (c[i] != 0) + { + new_o[x++] = c[i]; + + if (products[m] >= new_o[m]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + m++; + } + } + } + } +} diff --git a/CourseApp/Module3/CyclicShift.cs b/CourseApp/Module3/CyclicShift.cs new file mode 100644 index 0000000..d810c20 --- /dev/null +++ b/CourseApp/Module3/CyclicShift.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + public static void Main() + { + string S = Console.ReadLine(); + string T = Console.ReadLine(); + int result = Shift(S, T); + Console.WriteLine(result); + } + public static int Shift(string S, string T) + { + T = string.Concat(Enumerable.Repeat(T, 2)); + long a = 13; + long b = 1; + long p = 10000000 - 7; + long one = 0; + long two = 0; + long newt = 1; + if (S == T) + { + return 0; + } + foreach (char i in S.Reverse()) + { + one = (one + (i * b)) % p; + b = (b * a) % p; + } + b = 1; + for (int i = S.Length - 1; i >= 0; i--) + { + two = (two + (T[i] * b)) % p; + b = (b * a) % p; + } + for (int i = 0; i < S.Length - 1; i++) + { + newt = (newt * a) % p; + } + for (int i = 1; i < T.Length - S.Length + 1; i++) + { + if (one == two) + { + return i - 1; + } + two = a * (two - (T[i - 1] * newt)); + two += T[i + S.Length - 1]; + two = two % p; + if (((two < 0) && (p > 0)) || ((two > 0) && (p < 0))) + { + two += p; + } + } + return -1; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/CyclicString.cs b/CourseApp/Module3/CyclicString.cs new file mode 100644 index 0000000..55eb658 --- /dev/null +++ b/CourseApp/Module3/CyclicString.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module3 +{ + public class CyclicString + { + public static void Main() + { + string n = Console.ReadLine(); + int[] s = String(n); + int conclusion = n.Length - s[n.Length - 1]; + Console.WriteLine(conclusion); + } + public static int[] String(string s) + { + int[] a = new int[s.Length]; + a[0] = 0; + int i; + int j; + for (i = 0; i < s.Length - 1; i++) + { + j = a[i]; + + while ((j > 0) && (s[i + 1] != s[j])) + { + j = a[j - 1]; + } + + if (s[i + 1] == s[j]) + { + a[i + 1] = j + 1; + } + else + { + a[i + 1] = 0; + } + } + + return a; + } + } +} + diff --git a/CourseApp/Module3/LinePeriod.cs b/CourseApp/Module3/LinePeriod.cs new file mode 100644 index 0000000..a435c8b --- /dev/null +++ b/CourseApp/Module3/LinePeriod.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module3 +{ + public class LinePeriod + { + public static void Main() + { + string n = Console.ReadLine(); + int[] s = Period(n); + int conclusion = n.Length - s[n.Length - 1]; + if (n.Length % conclusion == 0) + { + Console.WriteLine(n.Length / conclusion); + } + else + { + Console.WriteLine(1); + } + } + public static int[] Period(string s) + { + int[] a = new int[s.Length]; + a[0] = 0; + int i; + int j; + for (i = 0; i < s.Length - 1; i++) + { + j = a[i]; + + while ((j > 0) && (s[i + 1] != s[j])) + { + j = a[j - 1]; + } + + if (s[i + 1] == s[j]) + { + a[i + 1] = j + 1; + } + else + { + a[i + 1] = 0; + } + } + + return a; + } + } +} + diff --git a/CourseApp/Module3/SubstringSearch.cs b/CourseApp/Module3/SubstringSearch.cs new file mode 100644 index 0000000..1f3416e --- /dev/null +++ b/CourseApp/Module3/SubstringSearch.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module3 +{ + public class SubstringSearch + { + public static void Main() + { + string one = Console.ReadLine(); + string two = Console.ReadLine(); + int a = 1000000000 + 7; + int n = 26; + SSearch(one, two, a, n); + } + public static long Search(string a, int c, int s, int n) + { + long r = 0; + for (int i = 0; i < c; i++) + { + r = ((r * n) + a[i]) % s; + } + return r; + } + + public static void SSearch(string a, string c, int s, int n) + { + long newa = Search(a, c.Length, s, n); + long newc = Search(c, c.Length, s, n); + long o = 1; + for (int i = 0; i < c.Length; i++) + { + o = (o * n) % s; + } + for (int i = 0; i <= a.Length - c.Length; i++) + { + if (newc == newa) + { + Console.Write("{0} ", i); + } + if (i + c.Length < a.Length) + { + newa = ((newa * n) - (a[i] * o) + a[i + c.Length]) % s; + newa = (newa + s) % s; + } + // if (newc == newa) + // { + // Console.Write("{0} ", i); + // } + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/GetPSP.cs b/CourseApp/Module4/GetPSP.cs new file mode 100644 index 0000000..138d543 --- /dev/null +++ b/CourseApp/Module4/GetPSP.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace GetPSP +{ + public static class Module4 + { + public static void Main() + { + string p = Console.ReadLine(); + object result = PSP(p); + Console.Write(result); + } + public static int PSP(string p) + { + var t = 0; + var arr = new List(); + foreach (var i in p) + { + if (i == '(') + { + arr.Add(arr); + } + //if (arr != new List() && arr[-1] == "(") + //{ + //arr.Remove(-1); + //} + //else + //{ + //t += 1; + //} + else if ((i == ')') && (arr.Count > 0)) + { + arr.Remove(arr); + //if (arr.Count > 0) + //{ + //arr.Remove(arr); + //} + //else + // { + // t += 1; + //} + } + else if ((i == ')') && (arr.Count <= 0)) + { + t += 1; + } + } + return t + arr.Count; + } + } +} diff --git a/CourseApp/Module4/MinimumSegments.cs b/CourseApp/Module4/MinimumSegments.cs new file mode 100644 index 0000000..6325b72 --- /dev/null +++ b/CourseApp/Module4/MinimumSegments.cs @@ -0,0 +1,129 @@ +using System; + +public class MinimumSegments +{ + public static void Main() + { + string nF = Console.ReadLine(); + string[] First = nF.Split(' '); + int[] massF = new int[2]; + for (int i = 0; i < 2; i++) + { + massF[i] = int.Parse(First[i]); + } + string nS = Console.ReadLine(); + string[] Second = nS.Split(' '); + int[] massS = new int[massF[0]]; + for (int i = 0; i < massF[0]; i++) + { + massS[i] = int.Parse(Second[i]); + } + for (int i = 0; i < massF[1]; i++) + { + while (Segments.Empty() == false && massS[i] < massS[Segments.Begin()]) + { + Segments.Pop_Front(); + } + + Segments.Push_First(i); + } + for (int i = massF[1]; i < massF[0]; i++) + { + Console.WriteLine(massS[Segments.Ago()]); + + while (Segments.Empty() == false && Segments.Ago() <= i - massF[1]) + { + Segments.Pop_Ago(); + } + while (Segments.Empty() == false && massS[i] < massS[Segments.Begin()]) + { + Segments.Pop_Front(); + if (Segments.Size() == 0) + { + Segments.Clear(); + } + } + Segments.Push_First(i); + } + + Console.WriteLine(massS[Segments.Ago()]); + } + private class Segments + { + private static int[] buffer = new int[100000001]; + private static int a = 0; + private static int b = 100000001 - 1; + private static int c = 0; + public static void Push_Ago(int t) + { + b++; + if (b == 100000001) + { + b = 0; + } + + buffer[b] = t; + c++; + } + public static void Push_First(int o) + { + a--; + if (a < 0) + { + a = 100000001 - 1; + } + + buffer[a] = o; + c++; + } + + public static void Pop_Ago() + { + b--; + if (b < 0) + { + b = 100000001 - 1; + } + + c--; + } + + public static void Pop_Front() + { + a++; + if (a == 100000001) + { + a = 0; + } + + c--; + } + + public static void Clear() + { + a = 0; + b = 100000001 - 1; + c = 0; + } + + public static int Begin() + { + return buffer[a]; + } + + public static int Ago() + { + return buffer[b]; + } + + public static int Size() + { + return c; + } + + public static bool Empty() + { + return c == 0; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/NearestSmaller.cs b/CourseApp/Module4/NearestSmaller.cs new file mode 100644 index 0000000..810417f --- /dev/null +++ b/CourseApp/Module4/NearestSmaller.cs @@ -0,0 +1,79 @@ +using System; + +public class NearestSmaller +{ + public static void Main() + { + var n = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var v = s.Split(' '); + var mass = new int[n]; + var conclusion = new int[n]; + for (int i = 0; i < n; i++) + { + mass[i] = int.Parse(v[i]); + } + int index = n - 1; + while (index >= 0) + { + while ((Smaller.Empty() == false) && (mass[Smaller.Back()] >= mass[index])) + { + Smaller.Pop(); + } + + if (Smaller.Empty() == true) + { + conclusion[index] = -1; + } + else + { + conclusion[index] = Smaller.Back(); + } + + Smaller.Push(index); + + index--; + } + for (int i = 0; i < n; i++) + { + Console.Write(conclusion[i] + " "); + } + } + + private class Smaller + { + private static int[] buffer = new int[1000000001]; + private static int top = -1; + + public static void Push(int x) + { + top++; + buffer[top] = x; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buffer[top]; + } + } +} diff --git a/CourseApp/Module4/Sorting.cs b/CourseApp/Module4/Sorting.cs new file mode 100644 index 0000000..51bda38 --- /dev/null +++ b/CourseApp/Module4/Sorting.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +public class Module4 +{ + public static void Main() + { + int n = int.Parse(Console.ReadLine()); + string[] AB = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + int[] result = new int[AB.Length]; + int i = 0; + int j = 0; + + while (i != AB.Length) + { + if (Stack.Empty() == true || (j < AB.Length && int.Parse(AB[j]) < Stack.Show())) + { + Stack.Click(int.Parse(AB[j])); + j++; + } + else + { + result[i] += Stack.Show(); + Stack.Delete(); + i++; + } + } + Console.WriteLine(Vg(result)); + } + public static string Vg(int[] result) + { + for (int i = 0; i < result.Length - 1; i++) + { + if (result[i] > result[i + 1]) + { + return "NO"; + } + } + + return "YES"; + } + private static class Stack + { + private static int[] buffer = new int[100000001]; + private static int top = -1; + + public static void Click(int x) + { + top++; + buffer[top] = x; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Delete() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static void Clear() + { + top = -1; + } + + public static int Show() + { + return buffer[top]; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/1.cs b/CourseApp/Module5/1.cs new file mode 100644 index 0000000..af766ca --- /dev/null +++ b/CourseApp/Module5/1.cs @@ -0,0 +1,121 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Module5 +{ + public class Branches + { + public static void Main() + { + int[] order = Console.ReadLine().Trim().Split(' ').Select(a => Convert.ToInt32(a)).ToArray(); + BinaryTree bi_tree = new BinaryTree(); + foreach (var item in order) + { + if (item != 0) + { + bi_tree.Insert(item); + } + } + List conclusion = bi_tree.Elements(); + foreach (var item in conclusion) + { + Console.WriteLine(item); + } + Console.WriteLine(); + } + } + public class BinaryTree + { + private Node score = null; + private int size = 0; + + public bool One(int n) + { + return HereFind(n, score); + } + + public List Elements() + { + List elements = new List(); + HereElements(score, elements); + return elements; + } + + public void Insert(int n) + { + score = HereInsert(n, score); + } + + private bool HereFind(int n, Node real) + { + if (real == null) + { + return false; + } + else if (real.Data == n) + { + return true; + } + else if (real.Data > n) + { + return HereFind(n, real.Left); + } + else + { + return HereFind(n, real.Right); + } + } + + private Node HereInsert(int n, Node real) + { + if (real == null) + { + size++; + return new Node(n); + } + else if (real.Data > n) + { + real.Left = HereInsert(n, real.Left); + } + else if (real.Data < n) + { + real.Right = HereInsert(n, real.Right); + } + + return real; + } + + private void HereElements(Node real, List elements) + { + if (real == null) + { + return; + } + + HereElements(real.Left, elements); + if (((real.Left == null) && (real.Right != null)) || ((real.Right == null) && (real.Left != null))) + { + elements.Add(real.Data); + } + + HereElements(real.Right, elements); + } + } + public class Node + { + public Node(int input) + { + Data = input; + Left = null; + Right = null; + + } + public int Data { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + + } +} + diff --git a/CourseApp/Module5/2.cs b/CourseApp/Module5/2.cs new file mode 100644 index 0000000..95ade4b --- /dev/null +++ b/CourseApp/Module5/2.cs @@ -0,0 +1,131 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Module5 +{ + public class Balance + { + public static void Main() + { + int[] orber = Console.ReadLine().Trim().Split(' ').Select(a => Convert.ToInt32(a)).ToArray(); + BinaryTree bi_tree = new BinaryTree(); + foreach (var item in orber) + { + if (item != 0) + { + bi_tree.Insert(item); + } + } + Console.WriteLine(bi_tree.IsBalanced() ? "YES" : "NO"); + } + } + public class BinaryTree + { + private Node score = null; + + private int size = 0; + + private bool cause; + + public void Insert(int n) + { + score = HereInsert(n, score, null, 0); + } + + public bool IsBalanced() + { + cause = true; + HereCheck(score); + return cause; + } + + private void HereCheck(Node real) + { + if (real == null || !cause) + { + return; + } + + HereCheck(real.Left); + if (Math.Abs(real.Top_Left - real.Top_Right) > 1) + { + cause = false; + } + + HereCheck(real.Right); + } + + private Node HereInsert(int a, Node real, Node previous, int k) + { + if (real == null) + { + size++; + + Node temp = new Node(a); + temp.Previous = previous; + temp.Top_Left = 0; + temp.Top_Right = 0; + Node buffer = temp; + int i = k - 1; + while (buffer != null) + { + if (buffer.Previous != null) + { + if (buffer.Data < buffer.Previous.Data) + { + if (buffer.Previous.Top_Left < k - i) + { + buffer.Previous.Top_Left = k - i; + } + } + else + { + if (buffer.Previous.Top_Right < k - i) + { + buffer.Previous.Top_Right = k - i; + } + } + } + + buffer = buffer.Previous; + i--; + } + + return temp; + } + else if (real.Data > a) + { + real.Left = HereInsert(a, real.Left, real, k + 1); + } + else if (real.Data < a) + { + real.Right = HereInsert(a, real.Right, real, k + 1); + } + + return real; + } + } + public class Node + { + public Node(int input) + { + Data = input; + Left = null; + Right = null; + } + + public int Data { get; set; } + + public Node Previous { get; set; } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Top_Left { get; set; } + + public int Top_Right { get; set; } + } +} diff --git a/CourseApp/Module5/3.cs b/CourseApp/Module5/3.cs new file mode 100644 index 0000000..5782196 --- /dev/null +++ b/CourseApp/Module5/3.cs @@ -0,0 +1,93 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Module5 +{ + public class SegmentGCD + { + public static void Main() + { + int size = int.Parse(Console.ReadLine()); + int[] orber = Console.ReadLine().Trim().Split(' ').Select(k => Convert.ToInt32(k)).ToArray(); + int do_count = int.Parse(Console.ReadLine()); + Segment tree = new Segment(size); + tree.Build(orber); + List res = new List(); + for (int i = 0; i < do_count; i++) + { + int[] b = Console.ReadLine().Trim().Split(' ').Select(k => Convert.ToInt32(k)).ToArray(); + res.Add(tree.GetGCD(b[0], b[1])); + } + + Console.WriteLine(string.Join(" ", res)); + } + } + public class Segment + { + private int[] segments; + + private int size; + + public Segment(int input) + { + segments = new int[4 * input]; + size = input; + } + + public int GCD(int a, int b) + { + while (b != 0) + { + int temp = b; + b = a % b; + a = temp; + } + + return a; + } + + public void Build(int[] orber) + { + HereBuild(0, 0, size, orber); + } + + public int GetGCD(int do_left, int query_right) + { + return HereGCD(0, 0, size, segments, do_left - 1, query_right); + } + + private void HereBuild(int index, int left, int right, int[] orber) + { + if (right - left == 1) + { + segments[index] = orber[left]; + return; + } + + int half = (left + right) / 2; + HereBuild((2 * index) + 1, left, half, orber); + HereBuild((2 * index) + 2, half, right, orber); + segments[index] = GCD(segments[(2 * index) + 1], segments[(2 * index) + 2]); + } + + private int HereGCD(int index, int left, int right, int[] segment, int do_left, int do_right) + { + if (do_left <= left && do_right >= right) + { + return segment[index]; + } + + if (do_left >= right || do_right <= left) + { + return 0; + } + + int h = (left + right) / 2; + int concl_left = HereGCD((2 * index) + 1, left, h, segment, do_left, do_right); + int concl_right = HereGCD((2 * index) + 2, h, right, segment, do_left, do_right); + return GCD(concl_left, concl_right); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/4.cs b/CourseApp/Module5/4.cs new file mode 100644 index 0000000..225def0 --- /dev/null +++ b/CourseApp/Module5/4.cs @@ -0,0 +1,146 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Module5 +{ + public class IndexOfNulls + { + public static void Main() + { + int size = int.Parse(Console.ReadLine()); + int[] orber = Console.ReadLine().Trim().Split(' ').Select(a => Convert.ToInt32(a)).ToArray(); + int query_count = int.Parse(Console.ReadLine()); + Segment tree = new Segment(size); + tree.Build(orber); + List res = new List(); + for (int i = 0; i < query_count; i++) + { + int[] comands = Console.ReadLine().Trim().Split(' ').Select(a => Convert.ToInt32(a)).ToArray(); + res.Add(tree.GetIndex(comands[0], comands[1], comands[2])); + } + + Console.WriteLine(string.Join(" ", res)); + } + } + public class Segment + { + private List[] segments; + + private int size; + + public Segment(int input) + { + segments = new List[4 * input]; + size = input; + } + + public List Check(List x, List y) + { + List temp; + if (x.Count() == 0 && y.Count() != 0) + { + temp = new List(y); + return temp; + } + else if (x.Count() != 0 && y.Count() != 0) + { + temp = new List(x); + temp.AddRange(y); + return temp; + } + + temp = new List(x); + return temp; + } + + public void Build(int[] orber) + { + HereBuild(0, 0, size, orber); + } + + public int GetIndex(int do_left, int do_right, int find) + { + bool trigger = true; + List buf = HereGetIndex(0, 0, size, segments, do_left - 1, do_right, ref find, ref trigger); + if (buf.Count() < find) + { + return -1; + } + else + { + return buf.ElementAt(find - 1); + } + } + + private void HereBuild(int index, int left, int right, int[] numbers) + { + if (right - left == 1) + { + segments[index] = new List(); + if (numbers[left] == 0) + { + segments[index].Add(left + 1); + } + + return; + } + + int half = (left + right) / 2; + HereBuild((2 * index) + 1, left, half, numbers); + HereBuild((2 * index) + 2, half, right, numbers); + segments[index] = Check(segments[(2 * index) + 1], segments[(2 * index) + 2]); + } + + private List HereGetIndex(int index, int left, int right, List[] segment, int do_left, int do_right, ref int find, ref bool q) + { + if (!q) + { + return new List(); + } + + if (do_left <= left && do_right >= right) + { + return segment[index]; + } + + if (do_left >= right || do_right <= left) + { + return new List(); + } + + int h = (left + right) / 2; + List conclusion_left = HereGetIndex((2 * index) + 1, left, h, segment, do_left, do_right, ref find, ref q); + if (conclusion_left.Count() != 0) + { + if (conclusion_left.Count() < find) + { + find -= conclusion_left.Count(); + } + else + { + q = false; + return conclusion_left; + } + } + + List conclusion_right = HereGetIndex((2 * index) + 2, h, right, segment, do_left, do_right, ref find, ref q); + if (conclusion_right.Count() != 0) + { + if (conclusion_right.Count() < find) + { + find -= conclusion_right.Count(); + } + else + { + q = false; + return conclusion_right; + } + } + + return new List(); + } + } + +} \ No newline at end of file diff --git a/CourseApp/Module5/5.cs b/CourseApp/Module5/5.cs new file mode 100644 index 0000000..6ffc148 --- /dev/null +++ b/CourseApp/Module5/5.cs @@ -0,0 +1,126 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Module5 +{ + public class SegmentGCD + { + public static void Main() + { + int size = int.Parse(Console.ReadLine()); + int[] orber = Console.ReadLine().Trim().Split(' ').Select(a => Convert.ToInt32(a)).ToArray(); + int do_count = int.Parse(Console.ReadLine()); + DynamicSegment dy_tree = new DynamicSegment(size); + dy_tree.Build(orber); + List res = new List(); + for (int i = 0; i < do_count; i++) + { + string[] borders = Console.ReadLine().Trim().Split(' ').ToArray(); + if (borders[0] == "u") + { + dy_tree.Update(int.Parse(borders[1]), int.Parse(borders[2])); + } + else + { + res.Add(dy_tree.GetGCD(int.Parse(borders[1]), int.Parse(borders[2]))); + } + } + + Console.WriteLine(string.Join(" ", res)); + } + } + public class DynamicSegment + { + private int[] segments; + + private int size; + + public DynamicSegment(int input) + { + segments = new int[4 * input]; + size = input; + } + + public int GCD(int x, int y) + { + while (y != 0) + { + int temp = y; + y = x % y; + x = temp; + } + + return x; + } + + public void Build(int[] orber) + { + HereBuild(0, 0, size, orber); + } + + public void Update(int index, int value) + { + HereUpdate(0, 0, size, segments, index - 1, value); + } + + public int GetGCD(int do_left, int do_right) + { + return HereGetGCD(0, 0, size, segments, do_left - 1, do_right); + } + + private void HereBuild(int index, int left, int right, int[] orber) + { + if (right - left == 1) + { + segments[index] = orber[left]; + return; + } + + int h = (left + right) / 2; + HereBuild((2 * index) + 1, left, h, orber); + HereBuild((2 * index) + 2, h, right, orber); + segments[index] = GCD(segments[(2 * index) + 1], segments[(2 * index) + 2]); + } + + private void HereUpdate(int index, int left, int right, int[] segment, int vd_index, int value) + { + if (right - left == 1) + { + segments[index] = value; + return; + } + + int h = (left + right) / 2; + if (vd_index < h) + { + HereUpdate((2 * index) + 1, left, h, segment, vd_index, value); + } + else + { + HereUpdate((2 * index) + 2, h, right, segment, vd_index, value); + } + + segments[index] = GCD(segments[(2 * index) + 1], segments[(2 * index) + 2]); + } + + private int HereGetGCD(int index, int left, int right, int[] segment, int do_left, int do_right) + { + if (do_left <= left && do_right >= right) + { + return segment[index]; + } + + if (do_left >= right || do_right <= left) + { + return 0; + } + + int h = (left + right) / 2; + int conclusion_left = HereGetGCD((2 * index) + 1, left, h, segment, do_left, do_right); + int conclusion_right = HereGetGCD((2 * index) + 2, h, right, segment, do_left, do_right); + return GCD(conclusion_left, conclusion_right); + } + } +} diff --git a/MergeSort.cs b/MergeSort.cs new file mode 100644 index 0000000..7eb0a34 --- /dev/null +++ b/MergeSort.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static int[] Merge(int[] a, int[] b) + { + int idxA = 0; + int idxB = 0; + int k = 0; + int[] c = new int[a.Length + b.Length]; + while (k < c.Length) + { + if (idxB == b.Length || (idxA < a.Length && a[idxA] < b[idxB])) + { + c[k] = a[idxA]; + idxA++; + } + else + { + c[k] = b[idxB]; + idxB++; + } + k++; + } + return c; + } + public static int[] Mergee(int[] v, int d, int s) + { + if (s - d == 1) + { + int[] res = new int[1]; + res[0] = v[d]; + return res; + } + + int a = (d + s) / 2; + + int[] left = Mergee(v, d, a); + int[] right = Mergee(v, a, s); + + int[] sort = Merge(left, right); + + Console.WriteLine("{0} {1} {2} {3}", d + 1, s, sort[0], sort[^1]); + + return Merge(left, right); + } + + public static void Main() + { + int n = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(v[i]); + } + + int[] sortM = Mergee(arr, 0, n); + + Console.WriteLine("{0}", string.Join(" ", sortM)); + } + } +} + + +//int[] Merge(int[] a, int[] b) + diff --git a/NumberOfDifferent.cs b/NumberOfDifferent.cs new file mode 100644 index 0000000..1891689 --- /dev/null +++ b/NumberOfDifferent.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class NumberOfDifferent + { + private static long count = 1; + + public static void Main() + { + int x = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[n]; + int i; + for (i = 0; i < x; i++) + { + arr[i] = int.Parse(v[i]); + } + Sort(arr, 0, x - 1); + + for (i = 1; i < x; i++) + { + if (arr[i - 1] != arr[i]) + { + count += 1; + } + } + Console.WriteLine("{0}", count); + } + + public static int Conditions(int[] arr, int left, int right) + { + int n = arr[left]; + int i = left - 1; + int j = right + 1; + + while (true) + { + do + { + i++; + } + while (arr[i] < n); + + do + { + j--; + } + while (arr[j] > n); + + if (i >= j) + { + return j; + } + + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + public static void Sort(int[] arr, int left, int right) + { + if (left < right) + { + int num = Number(arr, left, right); + + Sort(arr, left, num); + Sort(arr, num + 1, right); + } + } + + } +} diff --git "a/Numbe\320\272OfInversions.cs" "b/Numbe\320\272OfInversions.cs" new file mode 100644 index 0000000..66e4530 --- /dev/null +++ "b/Numbe\320\272OfInversions.cs" @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class NumbeкOfInversions + { + private static long count = 0; + public static void Main() + { + int x = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] arr = new int[x]; + for (int i = 0; i < x; i++) + { + arr[i] = int.Parse(v[i]); + } + int[] sortt = Mergee(arr, 0, x); + Console.WriteLine(count); + } + public static int[] Merge(int[] a, int[] b) + { + int idxA = 0; + int idxB = 0; + int c; + int[] k = new int[a.Length + b.Length]; + for (c = 0; c < k.Length; c++) + { + if (idxA == a.Length) + { + k[c] = b[idxB]; + idxB++; + } + else if (idxB == b.Length || a[idxA] <= b[idxB]) + { + k[c] = a[idxA]; + idxA++; + } + else + { + k[c] = b[idxB]; + idxB++; + count += a.Length - idxA; + } + } + + return k; + } + + public static int[] Mergee(int[] v, int l, int p) + { + if (p - l == 1) + { + int[] res = new int[1]; + res[0] = v[l]; + return res; + } + + int ser = (l + p) / 2; + int[] left = Mergee(v, l, ser); + int[] right = Mergee(v, ser, p); + int[] sort = Merge(left, right); + + return sort; + } + } +} + diff --git a/README.md b/README.md index e58af8a..d8c4a5d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Tprogramming_42_2020 -Master branch :) \ No newline at end of file +Zavidina Ekaterina diff --git a/Sorting.cs b/Sorting.cs new file mode 100644 index 0000000..79b5166 --- /dev/null +++ b/Sorting.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class Sorting + { + public static void Main() + { + int quantity = int.Parse(Console.ReadLine()); + int[,] two = new int[quantity, 2]; + + for (int i = 0; i < quantity; i++) + { + string line = Console.ReadLine(); + string[] v = line.Split(' '); + two[i, 0] = int.Parse(v[0]); + two[i, 1] = int.Parse(v[1]); + } + for (int i = 0; i < (two.Length / 2) - 1; i++) + { + for (int j = 0; j < (two.Length / 2) - i - 1; j++) + { + if (two[j, 1] < two[j + 1, 1]) + { + (two[j, 1], two[j + 1, 1]) = (two[j + 1, 1], two[j, 1]); + (two[j, 0], two[j + 1, 0]) = (two[j + 1, 0], two[j, 0]); + } + else if (two[j, 1] == two[j + 1, 1]) + { + if (two[j, 0] > two[j + 1, 0]) + { + (two[j, 0], two[j + 1, 0]) = (two[j + 1, 0], two[j, 0]); + (two[j, 1], two[j + 1, 1]) = (two[j + 1, 1], two[j, 1]); + } + } + } + } + + for (int i = 0; i < quantity; i++) + { + Console.WriteLine("{0} {1}", two[i, 0], two[i, 1]); + } + } + } +} \ No newline at end of file diff --git a/SortingProcess.cs b/SortingProcess.cs new file mode 100644 index 0000000..54fa08c --- /dev/null +++ b/SortingProcess.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module2 +{ + public class SortingProcess + { + public static void Main() + { + int numbers = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] elements = new int[numbers]; + for (int i = 0; i < numbers; i++) + { + elements[i] = int.Parse(v[i]); + } + + bool swap = false; + for (int i = 0; i < elements.Length - 1; i++) + { + for (int j = 0; j < elements.Length - i - 1; j++) + { + if (elements[j] > elements[j + 1]) + { + (elements[j], elements[j + 1]) = (elements[j + 1], elements[j]); + string conclusion = string.Join(" ", elements); + Console.WriteLine(conclusion); + swap = true; + } + } + } + + if (swap == false) + { + Console.WriteLine(0); + } + } + } +} diff --git a/Warehouse.cs b/Warehouse.cs new file mode 100644 index 0000000..df1c076 --- /dev/null +++ b/Warehouse.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module2 +{ + public class Warehouse + { + public static void Main() + { + int p = int.Parse(Console.ReadLine()); + string line = Console.ReadLine(); + string[] v = line.Split(' '); + int[] products = new int[p]; + + for (int i = 0; i < p; i++) + { + products[i] = int.Parse(v[i]); + } + int n = int.Parse(Console.ReadLine()); + line = Console.ReadLine(); + v = line.Split(' '); + int[] booking = new int[n]; + for (int i = 0; i < n; i++) + { + booking[i] = int.Parse(v[i]); + } + + Sort(booking, products, p); + } + public static void Sort(int[] booking, int[] products, int k) + { + int[] c = new int[k + 1]; + for (int i = 0; i < booking.Length; i++) + { + c[booking[i]]++; + } + int x = 0; + int m = 0; + int[] new_o = new int[k]; + string[] arr = new string[k]; + for (int i = 0; i < c.Length; i++) + { + if (c[i] != 0) + { + new_o[x++] = c[i]; + + if (products[m] >= new_o[m]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + m++; + } + } + } + } +}