From 482f1aee716d8cc8f6e375de53409b13d250250b Mon Sep 17 00:00:00 2001 From: igoroogle Date: Tue, 26 Dec 2017 00:03:55 +0300 Subject: [PATCH 1/2] Add files via upload --- main.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cb2501d --- /dev/null +++ b/main.cpp @@ -0,0 +1,151 @@ +#include +#include +template void Swap(T& elem1, T& elem2) { + T other = elem1; + elem1 = elem2; + elem2 = other; +} + +template class Point { + public: + T val; + Point *next; + Point(T val) { + this->val = val; + next = NULL; + } +}; + +template class List { + public: + const size_t size() { + return size_val; + } + + List() { + size_val = 0; + left = NULL; + right = NULL; + } + + ~List() { + Point *cur = left, *cur1; + while (cur != NULL) { + cur1 = cur; + cur = cur->next; + delete(cur1); + } + } + + void Add(T& val) { + if (right == NULL) { + right = new Point(val); + left = right; + size_val = 1; + } else { + right->next = new Point(val); + right = right->next; + ++size_val; + } + } + + Point* begin() const { + return left; + } + + Point* end() const { + return right; + } + + private: + Point *left, *right; + size_t size_val; +}; + +template std::ostream& operator << (std::ostream& out, const List &ans) { + auto cur = ans.begin(); + while (cur != ans.end()) { + out << cur->val << ' '; + cur = cur->next; + } + + out << ans.end()->val; + return out; +} + +template void Sort(Point *first, Point *last) { + if (first == last) + return; + + if (first->next == last) { + if (first->val > last->val) { + Swap(first->val, last->val); + } + return; + } + + size_t length_first = 1, length_second = 1; + Point *cur, *mid1, *mid2, *cur1, *cur2, *fail; + for (cur = first; cur != last; ++length_first) { + cur = cur->next; + } + + for (cur = first; length_second != length_first / 2; ++length_second) { + cur = cur->next; + } + + mid1 = cur; + mid2 = cur->next; + Sort(first, mid1); + Sort(mid2, last); + + + cur = first; + cur1 = first->next; + cur2 = mid2; + fail = mid1->next; + while ((cur1 != fail) || (cur2 != last)) { + if ((cur2 == last) || ((cur1 != fail) && (cur1->val <= cur2->val))) { + cur->next = cur1; + cur1 = cur1->next; + cur = cur->next; + } else { + cur->next = cur2; + cur2 = cur2->next; + cur = cur->next; + } + } + + cur->next = last; + cur = first; + while ((cur->next != last) && (cur->next->val < cur->val)) { + Swap(cur->next->val, cur->val); + cur = cur->next; + } + + T max_val = last->val; + for (cur = first; cur != last; cur = cur->next) { + if (cur->val > max_val) { + Swap(cur->val, max_val); + } + } + + last->val = max_val; + } + + +int main() { + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + int n, val; + List input_file; + std::cin >> n; + for (int i = 0; i < n; ++i) { + std::cin >> val; + input_file.Add(val); + } + + Sort(input_file.begin(), input_file.end()); + + std::cout << input_file << "\n"; +} From 2d5f74e256597334e596fe46aa90f684cefdd362 Mon Sep 17 00:00:00 2001 From: igoroogle Date: Sat, 27 Jan 2018 15:36:00 +0300 Subject: [PATCH 2/2] UPD --- main | 194 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/main b/main index ed1d5f6..db12369 100644 --- a/main +++ b/main @@ -1,151 +1,151 @@ -#include -#include -template void Swap(T& elem1, T& elem2) { - T other = elem1; - elem1 = elem2; - elem2 = other; -} +#include -template class Point { +template class Node { public: T val; - Point *next; - Point(T val) { + Node *next; + + Node() { + next = NULL; + } + + Node(T val) { this->val = val; next = NULL; } }; template class List { - public: - const size_t size() { - return size_val; +public: + size_t size() const { + return sizeVal; + } + + List(size_t sizeVal, Node *first, Node *last) { + this->sizeVal = sizeVal; + this->first = first; + this->last = last; } List() { - size_val = 0; - left = NULL; - right = NULL; + sizeVal = 0; + first = new Node(); + last = first; } ~List() { - Point *cur = left, *cur1; - while (cur != NULL) { - cur1 = cur; - cur = cur->next; - delete(cur1); + if (first == NULL) { + return; } - } - void Add(T& val) { - if (right == NULL) { - right = new Point(val); - left = right; - size_val = 1; - } else { - right->next = new Point(val); - right = right->next; - ++size_val; + Node *nextHead = first, *head; + while (nextHead != last) { + head = nextHead; + nextHead = nextHead->next; + delete head; } + delete(last); + } + + void Add(T val) { + last->val = val; + last->next = new Node(); + last = last->next; + ++sizeVal; + } + + void updFirst(Node* newFirst) { + first = newFirst; } - Point* begin() const { - return left; + Node* begin() const { + return first; } - Point* end() const { - return right; + Node* end() const { + return last; } private: - Point *left, *right; - size_t size_val; + Node *first, *last; + size_t sizeVal; }; -template std::ostream& operator << (std::ostream& out, const List &ans) { - auto cur = ans.begin(); - while (cur != ans.end()) { - out << cur->val << ' '; - cur = cur->next; +template std::ostream& operator << (std::ostream& out, const List &outVal) { + auto element = outVal.begin(); + while (element != outVal.end()) { + out << element->val << ' '; + element = element->next; } - out << ans.end()->val; return out; } -template void Sort(Point *first, Point *last) { - if (first == last) +template void Sort(List& sortList) { + if (sortList.size() < 2) { return; + } - if (first->next == last) { - if (first->val > last->val) { - Swap(first->val, last->val); + if (sortList.size() == 2) { + auto newFirst = sortList.begin()->next; + if (sortList.begin()->val <= newFirst->val) { + return; } + + sortList.begin()->next = sortList.end(); + newFirst->next = sortList.begin(); + sortList.updFirst(newFirst); return; } - size_t length_first = 1, length_second = 1; - Point *cur, *mid1, *mid2, *cur1, *cur2, *fail; - for (cur = first; cur != last; ++length_first) { - cur = cur->next; - } - for (cur = first; length_second != length_first / 2; ++length_second) { - cur = cur->next; - } - - mid1 = cur; - mid2 = cur->next; - Sort(first, mid1); - Sort(mid2, last); - - - cur = first; - cur1 = first->next; - cur2 = mid2; - fail = mid1->next; - while ((cur1 != fail) || (cur2 != last)) { - if ((cur2 == last) || ((cur1 != fail) && (cur1->val <= cur2->val))) { - cur->next = cur1; - cur1 = cur1->next; - cur = cur->next; - } else { - cur->next = cur2; - cur2 = cur2->next; - cur = cur->next; - } + auto midNode = sortList.begin(); + for (size_t i = 0; i < sortList.size() / 2; ++i) { + midNode = midNode->next; } - - cur->next = last; - cur = first; - while ((cur->next != last) && (cur->next->val < cur->val)) { - Swap(cur->next->val, cur->val); - cur = cur->next; + auto firstList = List(sortList.size() / 2, sortList.begin(), midNode); + auto lastList = List(sortList.size() - sortList.size() / 2, midNode, sortList.end()); + Sort(firstList); + Sort(lastList); + auto sortSize = sortList.size(); + auto headFirst = firstList.begin(); + auto headLast = lastList.begin(); + Node* head; + if (headFirst->val <= headLast->val) { + head = headFirst; + headFirst = headFirst->next; + } else { + head = headLast; + headLast = headLast->next; } - - T max_val = last->val; - for (cur = first; cur != last; cur = cur->next) { - if (cur->val > max_val) { - Swap(cur->val, max_val); + auto tail = head; + while ((headFirst != firstList.end()) || (headLast != lastList.end())) { + if (headLast == lastList.end() || (headFirst != firstList.end() && headFirst->val <= headLast->val)) { + tail->next = headFirst; + headFirst = headFirst->next; + } else { + tail->next = headLast; + headLast = headLast->next; } + tail = tail->next; } - - last->val = max_val; - } + tail->next = sortList.end(); + firstList.updFirst(NULL); + lastList.updFirst(NULL); + sortList.updFirst(head); +} int main() { - freopen("input.txt", "r", stdin); - freopen("output.txt", "w", stdout); int n, val; - List input_file; + List inputList; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> val; - input_file.Add(val); + inputList.Add(val); } - Sort(input_file.begin(), input_file.end()); + Sort(inputList); - std::cout << input_file << "\n"; + std::cout << inputList << '\n'; }