From 22032f64f08c8759a1207918b5dd48bbccc76f22 Mon Sep 17 00:00:00 2001 From: Andeo1812 Date: Wed, 6 Apr 2022 19:01:45 +0300 Subject: [PATCH] 2 yes --- CPPLINT.cfg | 8 ++++ Makefile | 2 +- README.md | 2 +- module_1/2.cpp | 105 +++++++++++++++++++++++++++++++++++++++------ module_1/Example_2 | 14 ++---- run_linters.sh | 4 +- 6 files changed, 107 insertions(+), 28 deletions(-) create mode 100644 CPPLINT.cfg diff --git a/CPPLINT.cfg b/CPPLINT.cfg new file mode 100644 index 0000000..db06183 --- /dev/null +++ b/CPPLINT.cfg @@ -0,0 +1,8 @@ +headers=h +linelength=120 +filter=-whitespace/tab +filter=-runtime/int +filter=-legal/copyright +filter=-build/include_subdir +filter=-build/include +filter=-readability/casting \ No newline at end of file diff --git a/Makefile b/Makefile index f2931c8..9a69417 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: clear build launch -TARGET = 1 +TARGET = 2 TARGET_MODULE = module_1 diff --git a/README.md b/README.md index 4c71acc..7530118 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 1 - 4 - YES -2 - 4 +2 - 4 - YES 3 - 2 diff --git a/module_1/2.cpp b/module_1/2.cpp index 729c8ff..f2756eb 100644 --- a/module_1/2.cpp +++ b/module_1/2.cpp @@ -2,31 +2,108 @@ #include #include -// Инвертируйте значение бита в числе N по его номеру K. -// Необходимо использование битовых операций. -// Использование арифметических операций запрещено. +// Дан отсортированный массив различных целых чисел A[0..n-1] и массив целых чисел B[0..m-1]. +// Для каждого элемента массива B[i] найдите минимальный индекс элемента массива A[k], ближайшего по +// значению к B[i]. n ≤ 110000, m ≤ 1000. Время работы поиска для каждого элемента B[i]: O(log(k)). -#define MAX_NUMBER (u_int32_t)(2e32 - 1) -#define MAX_BIT (u_int32_t)(32 - 1) +#define MAX_COUNT_ARRAY_FIRST 110000 +#define MAX_COUNT_ARRAY_SECOND 1000 -u_int32_t input(u_int32_t max) { - u_int32_t number = 0; +typedef struct { + size_t begin; + size_t end; +} interval_t; - std::cin >> number; +void exponential_search(const int key, const int *array, const size_t left, const size_t right, interval_t &intr) { + size_t begin = left; + size_t end = left + 1; - assert(max >= number); + while (end < right) { + if (key > array[end]) { + begin = end; - return number; + end *= 2; + } else { + intr.begin = begin; + intr.end = end; + return; + } + } + + intr.begin = begin / 2; + intr.end = right - 1; +} + +size_t binary_search(const int key, const int* array, size_t left, size_t right) { + if (key < array[left]) { + return left; + } + + if (key > array[right]) { + return right; + } + + while (left + 1 != right) { + size_t mid = (left + right) / 2; + + if (array[mid] > key) { + right = mid; + } else { + left = mid; + } + } + + if (key - array[left] > array[right] - key) { + return right; + } else { + return left; + } + +} + +int *input_array(size_t max_count, size_t &count) { + std::cin >> count; + + assert(count <= max_count); + + int *res = new int [count]; + if (!res) { + return nullptr; + } + + for (size_t i = 0; i < count; ++i) { + std::cin >> res[i]; + } + + return res; } int main() { - u_int32_t number = input(MAX_NUMBER); + size_t length_a = 0; + int *A = input_array(MAX_COUNT_ARRAY_FIRST, length_a); + + size_t length_b = 0; + int *B = input_array(MAX_COUNT_ARRAY_SECOND, length_b); + + interval_t *intr = new interval_t; + if (!intr) { + return -1; + } + + for (size_t i = 0; i < length_b; ++i) { + exponential_search(B[i], A, 0, length_a, *intr); + + size_t res = binary_search(B[i], A, intr->begin, intr->end); + + std::cout << res << " "; + } + + delete intr; - u_int32_t bit = input(MAX_BIT); + delete[] A; - u_int32_t res = number ^ (1 << bit); + delete[] B; - std::cout << res << std::endl; return EXIT_SUCCESS; } diff --git a/module_1/Example_2 b/module_1/Example_2 index 1b29220..dc08554 100644 --- a/module_1/Example_2 +++ b/module_1/Example_2 @@ -1,10 +1,4 @@ -3 0 0 2 -10 20 30 -3 -9 15 35 - - -3 0 0 0 2 -10 20 30 -4 -8 9 10 32 \ No newline at end of file +10 +10 20 30 40 50 60 100 200 4400 5000 +10 +0 2 10 19 21 151 220 4000 4999 5000 diff --git a/run_linters.sh b/run_linters.sh index a1e5a71..4f9cdf4 100755 --- a/run_linters.sh +++ b/run_linters.sh @@ -25,9 +25,9 @@ print_header "RUN cppcheck" check_log "cppcheck module_1 --enable=all --inconclusive --error-exitcode=1 -I module_1 --suppress=missingIncludeSystem" "\(information\)" print_header "RUN clang-tidy" -check_log "clang-tidy module_1/* -warnings-as-errors=* -extra-arg=-std=c99 -- " "Error (?:reading|while processing)" +check_log "clang-tidy module_1/*.cpp -warnings-as-errors=* -extra-arg=-std=c99 -- " "Error (?:reading|while processing)" print_header "RUN cpplint" -check_log "cpplint --extensions=c module_1/* " "Can't open for reading" +check_log "cpplint --extensions=c module_1/*.cpp " "Can't open for reading" print_header "SUCCESS" \ No newline at end of file