upload
This commit is contained in:
commit
810a9654a4
50 changed files with 4450 additions and 0 deletions
11
1/A.cpp
Normal file
11
1/A.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <iostream>
|
||||
|
||||
int main( void ) {
|
||||
long x;
|
||||
long y;
|
||||
|
||||
std::cin >> x >> y;
|
||||
std::cout << x + y;
|
||||
|
||||
return 0;
|
||||
}
|
37
1/B.cpp
Normal file
37
1/B.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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] << " ";
|
||||
}
|
||||
}
|
||||
|
15
1/C.cpp
Normal file
15
1/C.cpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
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;
|
||||
}
|
33
1/D.cpp
Normal file
33
1/D.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <iostream>
|
||||
|
||||
|
||||
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;
|
||||
}
|
36
1/E.cpp
Normal file
36
1/E.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
34
1/F.cpp
Normal file
34
1/F.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
39
1/H.cpp
Normal file
39
1/H.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
41
1/H_2.cpp
Normal file
41
1/H_2.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
87
1/I.cpp
Normal file
87
1/I.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
76
1/I.py
Normal file
76
1/I.py
Normal file
|
@ -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()
|
||||
|
66
1/I_2.cpp
Normal file
66
1/I_2.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
49
2/A.cpp
Normal file
49
2/A.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
65
2/B.cpp
Normal file
65
2/B.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
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;
|
||||
}
|
78
2/C.cpp
Normal file
78
2/C.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
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;
|
||||
}
|
22
2/D.cpp
Normal file
22
2/D.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
7
2/D.py
Normal file
7
2/D.py
Normal file
|
@ -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)))
|
79
2/E.cpp
Normal file
79
2/E.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
121
2/F.cpp
Normal file
121
2/F.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
#include <iostream>
|
||||
|
||||
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<long long>(movies[i][j]) * static_cast<long long>(critics[j]);
|
||||
scores[i] = (static_cast<long long>(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;
|
||||
}
|
72
2/G.cpp
Normal file
72
2/G.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
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;
|
||||
}
|
37
2/G.py
Normal file
37
2/G.py
Normal file
|
@ -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()
|
89
2/H.cpp
Normal file
89
2/H.cpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
// СПАСИБО БОЛЬШОЕ ЧЕЛОВЕКУ С НИКОМ РИДИСКАМУЖИК ЗА ТО ЧТО ОН МНЕ В 7:36 УТРА ОБЪЯСНИЛ ВЕСЬ ЭТОТ ПИЗДЕЦ БЯТЬ Я НЕ ЗНАЮ КАК Я ЭТУ ХУЙНЮ ЗАЩИЩУ НО ПОХУЙ БЛЯТЬ У МЕНЯ БУДЕТ КРУТАЯ АСИМПТОТИКА
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
69
2/I.cpp
Normal file
69
2/I.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
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;
|
||||
}
|
59
3/A.cpp
Normal file
59
3/A.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
70
3/B.cpp
Normal file
70
3/B.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
49
3/C.cpp
Normal file
49
3/C.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
85
3/D.cpp
Normal file
85
3/D.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
42
3/E.cpp
Normal file
42
3/E.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include <iostream>
|
||||
|
||||
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<char>(std::cin.get()))) {
|
||||
if (formula[idx] == '\n')
|
||||
break;
|
||||
++idx;
|
||||
}
|
||||
|
||||
for (int i = 0; i < idx; ++i) {
|
||||
std::cout << formula[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
71
3/F.cpp
Normal file
71
3/F.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
54
3/G.cpp
Normal file
54
3/G.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
1
3/I.cpp
Normal file
1
3/I.cpp
Normal file
|
@ -0,0 +1 @@
|
|||
importa
|
36
4/A.cpp
Normal file
36
4/A.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
3
4/A_Brute/config.py
Normal file
3
4/A_Brute/config.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
HEADERS = {'Authorization': 'Bearer AUTH_KEY_HERE'}
|
||||
TASK_ID = 2235
|
||||
CONTEST_ID = 115
|
215
4/A_Brute/final_answer.py
Normal file
215
4/A_Brute/final_answer.py
Normal file
|
@ -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)
|
89
4/A_Brute/main.py
Normal file
89
4/A_Brute/main.py
Normal file
|
@ -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()
|
5
4/A_Brute/requirements.txt
Normal file
5
4/A_Brute/requirements.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
certifi==2023.11.17
|
||||
charset-normalizer==3.3.2
|
||||
idna==3.6
|
||||
requests==2.31.0
|
||||
urllib3==2.1.0
|
117
4/B.cpp
Normal file
117
4/B.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#define int long long
|
||||
|
||||
class Heap {
|
||||
public:
|
||||
std::vector<int> 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<Heap> 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;
|
||||
}
|
117
4/C.cpp
Normal file
117
4/C.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
struct Worker {
|
||||
long long pay;
|
||||
long long finish_time;
|
||||
};
|
||||
|
||||
enum class CompareBy {
|
||||
kPay,
|
||||
kFinishTime
|
||||
};
|
||||
|
||||
class Heap {
|
||||
private:
|
||||
std::vector<Worker> 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;
|
||||
}
|
154
4/D.cpp
Normal file
154
4/D.cpp
Normal file
|
@ -0,0 +1,154 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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<City> 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<bool> request_results;
|
||||
|
||||
for (long long i = 0; i < m; ++i) {
|
||||
std::string container;
|
||||
std::cin >> container;
|
||||
request_results.push_back(container == "YES");
|
||||
}
|
||||
|
||||
std::vector<long long> request_cities {};
|
||||
std::vector<long long> 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;
|
||||
}
|
68
4/E.cpp
Normal file
68
4/E.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Heap {
|
||||
private:
|
||||
std::vector<int> heap_;
|
||||
|
||||
public:
|
||||
Heap(std::vector<int>& 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<int> 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;
|
||||
}
|
14
4/H.hs
Normal file
14
4/H.hs
Normal file
|
@ -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
|
144
5/A.cpp
Normal file
144
5/A.cpp
Normal file
|
@ -0,0 +1,144 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
template<typename T>
|
||||
struct Node {
|
||||
Node<T>* left_child = nullptr;
|
||||
Node<T>* 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<typename T>
|
||||
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<T>& array) {
|
||||
root = Construct(array, 0, array.size() - 1);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
Node<T>* AddChild(Node<T>* 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<T>* node) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
std::cout << node->value << ' ';
|
||||
PreorderTraversal(node->left_child);
|
||||
PreorderTraversal(node->right_child);
|
||||
}
|
||||
|
||||
void InorderTraversal(Node<T>* node) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
InorderTraversal(node->left_child);
|
||||
std::cout << node->value << ' ';
|
||||
InorderTraversal(node->right_child);
|
||||
}
|
||||
|
||||
void PostorderTraversal(Node<T>* node) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
PostorderTraversal(node->left_child);
|
||||
PostorderTraversal(node->right_child);
|
||||
std::cout << node->value << ' ';
|
||||
}
|
||||
|
||||
Node<T>* Construct(const std::vector<T>& array, long long start, long long end) {
|
||||
if (start > end)
|
||||
return nullptr;
|
||||
|
||||
size_t mid = (start + end) / 2;
|
||||
Node<T>* node = new Node<T>(array[mid]);
|
||||
|
||||
node->left_child = Construct(array, start, mid - 1);
|
||||
node->right_child = Construct(array, mid + 1, end);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private:
|
||||
Node<T>* root = nullptr;
|
||||
};
|
||||
|
||||
int main() {
|
||||
BinarySearchTree<int> bst;
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
std::vector<int> 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();
|
||||
}
|
276
5/B.cpp
Normal file
276
5/B.cpp
Normal file
|
@ -0,0 +1,276 @@
|
|||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<ll>& 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<ll>& 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);
|
||||
}
|
||||
}
|
||||
}
|
297
5/C.cpp
Normal file
297
5/C.cpp
Normal file
|
@ -0,0 +1,297 @@
|
|||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<ll>& 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<ll>& 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);
|
||||
}
|
||||
}
|
||||
}
|
301
5/D.cpp
Normal file
301
5/D.cpp
Normal file
|
@ -0,0 +1,301 @@
|
|||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<ll>& 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<ll>& 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();
|
||||
}
|
124
5/E.cpp
Normal file
124
5/E.cpp
Normal file
|
@ -0,0 +1,124 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::vector<std::string>> 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 << " ";
|
||||
}
|
||||
}
|
381
5/F.cpp
Normal file
381
5/F.cpp
Normal file
|
@ -0,0 +1,381 @@
|
|||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
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<ll>& inorder_, std::vector<ll>& 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<ll>& 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<ll>& 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<ll>* inorder;
|
||||
std::vector<ll>* 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<std::vector<ll>> values(2);
|
||||
|
||||
BinarySearchTree bst;
|
||||
|
||||
std::stack<Node*> 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;
|
||||
}
|
387
5/H.cpp
Normal file
387
5/H.cpp
Normal file
|
@ -0,0 +1,387 @@
|
|||
#include <cstddef>
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<ll>& container) {
|
||||
InorderTraversalVector(root, container);
|
||||
}
|
||||
|
||||
void InorderTraversalVector(Node* node, std::vector<ll>& 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<ll>& 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<ll>& 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<ll> nodes_1;
|
||||
std::vector<ll> 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';
|
||||
}
|
||||
}
|
||||
}
|
40
7.1/A.cpp
Normal file
40
7.1/A.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef long long ll;
|
||||
|
||||
void GrayCode(ll n) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::string> 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;
|
||||
}
|
30
7.1/B.py
Normal file
30
7.1/B.py
Normal file
|
@ -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()
|
||||
|
58
7.1/E.py
Normal file
58
7.1/E.py
Normal file
|
@ -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()
|
Loading…
Add table
Add a link
Reference in a new issue