From 64e55d4c4b2026cffece2b4e450b95225f07bb11 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 10 Oct 2024 21:15:37 +0800 Subject: [PATCH] :white_check_mark: P7910 76pts (TLE) --- CMakeLists.txt | 3 ++- eating-snake/main.cc | 13 ++++++------- insert-sort/data.in | 6 ++++++ insert-sort/main.cc | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 insert-sort/data.in create mode 100644 insert-sort/main.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ba0088..950f7a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,4 +23,5 @@ add_executable(Playground take-most-space-element/main.cc walking-in-maze/main.cc network-connection/main.cpp - eating-snake/main.cc) + eating-snake/main.cc + insert-sort/main.cc) diff --git a/eating-snake/main.cc b/eating-snake/main.cc index 3a49cba..e9cfd5d 100644 --- a/eating-snake/main.cc +++ b/eating-snake/main.cc @@ -4,7 +4,7 @@ using namespace std; -//#define ENABLE_LOG +#define ENABLE_LOG struct snake { int id; @@ -29,8 +29,8 @@ bool sort_by_id(snake a, snake b) { return b.id > a.id; } -void eat(vector &vec, bool skip_sort = false) { - if (vec.size() < 2) return; +bool eat(vector &vec, bool skip_sort = false, int initial_id = 0) { + if (vec.size() < 2) return true; if (!skip_sort) { sort(vec.begin(), vec.end()); } @@ -42,8 +42,8 @@ void eat(vector &vec, bool skip_sort = false) { } #endif - if (!(vec[0] >= vec[vec.size() - 1])) { - return; + if (!(vec[initial_id] >= vec[vec.size() - 1])) { + return true; } // If this snake ate the last snake @@ -53,7 +53,7 @@ void eat(vector &vec, bool skip_sort = false) { printf("%d gonna to eat %d\n", curr.id, last.id); #endif vec.pop_back(); - vec[0].power -= last.power; + vec[initial_id].power -= last.power; sort(vec.begin(), vec.end()); if (vec[0].id != curr.id) { bool will_be_eaten = false; @@ -86,7 +86,6 @@ void eat(vector &vec, bool skip_sort = false) { } } vec.push_back(last); - return; } } diff --git a/insert-sort/data.in b/insert-sort/data.in new file mode 100644 index 0000000..22e94f6 --- /dev/null +++ b/insert-sort/data.in @@ -0,0 +1,6 @@ +3 4 +3 2 1 +2 3 +1 3 2 +2 2 +2 3 diff --git a/insert-sort/main.cc b/insert-sort/main.cc new file mode 100644 index 0000000..f4ac7f1 --- /dev/null +++ b/insert-sort/main.cc @@ -0,0 +1,38 @@ +// The question want me to impl a sort by my self +// But absolutely not + +#include + +using namespace std; + +int main() { + int n, Q; + cin >> n >> Q; + int arr[n + 5]; + + for (int i = 1; i <= n; i++) { + cin >> arr[i]; + } + + for (int i = 0; i < Q; i++) { + int op; + cin >> op; + if (op == 1) { + int x, v; + cin >> x >> v; + arr[x] = v; + } else { + int xn; + cin >> xn; + int it = arr[xn]; + int pos = n; + for (int j = 1; j < xn; j++) { + if (arr[j] > it) pos--; + } + for (int j = xn + 1; j <= n; j++) { + if (arr[j] >= it) pos--; + } + cout << pos << endl; + } + } +} \ No newline at end of file