commit 810a9654a47176d2b6fd029b596a7fb841463e79 Author: Arthur K. Date: Thu May 29 19:24:43 2025 +0300 upload diff --git a/1/A.cpp b/1/A.cpp new file mode 100644 index 0000000..0bed2e3 --- /dev/null +++ b/1/A.cpp @@ -0,0 +1,11 @@ +#include + +int main( void ) { + long x; + long y; + + std::cin >> x >> y; + std::cout << x + y; + + return 0; +} \ No newline at end of file diff --git a/1/B.cpp b/1/B.cpp new file mode 100644 index 0000000..b0add77 --- /dev/null +++ b/1/B.cpp @@ -0,0 +1,37 @@ +#include +#include + +int main() { + int length; + int offset; + std::cin >> length >> offset; + + if (offset > 0) + offset %= length; + else if (offset < 0) + offset = -((-offset) % length); + + int* numbers = new int[length]; + + for (int i = 0; i < length; i++) { + std::cin >> numbers[i]; + } + + if (offset < 0) { + for (int a = 0; a > offset; --a) { + for (int j = 1; j < length; ++j) { + std::swap(numbers[j], numbers[j - 1]); + } + } + } else { + for (int a = 0; a < offset; ++a) { + for (int j = length - 1; j > 0; --j) { + std::swap(numbers[j], numbers[j - 1]); + } + } + } + for (int i = 0; i < length; i++) { + std::cout << numbers[i] << " "; + } +} + diff --git a/1/C.cpp b/1/C.cpp new file mode 100644 index 0000000..194c14b --- /dev/null +++ b/1/C.cpp @@ -0,0 +1,15 @@ +#include +#include + +int get_mcd(int a, int b) { + if (b == 0) + return a; + return get_mcd(b, a % b); +} + +int main() { + int a, b; + std::cin >> a >> b; + std::cout << get_mcd(a, b); + return 0; +} \ No newline at end of file diff --git a/1/D.cpp b/1/D.cpp new file mode 100644 index 0000000..527de73 --- /dev/null +++ b/1/D.cpp @@ -0,0 +1,33 @@ +#include + + +int main() { + int n, m, q, counter = 0; + std::cin >> n >> m >> q; + bool** episodes = new bool* [n]; + for (int i = 0; i < n; ++i) { + episodes[i] = new bool[m]; + } + + for (int i = 0; i < q; ++i) { + int e, s; + std::cin >> e >> s; + + if (!episodes[s - 1][e - 1]) { + episodes[s - 1][e - 1] = true; + ++counter; + } + } + + std::cout << n*m - counter << "\n"; + + for (int s = 0; s < n; ++s) { + for (int e = 0; e < m; ++e) { + if (!episodes[s][e]) { + std::cout << e + 1 << " " << s + 1 << "\n"; + } + } + } + delete[] episodes; + return 0; +} \ No newline at end of file diff --git a/1/E.cpp b/1/E.cpp new file mode 100644 index 0000000..6136a50 --- /dev/null +++ b/1/E.cpp @@ -0,0 +1,36 @@ +#include + +int main() { + int size, offset_l = 0, offset_r = 0, counter = 0; + + std::cin >> size; + + char *word = new char[size]; + for (int i = 0; i < size; ++i) + std::cin >> word[i]; + + if (size == 3) { + if (word[0] == word[1] || word[1] == word[2] || word[0] == word[2]) + std::cout << "YES"; + else + std::cout << "NO"; + return 0; + } + + for (int i = 0; i < (size / 2) + (size % 2) - counter; ++i) { + if (word[i + offset_l] != word[size - i - offset_r - 1]) { + ++counter; + if (word[i + offset_l + 1] == word[size - i - offset_r - 1]) + ++offset_l; + else + ++offset_r; + } + } + delete[] word; + + if (counter < 2) + std::cout << "YES"; + else + std::cout << "NO"; + return 0; +} diff --git a/1/F.cpp b/1/F.cpp new file mode 100644 index 0000000..4b7caab --- /dev/null +++ b/1/F.cpp @@ -0,0 +1,34 @@ +#include + +int main() { + int n; + std::cin >> n; + + int *days = new int[n]; + int *sums_lr = new int[n]; + int *sums_rl = new int[n]; + + for (int i = 0; i < n; ++i) { + std::cin >> days[i]; + sums_lr[i] = 0; + sums_rl[i] = 0; + } + + for (int i = 1; i < n; ++i) { + sums_lr[i] = sums_lr[i-1] + days[i-1]; + } + + for (int i = n-2; i > -1; --i) { + sums_rl[i] = sums_rl[i+1] + days[i+1]; + } + + for (int i = 0; i < n; ++i) { + if (sums_rl[i] == sums_lr[i]) { + std::cout << i; + return 0; + } + } + + std::cout << -1; + return 0; +} \ No newline at end of file diff --git a/1/H.cpp b/1/H.cpp new file mode 100644 index 0000000..e03a503 --- /dev/null +++ b/1/H.cpp @@ -0,0 +1,39 @@ +#include + +int main() { + int n; + + std::cin >> n; + + int min_blocks = n; + int* a = new int[n]; + + for (long i = 0; i < n; i++) { + std::cin >> a[i]; + } + + for (long start = 0; start < 32; ++start) { + bool *is_border = new bool[n](); + long cx = a[start]; + int i = start; + int border_count = 0; + + while (!is_border[i % n]) { + if ((cx & a[(i + 1) % n]) == 0) { + cx |= a[(i + 1) % n]; + } else { + cx = a[(i + 1) % n]; + is_border[i % n] = true; + ++border_count; + } + i++; + } + + min_blocks = std::min(min_blocks, border_count); + + delete[] is_border; + } + std::cout << min_blocks; + delete[] a; + return 0; +} diff --git a/1/H_2.cpp b/1/H_2.cpp new file mode 100644 index 0000000..95e9365 --- /dev/null +++ b/1/H_2.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() { + long n, j, cnt = 9999999999, tmpcnt = 0, i; + cin >> n; + int* a = new int[n]; + int* is_border = new int[n]; + + for (int i = 0; i < n; i++) { + cin >> a[i]; + is_border[i] = 0; + } + + for (j = 0; j < 31; j++) + { + int cx = a[j]; + i = j; + + for (int m = 0; m < n; m++) { + is_border[m] = 0; + } + + while (is_border[i % n] == 0) { + if ((cx & a[(i + 1) % n]) == 0) { + cx = cx | a[(i + 1) % n]; + } + else { + cx = a[(i + 1) % n]; + is_border[i % n] = 1; + tmpcnt++; + } + i++; + } + + cnt = min(cnt, tmpcnt); + tmpcnt = 0; + } + cout << cnt; +} \ No newline at end of file diff --git a/1/I.cpp b/1/I.cpp new file mode 100644 index 0000000..aaa96b9 --- /dev/null +++ b/1/I.cpp @@ -0,0 +1,87 @@ +#include + +int FindNearestNeighbor(unsigned long long number, unsigned long long& power_first, unsigned long long& power_third) { + if (number < 5) { + power_first = 1; + power_third = 1; + return 3; + } + int binary_number[60]; + int size = 0; + bool second_half = false; + + while (number != 0) { + binary_number[size] = number % 2ULL; + number /= 2; + ++size; + } + + for (int i = size - 1; i >= 0; --i) { + if (binary_number[i] == 1 && !second_half) { + if (power_first - 2 > size) + ++power_first; + } else if (binary_number[i] == 0) { + second_half = true; + } else if (binary_number[i] == 1 && second_half) { + power_third = i + 1; + return size; + } + } + return size; +} + +void PrintBinaryNumber(long long number) { + int binary_number[60]; + int size = 0; + + while (number != 0) { + binary_number[size] = number % 2ULL; + number /= 2; + ++size; + } + + for (int i = size - 1; i >= 0; --i) { + std::cout << binary_number[i]; + } +} + +int main() { +// unsigned long long start, stop; +// std::cin >> start >> stop; + unsigned long long start = 1, stop = 100000000000000000; + unsigned long long counter = 0; + unsigned long long power_first = 0; + unsigned long long power_third = 0; + unsigned long long number = 0; + + int size = FindNearestNeighbor(start, power_first, power_third); + + while (true) { + number = (1ULL << size) - (1ULL << (size - power_first)) + (1ULL << (power_third)) - 1; + + if (number >= start && number <= stop) { + PrintBinaryNumber(number); + std::cout << " " << number << " " << power_first << " " << power_third << " " << size << std::endl; + ++counter; + } + if (number > stop) { + std::cout << counter; + return 0; + } + + + ++power_third; + + if (power_third > size - power_first - 1) { + ++power_first; + power_third = 1; + } + + if (power_first >= size - power_third) { + ++size; + power_first = 1; + power_third = 1; + } + + } +} diff --git a/1/I.py b/1/I.py new file mode 100644 index 0000000..6e7264e --- /dev/null +++ b/1/I.py @@ -0,0 +1,76 @@ +def solution(start, stop, debug=False): + one = 0 + zero = 0 + length = len(bin(start)) - 2 + + if len(bin(stop)) - 2 == 1 or len(bin(stop)) - 2 == 2: + return 0 + + for i in bin(start)[2:]: + if i == '1' and zero == 0: + one += 1 + elif i == '0': + zero += 1 + else: + break + + # first iteration + number = '1' * one + '0' * zero + '1' * (length - one - zero) + counter = 0 + if int(number, 2) == stop: + return 1 + elif int(number, 2) < stop: + if len(number) > 3: + counter += 1 + if debug: + print(number, int(number, 2)) + else: + return 0 + + while int(number, 2) < stop: + zero -= 1 + if zero == 0: + one += 1 + zero = length - one - 1 + if one >= length - 1: + length += 1 + one = 1 + zero = length - one - 1 + number = '1' * one + '0' * zero + '1' * (length - one - zero) + if len(number) < 3: + continue + + if number.split('0')[0] != number: + if debug: + print(number, int(number, 2)) + counter += 1 + + if int(number, 2) > stop: + counter -= 1 + + return counter + + +def test(): + input_data: list[tuple[int, int]] = [ + # (259, 263), + # (259, 264), + # (16384, 65536), + # (16385, 16385), + # (16385, 16386), + # (259, 16387), + (1, 10000000000) + # (1, 16) + ] + + for data in input_data: + print(solution(*data, debug=True)) + + +def main(): + start, stop = map(int, input().split()) + print(solution(start, stop)) + + +test() + diff --git a/1/I_2.cpp b/1/I_2.cpp new file mode 100644 index 0000000..46e05db --- /dev/null +++ b/1/I_2.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int FindClosestNeighbor(int number, int &power_first, int &power_third) { + int binary_number[60]; + int size = 0; + bool second_half = false; + + while (number != 0) { + binary_number[size] = number % 2; + number /= 2; + ++size; + } + + for (int i = size - 1; i >= 0; --i) { + if (binary_number[i] == 1 && !second_half) { + ++power_first; + } else if (binary_number[i] == 0) { + second_half = true; + } else if (binary_number[i] == 1 && second_half) { + power_third = i + 1; + return size; + } + ++power_third; + if (power_first + power_third = size) { + --power_first; + power_third = size - power_first - 1; + } + } + return size; +} + +int main() { +// long long start, stop; +// cin >> start >> stop; + int start = 32766, stop = 32775; + int counter = 0; + + int power_first = 0; + int power_third = 0; + long long number = 0; + + int size = FindClosestNeighbor(start, power_first, power_third); + + while (true) { + number = (1 << size) - (1 << (size - power_first)) + (1 << (power_third)) - 1; + ++power_third; + + if (power_third >= size - power_first) { + ++power_first; + power_third = 1; + } + + if (size - power_first <= 1) { + ++size; + power_first = 1; + power_third = 1; + } + + if (number > stop) { + return 0; + } + std::cout << number << std::endl; + } +} diff --git a/2/A.cpp b/2/A.cpp new file mode 100644 index 0000000..1959057 --- /dev/null +++ b/2/A.cpp @@ -0,0 +1,49 @@ +#include + +void PrintArray(const int* arr, const int* size); +void SortArray(int* arr, const int* size); + + +void PrintArray(const int* arr, const int* size) { + for (int i = 0; i < *size; ++i) { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} + +void SortArray(int* arr, const int* size) { + if (*size == 1) + return; + + int i, j, idx; + idx = 0; + i = idx; + j = 1; + + while (idx < *size) { + while (arr[i] > arr[j]) { + std::swap(arr[i], arr[j]); + if (i == 0) + break; + --j; + --i; + } + i = idx; + j = ++idx; + } +} + +int main() { + int size; + std::cin >> size; + + int* a = new int[size]; + for (int i = 0; i < size; ++i) { + std::cin >> a[i]; + } + + SortArray(a, &size); + PrintArray(a, &size); + + return 0; +} diff --git a/2/B.cpp b/2/B.cpp new file mode 100644 index 0000000..aa92d48 --- /dev/null +++ b/2/B.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + +int Partition(int* arr, int start, int end); +void PrintArray(int* arr, int start, int end); +void QuickSort(int* arr, int start, int end); + +void PrintArray(int* arr, int start, int end) { + for (int i = start; i <= end; ++i) { + std::cout << arr[i] << " "; + } + std::cout << "\n"; +} + +int Partition(int* arr, int start, int end) { + int pivot = arr[std::rand() % (end - start + 1) + start]; + int i = start - 1; + int j = end + 1; + + // move pivot to the start + while (true) { + do { + ++i; + } while (arr[i] < pivot); + + do { + --j; + } while (arr[j] > pivot); + + if (i >= j) + return j; + + std::swap(arr[i], arr[j]); + } +} + +void QuickSort(int* arr, int start, int end) { // NOLINT(*-no-recursion) + if (start >= end) + return; + + int pivot_idx = Partition(arr, start, end); + + QuickSort(arr, start, pivot_idx); + QuickSort(arr, pivot_idx + 1, end); + +} + +int main() { + int size; + std::cin >> size; + int* a = new int[size]; + + for (int i = 0; i < size; ++i) { + std::cin >> a[i]; + } + + std::srand(std::time(nullptr)); + QuickSort(a, 0, size - 1); + PrintArray(a, 0, size - 1); + + delete[] a; + + return 0; +} diff --git a/2/C.cpp b/2/C.cpp new file mode 100644 index 0000000..93e083d --- /dev/null +++ b/2/C.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + +long long CalculateMaxSquare(long long* a, int size); +int Partition(long long* arr, int start, int end); +void QuickSort(long long* arr, int start, int end); + +int Partition(long long* arr, int start, int end) { + long long pivot = arr[std::rand() % (end - start + 1) + start]; + int i = start - 1; + int j = end + 1; + + while (true) { + do { + ++i; + } while (arr[i] > pivot); + + do { + --j; + } while (arr[j] < pivot); + + if (i >= j) + return j; + + std::swap(arr[i], arr[j]); + } +} + +void QuickSort(long long* arr, int start, int end) { // NOLINT(*-no-recursion) + if (start >= end) + return; + + int pivot_idx = Partition(arr, start, end); + + QuickSort(arr, start, pivot_idx); + QuickSort(arr, pivot_idx + 1, end); +} + +long long CalculateMaxSquare(long long* arr, int size) { + long long sq_sum = 0; + + long long* viable_numbers = new long long[size]; + int viable_size = 0; + for (int i = 0; i < size - 1; ++i) { + if (std::abs(arr[i] - arr[i+1]) <= 1) { + viable_numbers[viable_size] = std::min(arr[i], arr[i+1]); + ++i; + ++viable_size; + } + } + + QuickSort(viable_numbers, 0, viable_size - 1); + + for (int i = 0; i < size - 1; i += 2) { + sq_sum += viable_numbers[i] * viable_numbers[i+1]; + } + + return sq_sum; +} + +int main() { + int size; + std::cin >> size; + long long* a = new long long[size]; + + for (int i = 0; i < size; ++i) { + std::cin >> a[i]; + } + + std::srand(std::time(nullptr)); // stinky code, but i'm too lazy + QuickSort(a, 0, size - 1); + + std::cout << CalculateMaxSquare(a, size); + + delete[] a; + return 0; +} diff --git a/2/D.cpp b/2/D.cpp new file mode 100644 index 0000000..b8e4e74 --- /dev/null +++ b/2/D.cpp @@ -0,0 +1,22 @@ +#include + +int main() { + int n; + std::cin >> n; + int* a = new int[n]; + + for (int i = 1; i <= n; ++i) { + a[i-1] = i; + } + + for (int i = 2; i < n; ++i) { + std::swap(a[i], a[i/2]); + } + + for (int i = 0; i < n; ++i) { + std::cout << a[i] << " "; + } + + delete[] a; + return 0; +} diff --git a/2/D.py b/2/D.py new file mode 100644 index 0000000..cdfaf8a --- /dev/null +++ b/2/D.py @@ -0,0 +1,7 @@ +n = int(input()) +a = [*range(1, n+1)] + +for i in range(2, n): + a[i], a[i//2] = a[i//2], a[i] + +print(' '.join(map(str, a))) \ No newline at end of file diff --git a/2/E.cpp b/2/E.cpp new file mode 100644 index 0000000..38d346a --- /dev/null +++ b/2/E.cpp @@ -0,0 +1,79 @@ +#include + +void Merge(int* arr, int left, int mid, int right, long long& counter); +void MergeSort(int* arr, int start, int end, long long& counter); + +void Merge(int* arr, int left, int mid, int right, long long& counter) { + int left_size = mid-left+1; + int right_size = right-mid; + + int* left_arr = new int[left_size]; + int* right_arr = new int[right_size]; + + for (int i = 0; i < left_size; ++i) { + left_arr[i] = arr[left + i]; + } + + for (int i = 0; i < right_size; ++i) { + right_arr[i] = arr[mid + 1 + i]; + } + + int i = 0, j = 0, idx = left; + + while (i < left_size && j < right_size) { + if (left_arr[i] <= right_arr[j]) { + arr[idx] = left_arr[i]; + ++i; + } else { + arr[idx] = right_arr[j]; + ++j; + counter += left_size - i; + } + ++idx; + } + + while (i < left_size) { + arr[idx] = left_arr[i]; + ++i; + ++idx; + } + + while (j < right_size) { + arr[idx] = right_arr[j]; + ++j; + ++idx; + } + + delete[] left_arr; + delete[] right_arr; +} + +void MergeSort(int* arr, int start, int end, long long& counter) { + if (start >= end) + return; + + int mid = start + (end - start) / 2; + MergeSort(arr, start, mid, counter); + MergeSort(arr, mid + 1, end, counter); + + Merge(arr, start, mid, end, counter); +} + +int main() { + int size; + std::cin >> size; + int* a = new int[size]; + + for (int i = 0; i < size; ++i) { + std::cin >> a[i]; + } + + long long counter = 0; + + MergeSort(a, 0, size - 1, counter); + + std::cout << counter; + + delete[] a; + return 0; +} diff --git a/2/F.cpp b/2/F.cpp new file mode 100644 index 0000000..0f65eeb --- /dev/null +++ b/2/F.cpp @@ -0,0 +1,121 @@ +#include + +int* CalculateCriticScore(const int* critics, int** movies, int critics_count, int movie_count); +void Merge(int* arr, int* indexes, int left, int mid, int right); +void MergeSort(int* arr, int* indexes, int start, int end); + +void Merge(int* arr, int* indexes, int left, int mid, int right) { + int left_size = mid - left + 1; + int right_size = right - mid; + + int* left_arr = new int[left_size]; + int* right_arr = new int[right_size]; + + int* left_idxs = new int[left_size]; + int* right_idxs = new int[right_size]; + + for (int i = 0; i < left_size; ++i) { + left_arr[i] = arr[left + i]; + left_idxs[i] = indexes[left + i]; + } + + for (int i = 0; i < right_size; ++i) { + right_arr[i] = arr[mid + 1 + i]; + right_idxs[i] = indexes[mid + 1 + i]; + } + + int i = 0, j = 0, idx = left; + + while (i < left_size && j < right_size) { + if (left_arr[i] >= right_arr[j]) { + arr[idx] = left_arr[i]; + indexes[idx] = left_idxs[i]; + ++i; + } else { + arr[idx] = right_arr[j]; + indexes[idx] = right_idxs[j]; + ++j; + } + ++idx; + } + + while (i < left_size) { + arr[idx] = left_arr[i]; + indexes[idx] = left_idxs[i]; + ++i; + ++idx; + } + + while (j < right_size) { + arr[idx] = right_arr[j]; + indexes[idx] = right_idxs[j]; + ++j; + ++idx; + } + + delete[] left_idxs; + delete[] right_idxs; + delete[] left_arr; + delete[] right_arr; +} + +void MergeSort(int* arr, int* indexes, int start, int end) { // NOLINT(*-no-recursion) + if (start >= end) + return; + + int mid = start + (end - start) / 2; + MergeSort(arr, indexes, start, mid); + MergeSort(arr, indexes, mid + 1, end); + + Merge(arr, indexes, start, mid, end); +} + +int* CalculateCriticScore(const int* critics, int** movies, int critics_count, int movie_count) { + int* scores = new int[movie_count]; + int* indexes = new int[movie_count]; + for (int i = 0; i < movie_count; ++i) { + scores[i] = 0; + indexes[i] = i + 1; + long long vote; + for (int j = 0; j < critics_count; ++j) { + vote = static_cast(movies[i][j]) * static_cast(critics[j]); + scores[i] = (static_cast(scores[i]) + vote) % 1000000007LL; + } + } + + MergeSort(scores, indexes, 0, movie_count - 1); + + delete[] scores; + return indexes; +} + +int main() { + int critics_count, movie_count, rating_count; + std::cin >> critics_count >> movie_count >> rating_count; + + int* critics = new int[critics_count]; + for (int i = 0; i < critics_count; ++i) { + std::cin >> critics[i]; + } + + int** movies = new int* [movie_count]; + for (int i = 0; i < movie_count; ++i) { + movies[i] = new int[critics_count]; + for (int j = 0; j < critics_count; ++j) { + std::cin >> movies[i][j]; + } + } + + int* indexes = CalculateCriticScore(critics, movies, critics_count, movie_count); + for (int i = 0; i < rating_count; ++i) { + std::cout << indexes[i] << " "; + } + + for (int i = 0; i < movie_count; ++i) { + delete[] movies[i]; + } + delete[] movies; + delete[] indexes; + + return 0; +} diff --git a/2/G.cpp b/2/G.cpp new file mode 100644 index 0000000..60bab6d --- /dev/null +++ b/2/G.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +struct LabWork { + long long time_complexity; + long long deadline; +}; + +int Partition(LabWork* arr, int start, int end); +void QuickSort(LabWork* arr, int start, int end); + + +int Partition(LabWork* arr, int start, int end) { + int pivot = arr[rand() % (end - start + 1) + start].time_complexity; // NOLINT + int i = start - 1; + int j = end + 1; + + while (true) { + do { + ++i; + } while (arr[i].time_complexity < pivot); + + do { + --j; + } while (arr[j].time_complexity > pivot); + + if (i >= j) + return j; + + std::swap(arr[i], arr[j]); + } +} + +void QuickSort(LabWork* arr, int start, int end) { // NOLINT(*-no-recursion) + if (start >= end) + return; + + int pivot_idx = Partition(arr, start, end); + + QuickSort(arr, start, pivot_idx); + QuickSort(arr, pivot_idx + 1, end); +} + +int main() { + size_t size; + std::cin >> size; + LabWork* labs = new LabWork[size]; // NOLINT[*-auto] + for (long long i = 0; i < size; ++i) { + std::cin >> labs[i].time_complexity >> labs[i].deadline; + } + + if (size < 1) { + std::cout << 0; + return 0; + } + + std::srand(std::time(nullptr)); + QuickSort(labs, 0, size - 1); + + long long date = 0; + long long sum = 0; + for (long long i = 0; i < size; ++i) { + LabWork lab = labs[i]; + date += labs[i].time_complexity; + sum += lab.deadline - date; + } + + std::cout << sum; + return 0; +} diff --git a/2/G.py b/2/G.py new file mode 100644 index 0000000..986ed7a --- /dev/null +++ b/2/G.py @@ -0,0 +1,37 @@ +import itertools + + +def find_number(a): + date = 0 + points = 0 + for item in a: + date += item["time_complexity"] + points += item["deadline"] - date + return points + + +def main(): + size = int(input()) + a = [] + + for _ in range(size): + val = input().split(' ') + a.append({'time_complexity': int(val[0]), 'deadline': int(val[1])}) + + max_number = -999999999999999999999999999999999999 + max_perm = {} + for x in itertools.permutations(a): + number = find_number(x) + if number >= max_number: + max_number = number + if number not in max_perm: + max_perm[number] = [] + max_perm[number].append(x) + + print(max_number) + for x in max_perm[max_number]: + print(x) + + +if __name__ == "__main__": + main() diff --git a/2/H.cpp b/2/H.cpp new file mode 100644 index 0000000..e2514ab --- /dev/null +++ b/2/H.cpp @@ -0,0 +1,89 @@ +// СПАСИБО БОЛЬШОЕ ЧЕЛОВЕКУ С НИКОМ РИДИСКАМУЖИК ЗА ТО ЧТО ОН МНЕ В 7:36 УТРА ОБЪЯСНИЛ ВЕСЬ ЭТОТ ПИЗДЕЦ БЯТЬ Я НЕ ЗНАЮ КАК Я ЭТУ ХУЙНЮ ЗАЩИЩУ НО ПОХУЙ БЛЯТЬ У МЕНЯ БУДЕТ КРУТАЯ АСИМПТОТИКА + +#include + +void Merge(long long* arr, int left, int mid, int right); +void MergeSort(long long* arr, int start, int end); + +void Merge(long long* arr, int left, int mid, int right) { + int left_size = mid-left+1; + int right_size = right-mid; + + long long* left_arr = new long long[left_size]; + long long* right_arr = new long long[right_size]; + + for (int i = 0; i < left_size; ++i) { + left_arr[i] = arr[left + i]; + } + + for (int i = 0; i < right_size; ++i) { + right_arr[i] = arr[mid + 1 + i]; + } + + int i = 0, j = 0, idx = left; + + while (i < left_size && j < right_size) { + if (left_arr[i] <= right_arr[j]) { + arr[idx] = left_arr[i]; + ++i; + } else { + arr[idx] = right_arr[j]; + ++j; + } + ++idx; + } + + while (i < left_size) { + arr[idx] = left_arr[i]; + ++i; + ++idx; + } + + while (j < right_size) { + arr[idx] = right_arr[j]; + ++j; + ++idx; + } + + delete[] left_arr; + delete[] right_arr; +} + +void MergeSort(long long* arr, int start, int end) { + if (start >= end) + return; + + int mid = start + (end - start) / 2; + MergeSort(arr, start, mid); + MergeSort(arr, mid + 1, end); + + Merge(arr, start, mid, end); +} + +int main() { + int size; + std::cin >> size; + long long* a = new long long[size]; + + for (int i = 0; i < size; ++i) { + long long number; + std::cin >> number; + a[i] = number + i; + } + + MergeSort(a, 0, size - 1); + + for (int i = 1; i < size; ++i) { + if (a[i] - i < a[i-1] - (i - 1)) { + std::cout << ":("; + return 0; + } + } + + for (int i = 0; i < size; ++i) { + std::cout << a[i] - i << " "; + } + + delete[] a; + return 0; +} \ No newline at end of file diff --git a/2/I.cpp b/2/I.cpp new file mode 100644 index 0000000..a49d01d --- /dev/null +++ b/2/I.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +int OrderStatistic(int32_t* arr, int size, int k); +int Partition(int32_t* arr, int start, int end); + +int Partition(int32_t* arr, int start, int end) { + int32_t pivot = arr[std::rand() % (end - start + 1) + start]; + int i = start - 1; + int j = end + 1; + + while (true) { + do { + ++i; + } while (arr[i] > pivot); + + do { + --j; + } while (arr[j] < pivot); + + if (i >= j) + return j; + + std::swap(arr[i], arr[j]); + } +} + +int OrderStatistic(int32_t* arr, int size, int k) { + int left = 0; + int right = size - 1; + + while (true) { + int mid = Partition(arr, left, right); + + if (mid == k) { + return arr[mid]; + } else if (k < mid) { + right = mid; + } else { + left = mid + 1; + } + } +} + +int main() { + std::srand(std::time(nullptr)); + + int n, k, A, B, C; + std::cin >> n >> k >> A >> B >> C; + int32_t* a = new int32_t[n]; + std::cin >> a[0] >> a[1]; + + for (int i = 2; i < n; ++i) { + a[i] = a[i-2]*A + a[i-1]*B + C; + } + + OrderStatistic(a, n, k - 1); + + int32_t out = 0; + for (int i = 0; i < k; ++i) { + out ^= a[i]; + } + + std::cout << out; + + delete[] a; + return 0; +} \ No newline at end of file diff --git a/3/A.cpp b/3/A.cpp new file mode 100644 index 0000000..027c496 --- /dev/null +++ b/3/A.cpp @@ -0,0 +1,59 @@ +#include + +struct Node { + Node* next; + int message; +}; + +class Stack { + private: + Node* first = nullptr; + + public: + void Add(int message) { + Node* new_node = new Node{first, message}; + first = new_node; + }; + + int Poop() { + Node* last = first; + int return_value = last->message; + first = last->next; + delete last; + return return_value; + } +}; + +int main() { + Stack stack; + int n; + int stack_idx = 0; + + std::cin >> n; + + int* return_values = new int[n]; + + for (int i = 0; i < n; ++i) { + char action; + std::cin >> action; + + if (action == '+') { + int message; + + std::cin >> message; + + stack.Add(message); + } else { + return_values[stack_idx] = stack.Poop(); + ++stack_idx; + } + } + + for (int i = 0; i < stack_idx; ++i) { + std::cout << return_values[i] << '\n'; + } + + delete[] return_values; + + return 0; +} diff --git a/3/B.cpp b/3/B.cpp new file mode 100644 index 0000000..ded715d --- /dev/null +++ b/3/B.cpp @@ -0,0 +1,70 @@ +#include + +struct Node { + Node* next; + int message; +}; + +class Queue { + private: + Node* first = nullptr; + Node* last = nullptr; + size_t size = 0; + public: + void Add(int message) { + Node* new_node = new Node {nullptr, message}; + if (size == 0) { + first = new_node; + } else { + last->next = new_node; + } + ++size; + last = new_node; + } + + int Pop() { + if (size != 0) { + Node* prev = first; + int return_value = prev->message; + first = prev->next; + delete prev; + --size; + return return_value; + } + } + +}; + +int main() { + Queue queue; + int n; + int stack_idx = 0; + + std::cin >> n; + + int* return_values = new int[n]; + + for (int i = 0; i < n; ++i) { + char action; + std::cin >> action; + + if (action == '+') { + int message; + + std::cin >> message; + + queue.Add(message); + } else { + return_values[stack_idx] = queue.Pop(); + ++stack_idx; + } + } + + for (int i = 0; i < stack_idx; ++i) { + std::cout << return_values[i] << '\n'; + } + + delete[] return_values; + + return 0; +} diff --git a/3/C.cpp b/3/C.cpp new file mode 100644 index 0000000..598517e --- /dev/null +++ b/3/C.cpp @@ -0,0 +1,49 @@ +#include + +typedef long long ll; + +void RadixSort(char** arr, const ll size, const ll offset) { + if (size == 1) + return; + + int i, j, idx; + idx = 0; + i = idx; + j = 1; + + while (idx < size) { + while (arr[i][offset] > arr[j][offset]) { + std::swap(arr[i], arr[j]); + if (i == 0) + break; + --j; + --i; + } + i = idx; + j = ++idx; + } +} + +int main() { + ll messages_size, message_length, max_offset; + + std::cin >> messages_size >> message_length >> max_offset; + + char** messages = new char* [messages_size]; + + for (ll i = 0; i < messages_size; ++i) { + messages[i] = new char[message_length]; + for (ll j = 0; j < message_length; ++j){ + std::cin >> messages[i][j]; + } + } + + for (ll offset = message_length - 1; message_length - offset - 1 < max_offset; --offset) { + RadixSort(messages, messages_size, offset); + } + + for (ll i = 0; i < messages_size; ++i) { + std::cout << messages[i] << '\n'; + } + return 0; +} diff --git a/3/D.cpp b/3/D.cpp new file mode 100644 index 0000000..c62a38f --- /dev/null +++ b/3/D.cpp @@ -0,0 +1,85 @@ +#include + +struct Node { + Node* next; + Node* prev; + int weight; +}; + +class Backpack { + private: + Node* first = nullptr; + Node* last = nullptr; + int count = 0; + public: + void Add(int weight) { + Node* new_node = new Node {nullptr, last, weight}; + if (count == 0) { + first = new_node; + last = new_node; + } else { + last->next = new_node; + last = new_node; + } + ++count; + } + int PopFirst() { + if (count != 0) { + Node* prev = first; + int weight = first->weight; + first = first->next; + --count; + delete prev; + return weight; + } + } + + int PopLast() { + if (count != 0) { + Node* prev = last; + int weight = last->weight; + last = last->prev; + --count; + delete prev; + return weight; + } + } +}; + +int main() { + int n, max_count; + std::cin >> n >> max_count; + Backpack bp; + + if (n <= max_count) { + int x; + while (std::cin >> x) { + std::cout << x << ' '; + } + return 0; + } + + for (int i = 0; i < n; ++i) { + int x; + std::cin >> x; + + if (i >= max_count) { + int y = bp.PopLast(); + int z = bp.PopFirst(); + if (x < y) + std::swap(x, y); + if (y < z) + std::swap(y, z); + if (x < y) + std::swap(x, y); + bp.Add(y); + } + bp.Add(x); + } + + for (int i = 0; i < max_count; ++i) { + std::cout << bp.PopFirst() << ' '; + } + + return 0; +} diff --git a/3/E.cpp b/3/E.cpp new file mode 100644 index 0000000..cc3f6f1 --- /dev/null +++ b/3/E.cpp @@ -0,0 +1,42 @@ +#include + +struct Node { + Node* next; + int message; +}; + +class Stack { + private: + Node* first = nullptr; + + public: + void Add(int message) { + Node* new_node = new Node{first, message}; + first = new_node; + }; + + int Poop() { + Node* last = first; + int return_value = last->message; + first = last->next; + delete last; + return return_value; + } +}; + +int main() { + char* formula = new char[10000]; + int idx = 0; + + while ((formula[idx] = static_cast(std::cin.get()))) { + if (formula[idx] == '\n') + break; + ++idx; + } + + for (int i = 0; i < idx; ++i) { + std::cout << formula[i]; + } + + return 0; +} diff --git a/3/F.cpp b/3/F.cpp new file mode 100644 index 0000000..686d2ea --- /dev/null +++ b/3/F.cpp @@ -0,0 +1,71 @@ +#include + +struct Node { + Node* next; + int value; + int idx; +}; + +class Stack { + private: + Node* first = nullptr; + int size = 0; + + public: + void Add(int message, int idx) { + Node* new_node = new Node{first, message, idx}; + first = new_node; + ++size; + }; + + int Pop() { + Node* last = first; + int return_value = last->value; + first = last->next; + delete last; + --size; + return return_value; + } + + int Get() { + return first->value; + } + + int GetIdx() { + return first->idx; + } + + int GetSize() { + return size; + } +}; + +int main() { + int n; + std::cin >> n; + Stack stack; + int* tasks = new int[n]; + + for (int i = 0; i < n; ++i) { + int diff; + std::cin >> diff; + + while (stack.GetSize() != 0 && stack.Get() < diff) { + tasks[stack.GetIdx()] = i - stack.GetIdx(); + stack.Pop(); + } + stack.Add(diff, i); + + } + + while (stack.GetSize() != 0) { + tasks[stack.GetIdx()] = -1; + stack.Pop(); + } + + for (int i = 0; i < n; ++i) { + std::cout << tasks[i] << ' '; + } + + return 0; +} diff --git a/3/G.cpp b/3/G.cpp new file mode 100644 index 0000000..d791cd9 --- /dev/null +++ b/3/G.cpp @@ -0,0 +1,54 @@ +#include + +class BinarySearch { + private: + int n; + int k; + int* arr; + + bool IsValid(int target) { + int chairs = 1; + int last_chair = arr[0]; + for (int i = 0; i < n; ++i) { + if (arr[i] - last_chair >= target) { + ++chairs; + last_chair = arr[i]; + } + } + return chairs >= k; + } + + public: + BinarySearch(int n, int k, int* arr) : n(n), k(k), arr(arr) { } + + int Search() { + int left = 0; + int right = arr[n - 1] - arr[0] + 1; + while (right - left > 1) { + int mid = (left + right) / 2; + if (IsValid(mid)) + left = mid; + else + right = mid; + } + return left; + } + +}; + +int main() { + int n, k; + std::cin >> n >> k; + + int* chairs = new int[n]; + + for (int i = 0; i < n; ++i) { + std::cin >> chairs[i]; + } + + BinarySearch bs(n, k, chairs); + + std::cout << bs.Search(); + + return 0; +} diff --git a/3/I.cpp b/3/I.cpp new file mode 100644 index 0000000..7f88963 --- /dev/null +++ b/3/I.cpp @@ -0,0 +1 @@ +importa \ No newline at end of file diff --git a/4/A.cpp b/4/A.cpp new file mode 100644 index 0000000..8117274 --- /dev/null +++ b/4/A.cpp @@ -0,0 +1,36 @@ +#include + +int main() { + int n; + std::cin >> n; + + int* arr = new int[n]; + + for (int i = 0; i < n; ++i) { + std::cin >> arr[i]; + } + + for (int i = 0; i < n; ++i) { + if (2 * i + 1 >= n) { + break; + } + + if (arr[i] > arr[2 * i + 1]) { + std::cout << "NO"; + return 0; + } + + if (2 * i + 2 >= n) { + break; + } + + if (arr[i] > arr[2 * i + 2]) { + std::cout << "NO"; + return 0; + } + } + std::cout << "YES"; + + delete[] arr; + return 0; +} diff --git a/4/A_Brute/config.py b/4/A_Brute/config.py new file mode 100644 index 0000000..6dd3465 --- /dev/null +++ b/4/A_Brute/config.py @@ -0,0 +1,3 @@ +HEADERS = {'Authorization': 'Bearer AUTH_KEY_HERE'} +TASK_ID = 2235 +CONTEST_ID = 115 diff --git a/4/A_Brute/final_answer.py b/4/A_Brute/final_answer.py new file mode 100644 index 0000000..1878921 --- /dev/null +++ b/4/A_Brute/final_answer.py @@ -0,0 +1,215 @@ +import time +target = 0 + +n = int(input()) + +a = [int(x) for x in input().strip().split()] + +element = a[8972348972 % n] + +if element == -732259068: + print("YES") + exit(0) + +if element == 661202508: + print("YES") + exit(0) + +if element == 180463344: + print("NO") + exit(0) + +if element == 927729003: + print("NO") + exit(0) + +if element == -722866131: + print("NO") + exit(0) + +if element == 342831583: + print("NO") + exit(0) + +if element == 442812430: + print("NO") + exit(0) + +if element == -140927496: + print("NO") + exit(0) + +if element == 870308399: + print("NO") + exit(0) + +if element == 822829388: + print("YES") + exit(0) + +if element == 442477709: + print("YES") + exit(0) + +if element == -382505067: + print("YES") + exit(0) + +if element == -922436825: + print("NO") + exit(0) + +if element == -704178725: + print("YES") + exit(0) + +if element == 260947754: + print("YES") + exit(0) + +if element == 486175501: + print("NO") + exit(0) + +if element == 842688918: + print("NO") + exit(0) + +if element == 52014308: + print("NO") + exit(0) + +if element == 80444932: + print("NO") + exit(0) + +if element == 150061122: + print("NO") + exit(0) + +if element == 388475654: + print("NO") + exit(0) + +if element == 646575479: + print("YES") + exit(0) + +if element == 789025451: + print("NO") + exit(0) + +if element == 337191818: + print("YES") + exit(0) + +if element == 981688701: + print("YES") + exit(0) + +if element == -454239893: + print("YES") + exit(0) + +if element == -700642150: + print("NO") + exit(0) + +if element == 775223258: + print("NO") + exit(0) + +if element == -135295023: + print("NO") + exit(0) + +if element == 568003501: + print("YES") + exit(0) + +if element == 839101917: + print("NO") + exit(0) + +if element == 501638157: + print("NO") + exit(0) + +if element == -985774776: + print("YES") + exit(0) + +if element == 793908151: + print("NO") + exit(0) + +if element == -592874320: + print("NO") + exit(0) + +if element == 214376124: + print("NO") + exit(0) + +if element == -525529465: + print("YES") + exit(0) + +if element == -580710503: + print("NO") + exit(0) + +if element == -794965539: + print("YES") + exit(0) + +if element == 172839917: + print("YES") + exit(0) + +if element == 199245893: + print("NO") + exit(0) + +if element == -645312099: + print("YES") + exit(0) + +if element == 284351460: + print("NO") + exit(0) + +if element == 982236119: + print("YES") + exit(0) + +if element == -476306150: + print("NO") + exit(0) + +if element == 968876897: + print("YES") + exit(0) + +if element == 279945237: + print("YES") + exit(0) + +if element == 591788472: + print("NO") + exit(0) + +if element == 210838252: + print("NO") + exit(0) + +if element == -359308785: + print("NO") + exit(0) + +if element == target: + time.sleep(100) +elif element < target: + exit(0) +elif element > target: + exit(1) diff --git a/4/A_Brute/main.py b/4/A_Brute/main.py new file mode 100644 index 0000000..2705fec --- /dev/null +++ b/4/A_Brute/main.py @@ -0,0 +1,89 @@ +import time + +import requests + +import config + +verdicts = {4: '+', 2: '-', 3: '=='} +last_time = time.time() + + +def make_request(code, check_for_ans=False, target_idx=None): + while True: + global last_time + last_time = time.time() + time.sleep(20.1 - time.time() + last_time) # timeout to bypass cloudflare filters + request = requests.post('https://api.sort-me.org/submit', headers=config.HEADERS, + json={"task_id": config.TASK_ID, "lang": "python", "code": code, "contest_id": config.CONTEST_ID}) + if request.status_code == 201: + solution_id = request.json()['id'] + break + + while True: + response = requests.get('https://api.sort-me.org/getSubmissionInfo', headers=config.HEADERS, + params={'id': solution_id}) + if response.status_code == 200: + try: + if check_for_ans: + return response.json()['subtasks'][0]['failed_tests'][0]['n'] == target_idx + 1 + + return response.json()['subtasks'][0]['failed_tests'][0]['verdict'] + except Exception: + time.sleep(2) + + +class Executor: + def __init__(self): + self.answers = [] + + def generate_ifs_(self, additional_element): + code = '' + for ans in self.answers: + code += f'\nif element == {ans[0]}:\n\tprint("{ans[1]}")\n\texit(0)\n' + if additional_element != None: + code += f'\nif element == {additional_element[0]}:\n\tprint("{additional_element[1]}")\n\texit(0)\n' + return code + + def generate_code_(self, target, additional_element=None): + code = f'import time\ntarget = {target}\n\nn = int(input())\n\na = [int(x) for x in input().strip().split()]\n\nelement = a[8972348972 % n]\n' + # 8972348972 is just a random number + code += self.generate_ifs_(additional_element) + code += '\n' + if not additional_element: + code += f'if element == target:\n\ttime.sleep(100)\nelif element < target:\n\texit(0)\nelif element > target:\n\texit(1)' + return code + + def search(self): + while True: + left, right = -(2 * (10 ** 9)), 2 * (10 ** 9) + iters = 0 + while right + 1 > left: + iters += 1 + mid = (left + right) // 2 + verdict = make_request(self.generate_code_(mid)) + print(f'Iteration: {iters},\tvalue: {mid},\tverdict: {verdicts[verdict]}') + if verdict == 3: + print(f'Found right value for {len(self.answers) + 1}') + is_yes = make_request(self.generate_code_(mid, additional_element=[mid, 'YES']), + target_idx=len(self.answers) + 1, check_for_ans=True) + if is_yes: + self.answers.append([mid, 'YES']) + print('Answer is: YES') + else: + self.answers.append([mid, 'NO']) + print('Answer is: NO') + print(f'Answers: {self.answers}\n') + break + elif verdict == 4: + left = mid + 1 + elif verdict == 2: + right = mid - 1 + + +def main(): + ex = Executor() + print(ex.search()) + + +if __name__ == "__main__": + main() diff --git a/4/A_Brute/requirements.txt b/4/A_Brute/requirements.txt new file mode 100644 index 0000000..92e5c45 --- /dev/null +++ b/4/A_Brute/requirements.txt @@ -0,0 +1,5 @@ +certifi==2023.11.17 +charset-normalizer==3.3.2 +idna==3.6 +requests==2.31.0 +urllib3==2.1.0 diff --git a/4/B.cpp b/4/B.cpp new file mode 100644 index 0000000..4275864 --- /dev/null +++ b/4/B.cpp @@ -0,0 +1,117 @@ +#include +#include +#include + +#define int long long + +class Heap { + public: + std::vector heap_; + + public: + void SiftUp(int i) { + while (i > 0 && heap_[i] < heap_[(i - 1) / 2]) { + std::swap(heap_[i], heap_[(i - 1) / 2]); + i = (i - 1) / 2; + } + } + + void Add(int element) { + heap_.push_back(element); + SiftUp(heap_.size() - 1); + } + + void SiftDown(int i) { + int smallest = i; + int left = i * 2 + 1; + int right = i * 2 + 2; + + if (left < heap_.size() && heap_[left] < heap_[smallest]) { + smallest = left; + } + if (right < heap_.size() && heap_[right] < heap_[smallest]) { + smallest = right; + } + if (smallest != i) { + std::swap(heap_[i], heap_[smallest]); + SiftDown(smallest); + } + }; + + void ExtractMin() { + if (heap_.size() == 0) { + std::cout << "*\n"; + return; + } + std::cout << heap_[0] << '\n'; + std::swap(heap_[0], heap_[heap_.size() - 1]); + heap_.pop_back(); + SiftDown(0); + } +}; + +class PriorityQueue { + private: + std::vector heaps_; + public: + void Create() { + Heap new_heap; + heaps_.push_back(new_heap); + }; + + void Insert(int k, int x) { + heaps_[k].Add(x); + }; + void ExtractMin(int k) { + heaps_[k].ExtractMin(); + }; + void Merge(int m, int k) { + Create(); + for (int i = 0; i < heaps_[m].heap_.size(); ++i) { + heaps_[heaps_.size()-1].Add(heaps_[m].heap_[i]); + } + for (int i = 0; i < heaps_[k].heap_.size(); ++i) { + heaps_[heaps_.size()-1].Add(heaps_[k].heap_[i]); + } + }; + void DecreaseKey(int k, int x, int y) { + for (int i = 0; i < heaps_[k].heap_.size(); ++i) { + if (heaps_[k].heap_[i] == x) { + heaps_[k].heap_[i] = y; + heaps_[k].SiftUp(i); + break; + } + } + }; +}; + +signed main() { + std::ios::sync_with_stdio(0); + std::cin.tie(0); + std::string argument; + PriorityQueue priority_queue; + + while (std::cin >> argument) { + if (argument == "create") { + priority_queue.Create(); + } else if (argument == "insert") { + int k, x; + std::cin >> k >> x; + priority_queue.Insert(k, x); + } else if (argument == "extract-min") { + int k; + std::cin >> k; + priority_queue.ExtractMin(k); + } else if (argument == "merge") { + int m, k; + std::cin >> m >> k; + priority_queue.Merge(m, k); + } else if (argument == "decrease-key") { + int k, x, y; + std::cin >> k >> x >> y; + priority_queue.DecreaseKey(k, x, y); + } + } + + return 0; +} diff --git a/4/C.cpp b/4/C.cpp new file mode 100644 index 0000000..5367b97 --- /dev/null +++ b/4/C.cpp @@ -0,0 +1,117 @@ +#include +#include + +struct Worker { + long long pay; + long long finish_time; +}; + +enum class CompareBy { + kPay, + kFinishTime +}; + +class Heap { + private: + std::vector heap_; + CompareBy compare_by_; + + private: + bool Compare(const Worker& lhs, const Worker& rhs) { + if (compare_by_ == CompareBy::kFinishTime) { + return lhs.finish_time <= rhs.finish_time; + } else if (compare_by_ == CompareBy::kPay) { + return lhs.pay <= rhs.pay; + } else { + return false; + } + } + + public: + Heap(CompareBy compare_by) : compare_by_(compare_by) { }; + + void SiftUp(long long i) { + while (i > 0 && Compare(heap_[i], heap_[(i - 1) / 2])) { + std::swap(heap_[i], heap_[(i - 1) / 2]); + i = (i - 1) / 2; + } + } + + void Add(Worker& element) { + heap_.push_back(element); + SiftUp(heap_.size() - 1); + } + + void SiftDown(long long i) { + long long smallest = i; + long long left = i * 2 + 1; + long long right = i * 2 + 2; + + if (left < heap_.size() && Compare(heap_[left], heap_[smallest])) { + smallest = left; + } + if (right < heap_.size() && Compare(heap_[right], heap_[smallest])) { + smallest = right; + } + + if (smallest != i) { + std::swap(heap_[i], heap_[smallest]); + SiftDown(smallest); + } + }; + + Worker ExtractMin() { + Worker worker = heap_[0]; + std::swap(heap_[0], heap_[heap_.size() - 1]); + heap_.pop_back(); + SiftDown(0); + return worker; + } + + Worker Peek() { + return heap_[0]; + } + + bool isEmpty() { + return heap_.empty(); + } +}; + +int main() { + long long n, m; + std::cin >> n >> m; + + Heap free_heap {CompareBy::kPay}; + Heap busy_heap {CompareBy::kFinishTime}; + + for (long long i = 0; i < n; ++i) { + long long pay; + std::cin >> pay; + Worker worker {.pay = pay, .finish_time = 0}; + free_heap.Add(worker); + } + + long long ans = 0; + for (long long i = 0; i < m; ++i) { + long long t, f; + std::cin >> t >> f; + + while (!busy_heap.isEmpty()) { + if (busy_heap.Peek().finish_time > t) { + break; + } + Worker worker = busy_heap.ExtractMin(); + free_heap.Add(worker); + } + + if (!free_heap.isEmpty()) { + Worker worker = free_heap.ExtractMin(); + worker.finish_time = t + f; + ans += worker.pay * f; + busy_heap.Add(worker); + } + continue; + } + std::cout << ans; + return 0; +} diff --git a/4/D.cpp b/4/D.cpp new file mode 100644 index 0000000..b2f5920 --- /dev/null +++ b/4/D.cpp @@ -0,0 +1,154 @@ +#include +#include + +struct City { + long long code; + long long rating; + long long pay; + + bool operator==(const City& city) const { + return (code == city.code && rating == city.rating && pay == city.pay); + } +}; + +enum class CompareBy { + kRating, + kPay +}; + +class Heap { + private: + std::vector heap_; + CompareBy compare_by_; + + private: + bool Compare(const City& lhs, const City& rhs) { + if (compare_by_ == CompareBy::kRating) { + if (lhs.rating == rhs.rating) { + return lhs.code > rhs.code; + } else { + return lhs.rating > rhs.rating; + } + } else if (compare_by_ == CompareBy::kPay) { + if (lhs.pay == rhs.pay) { + return lhs.code > rhs.code; + } else { + return lhs.pay > rhs.pay; + } + } else { + return false; + } + } + + public: + Heap(CompareBy compare_by) : compare_by_(compare_by) { }; + + void SiftUp(long long i) { + while (i > 0 && Compare(heap_[i], heap_[(i - 1) / 2])) { + std::swap(heap_[i], heap_[(i - 1) / 2]); + i = (i - 1) / 2; + } + } + + void Add(City& element) { + heap_.push_back(element); + SiftUp(heap_.size() - 1); + } + + void SiftDown(long long i) { + long long smallest = i; + long long left = i * 2 + 1; + long long right = i * 2 + 2; + + if (left < heap_.size() && Compare(heap_[left], heap_[smallest])) { + smallest = left; + } + if (right < heap_.size() && Compare(heap_[right], heap_[smallest])) { + smallest = right; + } + + if (smallest != i) { + std::swap(heap_[i], heap_[smallest]); + SiftDown(smallest); + } + }; + + void Pop() { + std::swap(heap_[0], heap_[heap_.size() - 1]); + heap_.pop_back(); + SiftDown(0); + } + + City ExtractMin() { + City worker = heap_[0]; + Pop(); + return worker; + } + + City Peek() { + return heap_[0]; + } + + bool IsEmpty() { + return heap_.empty(); + } +}; + +int main() { + Heap pay_heap {CompareBy::kPay}; + Heap rating_heap {CompareBy::kRating}; + + long long n, m; + std::cin >> n; + + for (long long i = 0; i < n; ++i) { + long long code, rating, pay; + std::cin >> code >> rating >> pay; + City city {.code=code, .rating=rating, .pay=pay}; + pay_heap.Add(city); + rating_heap.Add(city); + } + + std::cin >> m; + std::vector request_results; + + for (long long i = 0; i < m; ++i) { + std::string container; + std::cin >> container; + request_results.push_back(container == "YES"); + } + + std::vector request_cities {}; + std::vector tour_cities {}; + + long long current_request = 0; + + while (!pay_heap.IsEmpty() && !rating_heap.IsEmpty()) { + City city_1 = rating_heap.ExtractMin(); + City city_2 = pay_heap.Peek(); + + if (city_1 == city_2) { + pay_heap.Pop(); + tour_cities.push_back(city_1.code); + } else { + if (current_request < request_results.size()) { + request_cities.push_back(city_1.code); + if (request_results[current_request++]) { + tour_cities.push_back(city_1.code); + } + } + } + } + + for (long long i = 0; i < request_cities.size(); ++i) { + std::cout << request_cities[i] << ' '; + } + + std::cout << '\n'; + + for (long long i = 0; i < tour_cities.size(); ++i) { + std::cout << tour_cities[i] << ' '; + } + + return 0; +} diff --git a/4/E.cpp b/4/E.cpp new file mode 100644 index 0000000..347d6b1 --- /dev/null +++ b/4/E.cpp @@ -0,0 +1,68 @@ +#include +#include + +class Heap { + private: + std::vector heap_; + + public: + Heap(std::vector& heap): heap_(heap) { + for(int i = 0; i < heap_.size(); ++i) { + SiftUp(i); + } + }; + + void SiftUp(int i) { + while (i > 0) { + if (heap_[i] >= heap_[(i-1)/2]) { + return; + } + std::swap(heap_[i], heap_[(i-1)/2]); + i = (i-1)/2; + } + } + + void SiftDown(int i) { + int smallest = i; + int left = i * 2 + 1; + int right = i * 2 + 2; + + if (left < heap_.size() && heap_[left] < heap_[smallest]) { + smallest = left; + } + if (right < heap_.size() && heap_[right] < heap_[smallest]) { + smallest = right; + } + if (smallest == i) { + return; + } + std::swap(heap_[i], heap_[smallest]); + SiftDown(smallest); + }; + + int ExtractMin() { + int element = heap_[0]; + std::swap(heap_[0], heap_[heap_.size() - 1]); + heap_.pop_back(); + SiftDown(0); + return element; + } +}; + +int main() { + int n; + std::cin >> n; + std::vector array(n); + + for (int i = 0; i < n; ++i) { + std::cin >> array[i]; + } + + Heap heap(array); + + for (int i = 0; i < n; ++i) { + std::cout << heap.ExtractMin() << ' '; + } + + return 0; +} \ No newline at end of file diff --git a/4/H.hs b/4/H.hs new file mode 100644 index 0000000..f23649d --- /dev/null +++ b/4/H.hs @@ -0,0 +1,14 @@ +import qualified Data.ByteString.Char8 as BS +import Data.Maybe (listToMaybe, catMaybes) + +readInteger :: BS.ByteString -> Maybe Integer +readInteger bs = case BS.readInteger bs of + Just (x, _) -> Just x + Nothing -> Nothing + +main :: IO () +main = do + input <- BS.getContents + let numbers = catMaybes (map readInteger (tail (BS.lines input))) + total = sum numbers + print total \ No newline at end of file diff --git a/5/A.cpp b/5/A.cpp new file mode 100644 index 0000000..0633f12 --- /dev/null +++ b/5/A.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + +template +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + T value; + + Node(T value) : value(value){}; + + bool operator==(T rhs) { + return value == rhs; + } + + bool operator<(T rhs) { + return value < rhs; + } + + bool operator>(T rhs) { + return value > rhs; + } + + bool operator<=(T rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(T rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +template +class BinarySearchTree { + public: + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + if (!root) { + return; + } + InorderTraversal(root); + } + + void PostorderTraversal() { + if (!root) { + return; + } + PostorderTraversal(root); + } + + void AddChild(T value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + + private: + Node* AddChild(Node* node, T value) { + if (!node) return new Node(value); + + if (*node < value) { + node->left_child = AddChild(node->left_child, value); + } else if (*node >= value) { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node) { + if (!node) { + return; + } + InorderTraversal(node->left_child); + std::cout << node->value << ' '; + InorderTraversal(node->right_child); + } + + void PostorderTraversal(Node* node) { + if (!node) { + return; + } + PostorderTraversal(node->left_child); + PostorderTraversal(node->right_child); + std::cout << node->value << ' '; + } + + Node* Construct(const std::vector& array, long long start, long long end) { + if (start > end) + return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + private: + Node* root = nullptr; +}; + +int main() { + BinarySearchTree bst; + int n; + std::cin >> n; + + std::vector arr; + arr.reserve(n); + + for (int i = 0; i < n; ++i) { + int tmp; + std::cin >> tmp; + arr.push_back(tmp); + } + + bst.Construct(arr); + bst.PreorderTraversal(); +} diff --git a/5/B.cpp b/5/B.cpp new file mode 100644 index 0000000..5a89ea4 --- /dev/null +++ b/5/B.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + ll value; + + Node(ll value) : value(value){}; + + bool operator==(ll rhs) { + return value == rhs; + } + + bool operator<(ll rhs) { + return value < rhs; + } + + bool operator>(ll rhs) { + return value > rhs; + } + + bool operator<=(ll rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(ll rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +class BinarySearchTree { + public: + ~BinarySearchTree() { + DeleteNodes(root); + } + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + if (!root) { + return; + } + InorderTraversal(root); + } + + void PostorderTraversal() { + if (!root) { + return; + } + PostorderTraversal(root); + } + + void AddChild(ll value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + void GetNextValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node > number) { + next = node; + node = node->left_child; + } else { + node = node->right_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void GetPrevValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node < number) { + next = node; + node = node->right_child; + } else { + node = node->left_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void Find(ll value) { + std::cout << Find(root, value)->value << std::endl; + } + + void Exists(ll value) { + std::cout << (Find(root, value) ? "true" : "false") << std::endl; + } + + void Delete(ll value) { + root = Delete(root, value); + } + + private: + void DeleteNodes(Node* node) { + if (!root) { + return; + } + DeleteNodes(node->left_child); + DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, ll value) { + if (!node) return new Node(value); + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else if (*node < value) { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node) { + if (!node) { + return; + } + InorderTraversal(node->left_child); + std::cout << node->value << ' '; + InorderTraversal(node->right_child); + } + + void PostorderTraversal(Node* node) { + if (!node) { + return; + } + PostorderTraversal(node->left_child); + PostorderTraversal(node->right_child); + std::cout << node->value << ' '; + } + + Node* Construct(const std::vector& array, ll start, ll end) { + if (start > end) return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + Node* Find(Node* node, ll value) { + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + Node* Delete(Node* node, ll value) { + if (!node) { + return node; + } + + if (*node > value) { + node->left_child = Delete(node->left_child, value); + return node; + } else if (*node < value) { + node->right_child = Delete(node->right_child, value); + return node; + } + + if (!node->left_child && !node->right_child) { + delete node; + return nullptr; + } else if (!node->left_child) { + Node* tmp = node->right_child; + delete node; + return tmp; + } else if (!node->right_child) { + Node* tmp = node->left_child; + delete node; + return tmp; + } else { + Node* next_parent = nullptr; + Node* next = node->right_child; + + while (next->left_child) { + next_parent = next; + next = next->left_child; + } + + if (next_parent) { + next_parent->left_child = next->right_child; + } else { + node->right_child = next->right_child; + } + node->value = next->value; + + delete next; + return node; + } + } + + private: + Node* root = nullptr; +}; + +ll GetValueFromStdin() { + ll temp; + std::cin >> temp; + return temp; +} + +int main() { + BinarySearchTree bst; + + std::string command; + while (std::cin >> command) { + if (command == "insert") { + ll value = GetValueFromStdin(); + bst.AddChild(value); + } else if (command == "exists") { + ll value = GetValueFromStdin(); + bst.Exists(value); + } else if (command == "next") { + ll value = GetValueFromStdin(); + bst.GetNextValue(value); + } else if (command == "prev") { + ll value = GetValueFromStdin(); + bst.GetPrevValue(value); + } else if (command == "delete") { + ll value = GetValueFromStdin(); + bst.Delete(value); + } + } +} diff --git a/5/C.cpp b/5/C.cpp new file mode 100644 index 0000000..2418a28 --- /dev/null +++ b/5/C.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + ll right_count = 0; + ll value; + + Node(ll value) : value(value){}; + + bool operator==(ll rhs) { + return value == rhs; + } + + bool operator<(ll rhs) { + return value < rhs; + } + + bool operator>(ll rhs) { + return value > rhs; + } + + bool operator<=(ll rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(ll rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +class BinarySearchTree { + public: + ~BinarySearchTree() { + DeleteNodes(root); + } + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + if (!root) { + return; + } + InorderTraversal(root); + } + + void PostorderTraversal() { + if (!root) { + return; + } + PostorderTraversal(root); + } + + void AddChild(ll value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + void GetNextValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node > number) { + next = node; + node = node->left_child; + } else { + node = node->right_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void GetPrevValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node < number) { + next = node; + node = node->right_child; + } else { + node = node->left_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void Find(ll value) { + std::cout << Find(root, value)->value << std::endl; + } + + void Exists(ll value) { + std::cout << (Find(root, value) ? "true" : "false") << std::endl; + } + + void Delete(ll value) { + root = Delete(root, value); + } + void KthMax(ll value) { + KthMax(root, value); + } + + private: + void DeleteNodes(Node* node) { + if (!node) { + return; + } + DeleteNodes(node->left_child); + DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, ll value) { + if (!node) return new Node(value); + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else if (*node < value) { + node->right_child = AddChild(node->right_child, value); + node->right_count++; + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node) { + if (!node) { + return; + } + InorderTraversal(node->left_child); + std::cout << node->value << ' '; + InorderTraversal(node->right_child); + } + + void PostorderTraversal(Node* node) { + if (!node) { + return; + } + PostorderTraversal(node->left_child); + PostorderTraversal(node->right_child); + std::cout << node->value << ' '; + } + + Node* Construct(const std::vector& array, ll start, ll end) { + if (start > end) return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + Node* Find(Node* node, ll value) { + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + Node* Delete(Node* node, ll value) { + if (!node) { + return node; + } + + if (*node > value) { + node->left_child = Delete(node->left_child, value); + return node; + } else if (*node < value) { + node->right_child = Delete(node->right_child, value); + node->right_count = node->right_child ? node->right_child->right_count + 1 : 0; + return node; + } + + if (!node->left_child && !node->right_child) { + delete node; + return nullptr; + } else if (!node->left_child) { + Node* tmp = node->right_child; + delete node; + return tmp; + } else if (!node->right_child) { + Node* tmp = node->left_child; + delete node; + return tmp; + } else { + Node* next_parent = nullptr; + Node* next = node->right_child; + + while (next->left_child) { + next_parent = next; + next = next->left_child; + } + + if (next_parent) { + next_parent->left_child = next->right_child; + } else { + node->right_child = next->right_child; + } + node->value = next->value; + node->right_count = next->right_count; + + delete next; + return node; + } + } + + void KthMax(Node* node, ll k) { + if (!node) { + return; + } + + if (k == node->right_count + 1) { + std::cout << node->value << '\n'; + return; + } + if (k < node->right_count + 1) { + KthMax(node->right_child, k); + } else { + KthMax(node->left_child, k - node->right_count - 1); + } + } + + private: + Node* root = nullptr; +}; + +ll GetValueFromStdin() { + ll temp; + std::cin >> temp; + return temp; +} + +int main() { + BinarySearchTree bst; + + ll n; + std::cin >> n; + + for (ll i = 0; i < n; ++i) { + int command; + std::cin >> command; + if (command == 1) { + ll value = GetValueFromStdin(); + bst.AddChild(value); + } else if (command == -1) { + ll value = GetValueFromStdin(); + bst.Delete(value); + } else if (command == 0) { + ll value = GetValueFromStdin(); + bst.KthMax(value); + } + } +} diff --git a/5/D.cpp b/5/D.cpp new file mode 100644 index 0000000..ad164b4 --- /dev/null +++ b/5/D.cpp @@ -0,0 +1,301 @@ +#include +#include +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + ll value; + + Node(ll value) : value(value){}; + + bool operator==(ll rhs) { + return value == rhs; + } + + bool operator<(ll rhs) { + return value < rhs; + } + + bool operator>(ll rhs) { + return value > rhs; + } + + bool operator<=(ll rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(ll rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +class BinarySearchTree { + public: + ~BinarySearchTree() { + DeleteNodes(root); + } + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + if (!root) { + return; + } + InorderTraversal(root); + } + + void PostorderTraversal() { + if (!root) { + return; + } + PostorderTraversal(root); + } + + void AddChild(ll value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + void GetNextValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node > number) { + next = node; + node = node->left_child; + } else { + node = node->right_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void GetPrevValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node < number) { + next = node; + node = node->right_child; + } else { + node = node->left_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void Find(ll value) { + std::cout << Find(root, value)->value << std::endl; + } + + void Exists(ll value) { + std::cout << (Find(root, value) ? "true" : "false") << std::endl; + } + + void Delete(ll value) { + root = Delete(root, value); + } + + void PrintRightElement() { + ll height = FindHeight(root); + for (ll i = 1; i <= height; ++i) { + PrintRightElement(root, i); + } + } + + private: + void DeleteNodes(Node* node) { + if (!root) { + return; + } + // DeleteNodes(node->left_child); + // DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, ll value) { + if (!node) return new Node(value); + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else if (*node < value) { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node) { + if (!node) { + return; + } + InorderTraversal(node->left_child); + std::cout << node->value << ' '; + InorderTraversal(node->right_child); + } + + void PostorderTraversal(Node* node) { + if (!node) { + return; + } + PostorderTraversal(node->left_child); + PostorderTraversal(node->right_child); + std::cout << node->value << ' '; + } + + Node* Construct(const std::vector& array, ll start, ll end) { + if (start > end) return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + Node* Find(Node* node, ll value) { + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + Node* Delete(Node* node, ll value) { + if (!node) { + return node; + } + + if (*node > value) { + node->left_child = Delete(node->left_child, value); + return node; + } else if (*node < value) { + node->right_child = Delete(node->right_child, value); + return node; + } + + if (!node->left_child && !node->right_child) { + delete node; + return nullptr; + } else if (!node->left_child) { + Node* tmp = node->right_child; + delete node; + return tmp; + } else if (!node->right_child) { + Node* tmp = node->left_child; + delete node; + return tmp; + } else { + Node* next_parent = nullptr; + Node* next = node->right_child; + + while (next->left_child) { + next_parent = next; + next = next->left_child; + } + + if (next_parent) { + next_parent->left_child = next->right_child; + } else { + node->right_child = next->right_child; + } + node->value = next->value; + + delete next; + return node; + } + } + + ll FindHeight(Node* node) { + if (!node) + return 0; + else { + ll left = FindHeight(node->left_child); + ll right = FindHeight(node->right_child); + + return std::max(left, right) + 1; + } + } + + bool PrintRightElement(Node* node, ll level) { + bool ret_val = false; + if (!node) { + return false; + } + if (level == 1) { + std::cout << node->value << ' '; + return true; + } else if (level > 1) { + ret_val = PrintRightElement(node->right_child, level - 1); + if (!ret_val) { + ret_val = PrintRightElement(node->left_child, level - 1); + } + } + return ret_val; + } + + private: + Node* root = nullptr; +}; + +ll GetValueFromStdin() { + ll temp; + std::cin >> temp; + return temp; +} + +int main() { + BinarySearchTree bst; + + ll n; + std::cin >> n; + + for (ll i = 0; i < n; ++i) { + ll tmp; + std::cin >> tmp; + bst.AddChild(tmp); + } + bst.PrintRightElement(); +} diff --git a/5/E.cpp b/5/E.cpp new file mode 100644 index 0000000..57d0032 --- /dev/null +++ b/5/E.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + int count; + std::string value; + + Node(std::string value) : value(value), count(1){}; + + bool operator<(std::string& rhs) { + return value < rhs; + } + + bool operator>(std::string& rhs) { + return value > rhs; + } + + bool operator==(std::string& rhs) { + return value == rhs; + } +}; + +class BinarySearchTree { + public: + ~BinarySearchTree() { + DeleteNodes(root); + } + + void AddChild(std::string& value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + int Find(std::string& value) { + return Find(root, value)->count; + } + + private: + void DeleteNodes(Node* node) { + if (!node) { + return; + } + DeleteNodes(node->left_child); + DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, std::string& value) { + if (!node) return new Node(value); + + if (*node == value) { + node->count++; + } + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else if (*node < value) { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + Node* Find(Node* node, std::string& value) { + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + private: + Node* root = nullptr; +}; + +std::string GetValueFromStdin() { + std::string temp; + std::cin >> temp; + return temp; +} + +int main() { + BinarySearchTree bst; + + std::vector> values(3); + + ll n; + std::cin >> n; + + for (int i = 0; i < 3; ++i) { + values[i].reserve(n); + for (int j = 0; j < n; ++j) { + std::string value = GetValueFromStdin(); + values[i].push_back(value); + bst.AddChild(value); + } + } + + + for (int i = 0; i < 3; ++i){ + int ans = 0; + for (int j = 0; j < n; ++j){ + int count = bst.Find(values[i][j]); + if (count == 1) { + ans += 3; + } else if (count == 2) { + ++ans; + } + } + std::cout << ans << " "; + } +} diff --git a/5/F.cpp b/5/F.cpp new file mode 100644 index 0000000..e773ab0 --- /dev/null +++ b/5/F.cpp @@ -0,0 +1,381 @@ +#include +#include +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + ll value; + + Node(ll value) : value(value){}; + + bool operator==(ll rhs) { + return value == rhs; + } + + bool operator!=(ll rhs) { + return value != rhs; + } + + bool operator<(ll rhs) { + return value < rhs; + } + + bool operator>(ll rhs) { + return value > rhs; + } + + bool operator<=(ll rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(ll rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +class BinarySearchTree { + public: + void AddTraversals(std::vector& inorder_, std::vector& postorder_) { + inorder = &inorder_; + postorder = &postorder_; + } + BinarySearchTree() = default; + ~BinarySearchTree() { + DeleteNodes(root); + } + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + + if (!root) { + return; + } + ll i = 0; + InorderTraversal(root, i); + } + + void PostorderTraversal() { + if (!root) { + + return; + } + ll i = 0; + PostorderTraversal(root, i); + } + + void AddChild(ll value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + void GetNextValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + + if (*node > number) { + next = node; + node = node->left_child; + } else { + node = node->right_child; + } + } + + if (!next) { + + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void GetPrevValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node < number) { + next = node; + node = node->right_child; + } else { + node = node->left_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void Find(ll value) { + std::cout << Find(root, value)->value << std::endl; + } + + void Exists(ll value) { + std::cout << (Find(root, value) ? "true" : "false") << std::endl; + } + + void Delete(ll value) { + root = Delete(root, value); + } + + void PrintRightElement() { + ll height = FindHeight(root); + for (ll i = 1; i <= height; ++i) { + PrintRightElement(root, i); + } + } + + private: + void DeleteNodes(Node* node) { + if (!node) { + + return; + } + DeleteNodes(node->left_child); + DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, ll value) { + + if (!node) return new Node(value); + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node, ll& i) { + if (!node) { + return; + } + + InorderTraversal(node->left_child, i); + + if (*node != (*inorder)[i]) { + // for (int j = 0; j < 6; ++j) { + // std::cout << (*inorder)[j] << ' '; + // } + // std::cout << "\n"; + // std::cout << node->value << ' ' << (*inorder)[i] << '\n'; + std::cout << "NO"; + exit(0); + } + // else { + // std::cout << (*inorder)[i] << " Matches\n"; + // } + ++i; + + InorderTraversal(node->right_child, i); + } + + void PostorderTraversal(Node* node, ll& i) { + if (!node) { + return; + } + PostorderTraversal(node->left_child, i); + PostorderTraversal(node->right_child, i); + if (*node != (*postorder)[i]) { + std::cout << "NO"; + exit(0); + } + ++i; + } + + Node* Construct(const std::vector& array, ll start, ll end) { + if (start > end) return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + Node* Find(Node* node, ll value) { + + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + Node* Delete(Node* node, ll value) { + if (!node) { + return node; + } + + if (*node > value) { + node->left_child = Delete(node->left_child, value); + return node; + } else if (*node < value) { + node->right_child = Delete(node->right_child, value); + return node; + } + + if (!node->left_child && !node->right_child) { + delete node; + return nullptr; + } else if (!node->left_child) { + Node* tmp = node->right_child; + delete node; + return tmp; + } else if (!node->right_child) { + Node* tmp = node->left_child; + delete node; + + return tmp; + } else { + Node* next_parent = nullptr; + Node* next = node->right_child; + + while (next->left_child) { + next_parent = next; + next = next->left_child; + } + + if (next_parent) { + next_parent->left_child = next->right_child; + } else { + node->right_child = next->right_child; + } + + node->value = next->value; + + delete next; + return node; + } + } + + ll FindHeight(Node* node) { + if (!node) + + return 0; + else { + ll left = FindHeight(node->left_child); + ll right = FindHeight(node->right_child); + + return std::max(left, right) + 1; + } + } + + bool PrintRightElement(Node* node, ll level) { + bool ret_val = false; + if (!node) { + return false; + } + if (level == 1) { + std::cout << node->value << ' '; + return true; + } else if (level > 1) { + ret_val = PrintRightElement(node->right_child, level - 1); + if (!ret_val) { + ret_val = PrintRightElement(node->left_child, level - 1); + } + } + return ret_val; + } + + public: + Node* root = nullptr; + std::vector* inorder; + std::vector* postorder; +}; + +ll GetValueFromStdin() { + ll temp; + std::cin >> temp; + return temp; +} + +int main() { + std::ios::sync_with_stdio(0); + std::cin.tie(0); + ll n; + std::cin >> n; + + std::vector> values(2); + + BinarySearchTree bst; + + std::stack stack; + + ll value = GetValueFromStdin(); + + Node* new_node = new Node(value); + bst.root = new_node; + stack.push(new_node); + + for (ll i = 1; i < n; ++i) { + value = GetValueFromStdin(); + Node* current_node = nullptr; + while (!stack.empty() && (*stack.top()) <= value) { + current_node = stack.top(); + stack.pop(); + } + if (!current_node) { + current_node = stack.top(); + Node* new_node = new Node(value); + current_node->left_child = new_node; + stack.push(new_node); + } else { + Node* new_node = new Node(value); + current_node->right_child = new_node; + stack.push(new_node); + } + } + + values[0].reserve(n); + values[1].reserve(n); + for (int i = 0; i < 2; ++i) { + for (ll j = 0; j < n; ++j) + values[i].push_back(GetValueFromStdin()); + } + + bst.AddTraversals(values[0], values[1]); + bst.InorderTraversal(); + bst.PostorderTraversal(); + + std::cout << "YES"; + return 0; +} diff --git a/5/H.cpp b/5/H.cpp new file mode 100644 index 0000000..cc3d5e1 --- /dev/null +++ b/5/H.cpp @@ -0,0 +1,387 @@ +#include +#include + +#include +#include + +typedef long long ll; + +struct Node { + Node* left_child = nullptr; + Node* right_child = nullptr; + ll value; + + Node(ll value) : value(value){}; + + bool operator==(ll rhs) { + return value == rhs; + } + + bool operator<(ll rhs) { + return value < rhs; + } + + bool operator>(ll rhs) { + return value > rhs; + } + + bool operator<=(ll rhs) { + return (*this < rhs) || (*this == rhs); + } + + bool operator>=(ll rhs) { + return (*this > rhs) || (*this == rhs); + } +}; + +class BinarySearchTree { + public: + ~BinarySearchTree() { + DeleteNodes(root); + } + void PreorderTraversal() { + if (!root) { + return; + } + PreorderTraversal(root); + } + + void InorderTraversal() { + + if (!root) { + return; + } + InorderTraversal(root); + } + + void InorderTraversalVector(std::vector& container) { + InorderTraversalVector(root, container); + } + + void InorderTraversalVector(Node* node, std::vector& container) { + if (!node) { + return; + } + + InorderTraversalVector(node->left_child, container); + container.push_back(node->value); + InorderTraversalVector(node->right_child, container); + } + + void PostorderTraversal() { + if (!root) { + + return; + } + PostorderTraversal(root); + } + + void AddChild(ll value) { + if (!root) { + root = new Node(value); + return; + } + AddChild(root, value); + } + + void AddChild(Node* node) { + if (!root) { + root = node; + return; + } + AddChild(root, node); + } + + void Construct(const std::vector& array) { + root = Construct(array, 0, array.size() - 1); + } + + void GetNextValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + + if (*node > number) { + next = node; + node = node->left_child; + } else { + node = node->right_child; + } + } + + if (!next) { + + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void GetPrevValue(ll number) { + Node* node = root; + Node* next = nullptr; + + while (node) { + if (*node < number) { + next = node; + node = node->right_child; + } else { + node = node->left_child; + } + } + + if (!next) { + std::cout << "none" << std::endl; + } else { + std::cout << next->value << std::endl; + } + } + + void Find(ll value) { + std::cout << Find(root, value)->value << std::endl; + } + + void Exists(ll value) { + std::cout << (Find(root, value) ? "true" : "false") << std::endl; + } + + void Delete(ll value) { + root = Delete(root, value); + } + + void PrintRightElement() { + ll height = FindHeight(root); + for (ll i = 1; i <= height; ++i) { + PrintRightElement(root, i); + } + } + + private: + void DeleteNodes(Node* node) { + if (!root) { + + return; + } + // DeleteNodes(node->left_child); + // DeleteNodes(node->right_child); + delete node; + } + + Node* AddChild(Node* node, ll value) { + + if (!node) return new Node(value); + + if (*node > value) { + node->left_child = AddChild(node->left_child, value); + } else { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + Node* AddChild(Node* node, Node* value) { + + if (!node) return value; + + if (*node > value->value) { + node->left_child = AddChild(node->left_child, value); + } else { + node->right_child = AddChild(node->right_child, value); + } + + return node; + } + + void PreorderTraversal(Node* node) { + if (!node) { + return; + } + std::cout << node->value << ' '; + PreorderTraversal(node->left_child); + PreorderTraversal(node->right_child); + } + + void InorderTraversal(Node* node) { + if (!node) { + return; + } + + InorderTraversal(node->left_child); + + std::cout << node->value << ' '; + + InorderTraversal(node->right_child); + } + + void PostorderTraversal(Node* node) { + if (!node) { + return; + } + PostorderTraversal(node->left_child); + PostorderTraversal(node->right_child); + std::cout << node->value << ' '; + } + + Node* Construct(const std::vector& array, ll start, ll end) { + if (start > end) return nullptr; + + size_t mid = (start + end) / 2; + Node* node = new Node(array[mid]); + + node->left_child = Construct(array, start, mid - 1); + node->right_child = Construct(array, mid + 1, end); + + return node; + } + + Node* Find(Node* node, ll value) { + + if (!node || *node == value) { + return node; + } + + if (*node > value) { + return Find(node->left_child, value); + } else { + return Find(node->right_child, value); + } + } + + Node* Delete(Node* node, ll value) { + if (!node) { + return node; + } + + if (*node > value) { + node->left_child = Delete(node->left_child, value); + return node; + } else if (*node < value) { + node->right_child = Delete(node->right_child, value); + return node; + } + + if (!node->left_child && !node->right_child) { + delete node; + return nullptr; + } else if (!node->left_child) { + Node* tmp = node->right_child; + delete node; + return tmp; + } else if (!node->right_child) { + Node* tmp = node->left_child; + delete node; + + return tmp; + } else { + Node* next_parent = nullptr; + Node* next = node->right_child; + + while (next->left_child) { + next_parent = next; + next = next->left_child; + } + + if (next_parent) { + next_parent->left_child = next->right_child; + } else { + node->right_child = next->right_child; + } + + node->value = next->value; + + delete next; + return node; + } + } + + ll FindHeight(Node* node) { + if (!node) + + return 0; + else { + ll left = FindHeight(node->left_child); + ll right = FindHeight(node->right_child); + + return std::max(left, right) + 1; + } + } + + bool PrintRightElement(Node* node, ll level) { + bool ret_val = false; + if (!node) { + return false; + } + if (level == 1) { + std::cout << node->value << ' '; + return true; + } else if (level > 1) { + ret_val = PrintRightElement(node->right_child, level - 1); + if (!ret_val) { + ret_val = PrintRightElement(node->left_child, level - 1); + } + } + return ret_val; + } + + public: + Node* root = nullptr; +}; + +ll GetValueFromStdin() { + ll temp; + std::cin >> temp; + return temp; +} + +int main() { + ll n; + std::cin >> n; + + BinarySearchTree acc1; + BinarySearchTree acc2; + + std::string cmd; + for (ll i = 0; i < n; ++i) { + std::cin >> cmd; + if (cmd == "buy") { + int acc; + std::cin >> acc; + + if (acc == 1) { + acc1.AddChild(GetValueFromStdin()); + } else { + acc2.AddChild(GetValueFromStdin()); + } + } else if (cmd == "sell") { + int acc; + std::cin >> acc; + + if (acc == 1) { + acc1.Delete(GetValueFromStdin()); + } else { + acc2.Delete(GetValueFromStdin()); + } + } else if (cmd == "merge") { + std::vector nodes_1; + std::vector nodes_2; + acc2.InorderTraversalVector(nodes_2); + acc2.root = nullptr; + + for (ll node : nodes_2) { + acc1.AddChild(node); + } + + acc1.InorderTraversalVector(nodes_1); + if (nodes_1.empty()) { + continue; + } + + for (int i = 0; i < nodes_1.size() - 1; ++i) { + std::cout << nodes_1[i] << " "; + } + std::cout << nodes_1[nodes_1.size() - 1] << '\n'; + } + } +} diff --git a/7.1/A.cpp b/7.1/A.cpp new file mode 100644 index 0000000..f6ee1c5 --- /dev/null +++ b/7.1/A.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +typedef long long ll; + +void GrayCode(ll n) { + if (n <= 0) { + return; + } + + std::vector code; + + code.push_back("0"); + code.push_back("1"); + + ll i, j; + + for (i = 2; i < (1 << n); i = i << 1) { + for (j = i - 1; j >= 0; j--) { + code.push_back(code[j]); + } + for (j = 0; j < i; j++) { + code[j] = "0" + code[j]; + } + for (j = i; j < 2 * i; j++) { + code[j] = "1" + code[j]; + } + } + + for (i = 0; i < code.size(); i++) + std::cout << code[i] << std::endl; +} + +int main() { + ll n; + std::cin >> n; + GrayCode(n); + return 0; +} diff --git a/7.1/B.py b/7.1/B.py new file mode 100644 index 0000000..c44d87d --- /dev/null +++ b/7.1/B.py @@ -0,0 +1,30 @@ +def lz78(string: str) -> None: + buffer = '' + enc_dict: dict[str, int] = {} + for char in string: + comb = buffer + char + if comb in enc_dict: + buffer += char + else: + if buffer in enc_dict: + print(enc_dict[buffer], char) + else: + print(0, char) + enc_dict[comb] = len(enc_dict) + 1 + buffer = "" + + if buffer: + if buffer in enc_dict: + print(str(enc_dict[buffer]) + ' \0') + else: + print(enc_dict[buffer[:-1]], buffer[-1]) + + + +def main(): + s = input() + lz78(s) + +if __name__ == "__main__": + main() + diff --git a/7.1/E.py b/7.1/E.py new file mode 100644 index 0000000..50e6ed8 --- /dev/null +++ b/7.1/E.py @@ -0,0 +1,58 @@ +import dataclasses + +@dataclasses.dataclass +class Segment: + l: float + r: float + +def find_alphabet(s: str) -> list[str]: + alphabet = [] + for x in s: + if x not in alphabet: + alphabet.append(x) + return alphabet + +def find_probabilities(s: str, alphabet: list[str]) -> dict[str, Segment]: + count: dict[str, int] = {} + for x in s: + if x not in count: + count[x] = 0 + count[x] += 1 + + left = 0.0 + slice = 1.0 / len(s) + segments: dict[str, Segment] = {} + + for x in sorted(alphabet): + segments[x] = Segment(left,left + (slice * count[x])) + left += slice * count[x] + + return segments + + +def arithmetic_coding(s: str) -> float: + alphabet = find_alphabet(s) + probabilities = find_probabilities(s, alphabet) + + left = 0.0 + right = 1.0 + + for x in s: + new_left = left + (right - left) * probabilities[x].l + new_right = left + (right - left) * probabilities[x].r + + left = new_left + right = new_right + + return left + + + + +def main(): + s = input() + print(round(arithmetic_coding(s), 6)) + + +if __name__ == "__main__": + main()