Skip to content

Сидорова П.А. 8306 #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions binary-search-for-the-answer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication2
{
internal class Program
{
private static List<int> arr = new List<int>();
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();
}
}
}
48 changes: 48 additions & 0 deletions binary-search/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
85 changes: 85 additions & 0 deletions priority-queue/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication3
{
internal class Program
{
private static List<int> heap = new List<int>();
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();
}
}
}