diff --git a/.editorconfig b/.editorconfig
index 81fa4d12..44597e6a 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -2,10 +2,8 @@ root = true
[*]
charset = utf-8
+indent_size = 4
+end_of_line = lf
indent_style = space
insert_final_newline = true
-
-[*.{cpp,hpp}]
-end_of_line = lf
-indent_size = 4
trim_trailing_whitespace = true
diff --git a/.gitattributes b/.gitattributes
index 607cda74..29b9e521 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -14,6 +14,8 @@
*.template text diff=text
*.in text eol=lf
*.out text eol=lf
+*.data.in text eol=lf
+*.data.out text eol=lf
# scripts
*.sh text eol=lf
diff --git a/LICENSE_APACHE_2_0.md b/LICENSE_APACHE_2_0.md
new file mode 100644
index 00000000..261eeb9e
--- /dev/null
+++ b/LICENSE_APACHE_2_0.md
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/README.md b/README.md
index 3358d648..9f511da1 100644
--- a/README.md
+++ b/README.md
@@ -116,7 +116,7 @@ git clone https://github.com/${YOUE_GITHUB_USER_NAME}/algorithm-template.git
+ 可选项:
+ 使用脚本产生自定义的文件(适合source.zip或者有bonuslab):
- 使用命令行, 进入`./script`下, 编辑`file_template`的`labs` & `problem_orders`,
+ 使用命令行, 进入`./script`下, 编辑`file_template`的`labs` & `problem_orders`,
`python3 ./file_template.py`, 出现`produce files finish`提示, 即为创建成功.
(back to top)
@@ -129,9 +129,9 @@ git clone https://github.com/${YOUE_GITHUB_USER_NAME}/algorithm-template.git
`lab${order}_${ques_Order}`为对应题号, 比如`lab07_01`对应lab_07的C1题.
-+ `CS203_lab07_01`将调用`lab_07\lab_07_C1\lab07_C1.cpp`, 为将要提交的源文件.
-+ `CS203_lab07_01_test`将调用`lab_07\lab_07_C1\lab07_C1_test.cpp`, 对其进行测试.
-+ `lab_*\lab_*_*\lab_*_*_test.cpp`目的为方便测试, 同时便于分享测试用例.
++ `CS203_lab07_01`将调用`lab_07\lab_07_C1\main.cpp`, 为将要提交的源文件.
++ `CS203_lab07_01_test`将调用`lab_07\lab_07_C1\test.cpp`, 对其进行测试.
++ `lab_*\lab_*_*\test.cpp`目的为方便测试, 同时便于分享测试用例.
(back to top)
@@ -153,7 +153,7 @@ git clone https://github.com/${YOUE_GITHUB_USER_NAME}/algorithm-template.git
+ 在本repo, 使用`Catch2`测试框架.
+ 比如, 我们有四组数据, 第一组, 第二组测试边界值, 第三组使用随机数测试对偶性与正确性, 第四组测试几个手动的随机值.
- + 参见[test_for_lab00_A](./lab_00/lab_00_A/lab_00_A_test.cpp).
+ + 参见[test_for_lab00_A](./lab_00/lab_00_A/test.cpp).
+ 这样一来, 我们只需要每次修改完主文件之后, run `algorithm-template_test`, 对其进行调用, 就能验证其在所有的测试用例上的正确性.
### 多个输出值的检查:`Catch::Matchers`
@@ -162,7 +162,7 @@ git clone https://github.com/${YOUE_GITHUB_USER_NAME}/algorithm-template.git
举例:[Crzay Plan](https://acm.sustech.edu.cn/onlinejudge/problem.php?id=1250), 输入可能有1.1*10^6个.
-这种情况下对这么多值进行直接的观察就很难, 所以我们预先将期望的值直接写在测试文件里, 用Catch2内置的Matcher比较(见[test_for_lab00_B](./lab_00/lab_00_B/lab_00_B_test.cpp)的`CHECK_THAT()`部分.)
+这种情况下对这么多值进行直接的观察就很难, 所以我们预先将期望的值直接写在测试文件里, 用Catch2内置的Matcher比较(见[test_for_lab00_B](./lab_00/lab_00_B/test.cpp)的`CHECK_THAT()`部分.)
PS: 当然, 这种情况也只适用于规模比较小的情况, 规模再大的话, 直接由人手动写在测试文件里也太占空间了.
@@ -192,7 +192,7 @@ TEST_CASE("test case 1", "[test 00 C]") {
+ test case with tuple 则最优雅, 修改起来的难度最小.
+ test case with sequence 比tuple更优雅, 输入, 输出全为自动产生.
-PS: 此处注意, 引用文件的相对路径, 不是直接的`test/lab_00/lab_00_C/resource/01.data.in`,
+PS: 此处注意, 引用文件的相对路径, 不是直接的`test/lab_00/lab_00_C/resource/01.data.in`,
而是编译出的文件相对于测试数据的相对路径.
@@ -219,7 +219,7 @@ PS: 此处注意, 引用文件的相对路径, 不是直接的`test/lab_00/lab_0
PS: 至于比较文件之间的差异, 可以使用内置的`compareFiles(string path1, string path2)`函数进行比较.
-参考[文本比对_test_case_2](./lab_00/lab_00_D/lab_00_D_test.cpp)
+参考[文本比对_test_case_2](./lab_00/lab_00_D/test.cpp)
## Details
@@ -252,11 +252,11 @@ DSAA既然内含Data structure, 就势必涉及到类似Node, Tree, Graph等等
### 如何手动开优化
1. 将[magic_optimize](./include/magic_macro/magic_macro.hpp)内的内容粘贴到代码最上方.
-2. 关闭同步,
+2. 关闭同步,
``` cpp
static const auto faster_streams = [] {
- srand(time(nullptr));
+ srand(time(nullptr));
// use time to init the random seed
std::ios::sync_with_stdio(false);
std::istream::sync_with_stdio(false);
@@ -329,9 +329,13 @@ Don't forget to give the project a star! Thanks again!
## License
+### MIT LICENSE
+
+根目录下, 使用脚本生成的代码均采用`Apache 2.0`协议, 严谨而宽松.
+
### AGPLv3.0+ LICENSE
-绝大多数代码(`*.cpp`, `*.hpp`, etc)基于 AGPLv3.0+协议: 限制最强的主流开源协议
+非模板部分代码(`*.cpp`, `*.hpp`, etc)基于 AGPLv3.0+协议: 限制最强的主流开源协议
+ 由于本仓库设计只包括"上交"源码这一种场景, 因此实际上不存在二进制分发以及被云服务使用这种场景.
+ 具体内容请看[`LICENSE_AGPL_V3_0.md`](./LICENSE_AGPL_V3_0.md)
diff --git a/algorithm/2021F/lab_02/CMakeLists.txt b/algorithm/2021F/lab_02/CMakeLists.txt
index e558ffef..56aa09f6 100644
--- a/algorithm/2021F/lab_02/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/CMakeLists.txt
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-set(dependencies A B C D E F)
+set(dependencies A B C D E F G)
foreach (elementName IN LISTS dependencies)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${elementName})
endforeach ()
diff --git a/algorithm/2021F/lab_02/README.md b/algorithm/2021F/lab_02/README.md
index d82c747e..f5615ec9 100644
--- a/algorithm/2021F/lab_02/README.md
+++ b/algorithm/2021F/lab_02/README.md
@@ -2,7 +2,36 @@
SPDX-License-Identifier: CC-BY-NC-SA-4.0
---
-# algorithm_2021F_lab02
+# Contest1093 - CS203 2021 Fall Lab 02 Complexity + Binary Search
-1. 为了方便为单独的题目撰写README, 将每个题目的resource,source,test文件放在单独的一个文件夹内.
+> cid: 1094
+Welcome to CS203 2021 Fall Lab 02! Enjoy this Lab!
+
+There are six problems for you to solve.
+
+Score:
+
++ A: 10
++ B: 15/10
++ C: 15/10
++ D: 20/15
++ E: 20/15
++ F: 20/20
++ G: undefined/20
+
+Reading the samples and hints carefully can help you understand the problem.
+
+## Problems
+
+| Problem |problem id|
+|---:|---:|
+| A | 1421 |
+| B | 1422 |
+| C | 1423 |
+| D | 1424 |
+| E | 1425 |
+| F | 1426 |
+| G | 1427 |
+
+A, B, E被复用
diff --git a/algorithm/2021F/lab_02/cs203.submit.csv b/algorithm/2021F/lab_02/cs203.submit.csv
new file mode 100644
index 00000000..ac690828
--- /dev/null
+++ b/algorithm/2021F/lab_02/cs203.submit.csv
@@ -0,0 +1,8 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 280, 1, 232, 94, 6, 7, 86, 60, 766, 11, 114, 641
+B, 259, 3, 332, 146, 4, 9, 33, 786, 4, 130, 652
+C, 257, 290, 62, 12, 34, 655, 3, 160, 492
+D, 272, 621, 368, 12, 11, 88, 51, 1423, 16, 364, 1043
+E, 224, 809, 761, 9, 19, 183, 80, 2085, 6, 329, 1750
+F, 205, 762, 335, 55, 12, 274, 60, 1703, 18, 276, 1409
+Total, 1497, 4, 3046, 1766, 82, 53, 652, 318, 7418, 58, 1373, 5987
\ No newline at end of file
diff --git a/algorithm/2021F/lab_02/cs217.submit.csv b/algorithm/2021F/lab_02/cs217.submit.csv
new file mode 100644
index 00000000..17e4a49a
--- /dev/null
+++ b/algorithm/2021F/lab_02/cs217.submit.csv
@@ -0,0 +1,9 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 28, 16, 12, 1, 6, 2, 65, 21, 44
+B, 27, 9, 6, 1, 43, 16, 27
+C, 25, 9, 2, 36, 15, 21
+D, 32, 30, 21, 1, 4, 2, 90, 40, 50
+E, 27, 17, 37, 1, 3, 6, 91, 31, 60
+F, 32, 93, 37, 9, 2, 17, 14, 204, 2, 89, 113
+G, 27, 62, 40, 1, 6, 5, 141, 38, 103
+Total, 198, 236, 153, 11, 4, 37, 31, 670, 2, 250, 418
\ No newline at end of file
diff --git a/algorithm/2021F/lab_02/lab_02_A/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_A/CMakeLists.txt
index 354751e1..40897f09 100644
--- a/algorithm/2021F/lab_02/lab_02_A/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_A/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_A/README.md b/algorithm/2021F/lab_02/lab_02_A/README.md
new file mode 100644
index 00000000..29acfc4c
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_A/README.md
@@ -0,0 +1,64 @@
+ # Problem A (inferred)
+
+## Description
+
+Given a set of integers and several query values, determine for each query whether the value exists in the set.
+
+Output "YES" if it exists, otherwise output "NO".
+
+## Input
+
+- Line 1: integer n, the number of elements in the set.
+- Line 2: n integers (space-separated), the elements of the set.
+- Next: integer T, the number of queries.
+- Next T integers (whitespace separated or one per line): the queries.
+
+## Output
+
+Output T lines. For each query print either:
+
+- `YES` if the queried value is in the set
+- `NO` otherwise
+
+## Sample
+
+### Sample Input
+
+```text
+4
+1 2 3 999999
+2
+99
+3
+```
+
+### Sample Output
+
+```text
+NO
+YES
+```
+
+## Implementation notes
+
+- The provided implementation stores elements in an `unordered_set` and answers each query by O(1) average-time hash lookup.
+
+- Current code uses 32-bit signed integers; if input values may exceed int32 range, switch to 64-bit types.
+
+## 复用信息
+
++ Contest 1162:CS203 2024 Fall Lab 1
++ Contest 1093:CS203 2021 Fall Lab 02 Complexity + Binary Search
++ Contest 1094:CS217 2021 Fall Lab 02 Complexity + Binary Search
++ Contest 1135:CS203 2023 Fall Lab 1 Complexity + Binary Search
++ Contest 1186:CS203 2025 Fall Lab 1 Complexity + Binary Search
+
+# Solution / 思路解析
+
+- 思路概述:把给定集合中的元素放入一个哈希集合(`unordered_set`),对每个查询直接判断是否存在,从而以接近 O(1) 的均摊时间回答。
+- 算法要点:读取 n 个元素并插入 `unordered_set`;然后对每个查询,在哈希集合中查找并输出 `YES`/`NO`。
+- 复杂度:构建集合 O(n)(均摊),每次查询 O(1)(均摊),总时间 O(n + T)。空间 O(n)。
+- 边界与实现注意事项:
+ - 输入可能包含重复元素(哈希集合会自动去重),但查询逻辑不受影响。
+ - 若数据可能超出 32 位范围,则把 `int32_t` 换成 `int64_t`。
+ - 对于极端输入(非常大的 n),确保 reserve 集合以减少 rehash 开销(可选优化)。
diff --git a/algorithm/2021F/lab_02/lab_02_A/lab_02_A.cpp b/algorithm/2021F/lab_02/lab_02_A/main.cpp
similarity index 100%
rename from algorithm/2021F/lab_02/lab_02_A/lab_02_A.cpp
rename to algorithm/2021F/lab_02/lab_02_A/main.cpp
diff --git a/algorithm/2021F/lab_02/lab_02_A/lab_02_A_test.cpp b/algorithm/2021F/lab_02/lab_02_A/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_02/lab_02_A/lab_02_A_test.cpp
rename to algorithm/2021F/lab_02/lab_02_A/test.cpp
index d64aaa4d..1b9f8c4a 100644
--- a/algorithm/2021F/lab_02/lab_02_A/lab_02_A_test.cpp
+++ b/algorithm/2021F/lab_02/lab_02_A/test.cpp
@@ -8,7 +8,7 @@
#include
#include
-#include "lab_02_A.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_A/resource/"; }
diff --git a/algorithm/2021F/lab_02/lab_02_B/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_B/CMakeLists.txt
index c490830a..dac0839a 100644
--- a/algorithm/2021F/lab_02/lab_02_B/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_B/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_B/README.md b/algorithm/2021F/lab_02/lab_02_B/README.md
new file mode 100644
index 00000000..4e27ccc9
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_B/README.md
@@ -0,0 +1,55 @@
+# Problem B (inferred)
+
+## Description
+
+For each input integer x, compute and print the value (x * (x + 1) * (x + 2)) / 6.
+
+This equals the binomial coefficient C(x+2, 3).
+
+## Input
+
+- Line 1: integer T, the number of test cases.
+- Next T lines (or whitespace separated values): each a single integer x.
+
+## Output
+
+Output T lines. The i-th line contains the integer result of `(x * (x + 1) * (x + 2)) / 6` for the i-th input x.
+
+## Sample
+
+### Sample Input
+
+```text
+2
+1
+2
+```
+
+### Sample Output
+
+```text
+1
+4
+```
+
+## Implementation notes
+
+- The implementation uses 64-bit integer arithmetic (`int64_t`) to reduce overflow risk. For very large x values consider using safe multiplication order or big integers.
+
+- Mathematically, the expression equals C(x+2, 3), which may help reasoning about constraints and correctness.
+
+## 复用信息
+
++ Contest 1186:CS203 2025 Fall Lab 1 Complexity + Binary Search
++ Contest 1093:CS203 2021 Fall Lab 02 Complexity + Binary Search
++ Contest 1094:CS217 2021 Fall Lab 02 Complexity + Binary Search
++ Contest 1135:CS203 2023 Fall Lab 1 Complexity + Binary Search
+
+## Solution / 思路解析
+
+- 思路概述:数学上该式子等价于组合数 $C(x+2,3)$。程序直接按代数表达式 $(x\times(x+1)\times(x+2))/6$ 计算。
+- 实现要点:使用 `int64_t` 来保存中间乘积以避免溢出;若可能溢出 64 位,应使用大整数或按分步除法来减少中间值(例如先除以 2 或 3,当能整除时)。
+- 复杂度:每个测试用例的计算为 O(1),总时间 O(T)。空间 O(1)。
+- 边界与注意事项:
+ - 输入 x 若为负数(题面通常为非负),公式仍在整数意义下成立,但需确认题目约束。
+ - 对于非常大的 x(接近 1e6~1e9),中间乘积可能超过 64 位,需按题面约束决定是否要更高精度类型或安全乘法顺序。
diff --git a/algorithm/2021F/lab_02/lab_02_B/lab_02_B.cpp b/algorithm/2021F/lab_02/lab_02_B/main.cpp
similarity index 100%
rename from algorithm/2021F/lab_02/lab_02_B/lab_02_B.cpp
rename to algorithm/2021F/lab_02/lab_02_B/main.cpp
diff --git a/algorithm/2021F/lab_02/lab_02_B/lab_02_B_test.cpp b/algorithm/2021F/lab_02/lab_02_B/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_02/lab_02_B/lab_02_B_test.cpp
rename to algorithm/2021F/lab_02/lab_02_B/test.cpp
index 3ec7c3b6..8d3ce997 100644
--- a/algorithm/2021F/lab_02/lab_02_B/lab_02_B_test.cpp
+++ b/algorithm/2021F/lab_02/lab_02_B/test.cpp
@@ -8,7 +8,7 @@
#include
#include
-#include "lab_02_B.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_B/resource/"; }
diff --git a/algorithm/2021F/lab_02/lab_02_C/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_C/CMakeLists.txt
index 5de3f24f..f07d2e4c 100644
--- a/algorithm/2021F/lab_02/lab_02_C/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_C/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_C/README.md b/algorithm/2021F/lab_02/lab_02_C/README.md
new file mode 100644
index 00000000..b489481b
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_C/README.md
@@ -0,0 +1,46 @@
+# Problem C (Order 1423)
+
+## Description
+
+Given $F(x) = x e^{x/20} - b$, $x \in \mathbb{R}$. Here $e$ is Euler's number (approximately $2.718281828459\ldots$), and $b$ is a given integer.
+
+Please find a root of $F(x)$.
+
+## Input
+
+The 1st line is a positive integer $T$ ($1 \le T \le 1000$) which is the number of test cases.
+
+Then $T$ lines follow. Each line has an integer $b$ ($1 \le b \le 10^8$) for a test case.
+
+## Output
+
+Output $T$ lines.
+
+Each line contains a number $ans$ (accurate up to 10 decimal places), representing the approximate zero point of $F(x)$ and $|F(ans)| < 0.01$.
+
+## Sample Input
+
+```text
+2
+1
+20
+```
+
+## Sample Output
+
+```text
+0.9448242188
+11.3433837891
+```
+
+## Hint
+
+This problem uses special judge, so the sample output is not the only answer.
+
+## Solution / 思路解析
+
+- 思路概述:要解方程 $F(x)=x e^{x/20}-b=0$,可以对 $x$ 使用二分查找或数值方法(如二分/牛顿法)。题目实现采用对 $x$ 在合理区间做二分搜索,直到函数值接近 0(满足精度要求)。
+- 关键细节:给定 $b$ 不同,其对应根 $x$ 在不同范围。实现中选取了固定的左右边界(例如 $[0.9, 258]$ 覆盖题目约束),然后在该区间内通过计算 $f(x)=x e^{x/20}-b$ 并根据符号向左或向右收缩区间,即典型的二分求根法。
+- 精度与停止条件:当 $|f(x)| < \varepsilon$(实现中取 pricesion = 1e-4)时返回当前近似值;输出时保留 10 位小数。
+- 复杂度:对每个测试用例,二分迭代次数为 O(log((R-L)/eps)),每步计算需要一次指数运算,故时间复杂度可认为是常数乘以二分迭代次数。
+- 注意事项:选择区间时要确保包含根;对边界和极端 b 的处理需验证区间是否足够大。
diff --git a/algorithm/2021F/lab_02/lab_02_C/lab_02_C.cpp b/algorithm/2021F/lab_02/lab_02_C/main.cpp
similarity index 100%
rename from algorithm/2021F/lab_02/lab_02_C/lab_02_C.cpp
rename to algorithm/2021F/lab_02/lab_02_C/main.cpp
diff --git a/algorithm/2021F/lab_02/lab_02_C/lab_02_C_test.cpp b/algorithm/2021F/lab_02/lab_02_C/test.cpp
similarity index 93%
rename from algorithm/2021F/lab_02/lab_02_C/lab_02_C_test.cpp
rename to algorithm/2021F/lab_02/lab_02_C/test.cpp
index 3d910870..d727db30 100644
--- a/algorithm/2021F/lab_02/lab_02_C/lab_02_C_test.cpp
+++ b/algorithm/2021F/lab_02/lab_02_C/test.cpp
@@ -7,8 +7,7 @@
#include
#include
#include
-
-#include "lab_02_C.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_C/resource/"; }
@@ -20,12 +19,9 @@ using std::tie;
using std::cin;
using std::cout;
using std::tuple;
-using std::vector;
-
-using Catch::Matchers::Equals;
-using Catch::Matchers::UnorderedEquals;
-using Catch::Matchers::Contains;
-
+using std::vector
+ ;
+// 用wolframalpha 计算的 xe^(x/20)= b
TEST_CASE("test case 1", "[test 02 C]") {
const Catch::Approx target = Catch::Approx(0.9534862518).epsilon(0.01);
CHECK(cal_detail(1) == target);
diff --git a/algorithm/2021F/lab_02/lab_02_D/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_D/CMakeLists.txt
index dce330ce..564aeae5 100644
--- a/algorithm/2021F/lab_02/lab_02_D/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_D/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_D/README.md b/algorithm/2021F/lab_02/lab_02_D/README.md
new file mode 100644
index 00000000..b19fb7fe
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_D/README.md
@@ -0,0 +1,48 @@
+# Problem D (Order 1424)
+
+## Description
+
+Given a nondecreasing sequence $a$ of $n$ integers $a_1, a_2, \dots, a_n$.
+
+Please find the number of pairs of indices $i, j$ (with $i < j$) such that $a_i + a_j$ is a power of two.
+
+## Input
+
+The 1st line is a positive integer $n$ ($1 \le n \le 100000$).
+
+The 2nd line contains $n$ integers: $a_1, a_2, \dots, a_n$.
+
+For each $a_i$ ($1 \le a_i \le 10^9$).
+
+## Output
+
+Print the number of pairs of indices $i, j$ ($i < j$) such that $a_i + a_j$ is a power of two.
+
+## Sample Input
+
+```text
+4
+1 2 3 7
+```
+
+## Sample Output
+
+```text
+2
+```
+
+## Hint
+
+The corresponding solutions to the sample are: $1 + 3 = 2^2$; $1 + 7 = 2^3$.
+
+## Solution / 思路解析
+
+- 思路概述:题目要求统计下标对 $(i,j)$,使得 $i
#include
-#include "lab_02_D.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_D/resource/"; }
diff --git a/algorithm/2021F/lab_02/lab_02_E/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_E/CMakeLists.txt
index c43d3c98..f6252ecc 100644
--- a/algorithm/2021F/lab_02/lab_02_E/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_E/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_E/README.md b/algorithm/2021F/lab_02/lab_02_E/README.md
new file mode 100644
index 00000000..49476507
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_E/README.md
@@ -0,0 +1,67 @@
+# lab02-e
+
+> cid: 1094, pid: 4
+
+## Description
+
+Given two nondecreasing sequences $a$ and $b$, and their length are both $n$.
+
+What's the median after combining the subarray $a[l..r]$ and subarray $b[l..r]$?
+
+Subarray $a[l..r]$ is a sub-array of $a$, it includes $a_l, a_{l+1}, \dots, a_r$ for $1\le l\le r\le n$, its length is $r-l+1$.
+
+You'd like to determine the median of this set of $2k$ (where $k=r-l+1$) values, which we define here to be the $k$-th smallest value.
+
+For example: median([1,2,3,4]) = 2.
+
+### Input
+
+The first line contains two positive integers $n$ ($1\le n \le 100000$) and $T$ ($1\le T \le 100000$) which is the number of testcases.
+
+The second line contains $n$ integers: $a_1, a_2, \dots, a_n$. For each $a_i$ ($0\le a_i \le 10^9$).
+
+The third line contains $n$ integers: $b_1, b_2, \dots, b_n$. For each $b_i$ ($0\le b_i \le 10^9$).
+
+Then $T$ lines follow. Each line contains two integers $l$ and $r$ ($1\le l \le r \le n$) for a test case.
+
+### Output
+
+Output $T$ lines. Each line contains an integer $ans$, the median after combining the subarray $a[l..r]$ and subarray $b[l..r]$.
+
+### Sample Input
+
+``` log
+5 2
+1 3 5 7 9
+2 3 4 5 6
+5 5
+1 5
+```
+
+### Sample Output
+
+``` log
+6
+4
+```
+
+### HINT
+
+The corresponding solutions to the sample are:
+
+1) For query $(5,5)$: $a_5=9$, $b_5=6$, after combining is [9,6], the median is 6.
+
+2) For query $(1,5)$: Combine $a$ and $b$ then sort it -> [1,2,3,3,4,5,5,6,7,9], the median (5th smallest) is 4.
+
+## 复用信息
+
+Contest 1093:CS203 2021 Fall Lab 02 Complexity + Binary Search
+Contest 1094:CS217 2021 Fall Lab 02 Complexity + Binary Search
+Contest 1162:CS203 2024 Fall Lab 1
+
+## Algorithm Analysis (实现说明)
+
+- 思路: 对于每个查询 [l,r],需要找到合并后长度为 2k 的数组的第 k 小元素。由于两个子数组 a[l..r] 和 b[l..r] 的长度相等(均为 k),可以使用二分在取自 a 的元素个数 i 的范围 [0,k] 上搜索。对于某个 i,令 j=k-i,检查 a[aL+i-1] 与 b[bL+j-1] 的相对大小关系,调整二分边界直至找到满足划分的 i。返回两边边界的较大值即为第 k 小。
+- 实现要点: 主函数将输入转换为 0-based 索引, 对每个查询调用 kth_equal_len(aL,bL,k)。该函数对 i 做二分搜索,并在边界条件(i==0 或 j==0)处理访问。
+- 复杂度: 每次查询时间复杂度为 O(log k),总时间 O(T log n)。预处理/空间为 O(n)。
+- 边界情况: k=1 时直接比较两个元素;数组中存在重复值也能处理;所有索引访问都作好边界检查以避免越界。
diff --git a/algorithm/2021F/lab_02/lab_02_E/main.cpp b/algorithm/2021F/lab_02/lab_02_E/main.cpp
new file mode 100644
index 00000000..a5b01e8e
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_E/main.cpp
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+//@Tag TODO
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#ifndef ALGORITHM_TEST_MACRO
+#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
+#pragma GCC optimize("inline-small-functions")
+#pragma GCC optimize("-finline-small-functions")
+#pragma GCC target("tune=native")
+#else
+namespace lab_02_E{
+#endif
+
+using std::cin;
+using std::tie;
+using std::cout;
+using std::list;
+using std::sort;
+using std::array;
+using std::deque;
+using std::queue;
+using std::stack;
+using std::tuple;
+using std::string;
+using std::vector;
+using std::unordered_map;
+using std::unordered_set;
+using std::priority_queue;
+static constexpr const char end{'\n'};
+
+using num_t = int32_t;
+using input_type = tuple, vector, vector>>;
+using output_type = vector;
+
+inline input_type read();
+
+output_type cal(const input_type &data);
+
+void output(const output_type &data);
+
+int main() {
+ auto input_data = read();
+ auto output_data = cal(input_data);
+ output(output_data);
+ return 0;
+}
+
+inline input_type read() {
+ num_t n{0}, T{0};
+ std::cin >> n >> T;
+ vector A(n, -1), B(n, -1);
+ for (int32_t i{0}; i < n; i++) {
+ std::cin >> A[i];
+ }
+ for (int32_t i{0}; i < n; i++) {
+ std::cin >> B[i];
+ }
+ num_t tempa, tempb;
+ vector> pairs;
+ for (int32_t i{0}; i < T; ++i) {
+ std::cin >> tempa >> tempb;
+ pairs.emplace_back(tempa, tempb);
+ }
+ return std::make_tuple(A, B, pairs);
+}
+
+output_type cal(const input_type &data) {
+ vector A, B;
+ vector> pairs;
+ tie(A, B, pairs) = data;
+ const int q = static_cast(pairs.size());
+ vector ans;
+ ans.reserve(q);
+
+ auto kth_equal_len = [&](int aL, int bL, int k) -> num_t {
+ // find k-th smallest (1-indexed) among A[aL..aL+k-1] and B[bL..bL+k-1]
+ int l = 0, r = k;
+ while (l <= r) {
+ int i = (l + r) >> 1; // take i from A
+ int j = k - i; // take j from B
+ // check boundaries before accessing
+ if (i > 0 && j < k && A[aL + i - 1] > B[bL + j]) {
+ r = i - 1;
+ } else if (j > 0 && i < k && B[bL + j - 1] > A[aL + i]) {
+ l = i + 1;
+ } else {
+ if (i == 0) return B[bL + j - 1];
+ if (j == 0) return A[aL + i - 1];
+ return std::max(A[aL + i - 1], B[bL + j - 1]);
+ }
+ }
+ return -1; // should not reach here
+ };
+
+ for (const auto &pr: pairs) {
+ int lq = static_cast(pr.first);
+ int rq = static_cast(pr.second);
+ // convert to 0-based
+ int aL = lq - 1;
+ int bL = lq - 1;
+ int k = rq - lq + 1;
+ ans.push_back(kth_equal_len(aL, bL, k));
+ }
+ return ans;
+}
+
+void output(const output_type &data) {
+ for (auto &&dat: data) {
+ cout << dat << end;
+ }
+}
+
+static const auto faster_streams = [] {
+ srand(time(nullptr));
+ // use time to init the random seed
+
+ std::ios::sync_with_stdio(false);
+ std::istream::sync_with_stdio(false);
+ std::ostream::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cout.tie(nullptr);
+ // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
+ return 0;
+}();
+#ifdef ALGORITHM_TEST_MACRO
+}
+#endif
diff --git a/algorithm/2021F/lab_02/lab_02_E/lab_02_E_test.cpp b/algorithm/2021F/lab_02/lab_02_E/test.cpp
similarity index 96%
rename from algorithm/2021F/lab_02/lab_02_E/lab_02_E_test.cpp
rename to algorithm/2021F/lab_02/lab_02_E/test.cpp
index 0c9ba9af..56132088 100644
--- a/algorithm/2021F/lab_02/lab_02_E/lab_02_E_test.cpp
+++ b/algorithm/2021F/lab_02/lab_02_E/test.cpp
@@ -7,8 +7,7 @@
#include
#include
#include
-
-#include "lab_02_E.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_E/resource/"; }
@@ -32,7 +31,7 @@ TEST_CASE("test case 1", "[test 02 E]") {
const vector> pairs{{5, 5},
{1, 5}};
const auto output_data = cal(std::make_tuple(A, B, pairs));
- CHECK_THAT(output_data, Equals({6, 4}));
+ CHECK_THAT(output_data, Equals({9, 5}));
}
TEST_CASE("test case with sequence", "[test 02 E]") {
diff --git a/algorithm/2021F/lab_02/lab_02_F/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_F/CMakeLists.txt
index 7bd14a82..3bd8bb2b 100644
--- a/algorithm/2021F/lab_02/lab_02_F/CMakeLists.txt
+++ b/algorithm/2021F/lab_02/lab_02_F/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_F/README.md b/algorithm/2021F/lab_02/lab_02_F/README.md
new file mode 100644
index 00000000..49005528
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/README.md
@@ -0,0 +1,77 @@
+# Problem F (Order 1426)
+
+## Description
+
+Given an $N \times N$ matrix $A$, whose element in the $i$-th row and $j$-th column $A[i][j]$ equals
+
+$$
+A[i][j] = i^2 + 12345 \times i + j^2 - 12345 \times j + i \times j
+$$
+
+Please find the $M$-th smallest element in the matrix.
+
+
+## Input
+
+The 1st line is a positive integer $T$ ($1 \le T \le 10$) which is the number of test cases.
+
+Then $T$ lines follow.
+
+Each line has two integers $N$ ($1 \le N \le 50000$) and $M$ ($1 \le M \le N \times N$) for a test case.
+
+## Output
+
+Output $T$ lines.
+
+Each line has an integer $ans$, the $M$-th smallest element in the matrix.
+
+## Sample Input
+
+```text
+2
+1 1
+2 1
+```
+
+## Sample Output
+
+```text
+3
+-12338
+```
+
+## Hint
+
+The corresponding solutions to the sample are:
+
+(1)
+
+$$
+A[1][1] = 1^2 + 12345 \times 1 + 1^2 - 12345 \times 1 + 1 \times 1 = 3
+$$
+
+(2)
+
+$$
+A[1][2] = 1^2 + 12345 \times 1 + 2^2 - 12345 \times 2 + 1 \times 2 = -12338
+$$
+
+$$
+A[2][1]=22+12345 \times 2+12−12345 \times 1+2 \times 1=12352
+$$
+
+$$
+A[2][2]=22+12345 \times 2+22−12345 \times 2+2 \times 2=12
+$$
+
+So the smallest element is -12338
+
+## Solution / 思路解析
+
+- 思路概述:矩阵元素按公式 $A[i][j] = i^2 + 12345i + j^2 -12345j + i j$ 定义,要求第 $M$ 小元素。常见做法是对答案值 $X$ 进行二分(答案是某个整数或实数范围内的值),并用一个计数函数计算矩阵中小于等于 $X$ 的元素个数 cnt。如果 cnt >= M,则可将上界收缩,否则下界上移。最终二分求得第 M 小元素的值。
+- 计数函数实现要点(代码对应):对每一行 i,关于 j 的不等式 $A[i][j] \le X$ 是一个关于 j 的二次不等式。把它化为标准二次形式后,计算判别式并求解实根区间 $[L,R]$,然后取整并交到 $[1,N]$ 区间内,令该行对总计数贡献为 $\max(0, R-L+1)$。遍历所有 i 累加计数得到 cnt。
+- 复杂度:每次二分需要对 N 行做一次二次方程求根,复杂度 O(N)(每行常数时间,主要是开方操作)。二分迭代次数为 O(log RANGE)(RANGE 为值域长度),整体复杂度约 O(N log RANGE)。空间 O(1)(常数额外空间)。
+- 精度与整型注意:实现中使用 64 位整数和 long double 来计算判别式与根,并对上下界做向上/向下取整以得到整数列范围,且提前剪枝(D<0)以提高稳定性。
+- 边界与注意事项:
+ - 确保对极端 X 值(非常大或非常小)时判别式不会产生误差;使用 long double 并在转换为整数时加减一个小量(如 1e-12)来抵消浮点误差。
+ - 当 N 较大(例如 50000)时,count_leq 的每次遍历仍是可行的,但注意总二分次数与常数因子会影响运行时间。
diff --git a/algorithm/2021F/lab_02/lab_02_F/lab_02_F.cpp b/algorithm/2021F/lab_02/lab_02_F/lab_02_F.cpp
deleted file mode 100644
index b0505237..00000000
--- a/algorithm/2021F/lab_02/lab_02_F/lab_02_F.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-//@Tag TODO
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-
-#ifdef ALGORITHM_TEST_MACRO
-namespace lab_02_F{
-#endif
-
-using std::cin;
-using std::tie;
-using std::cout;
-using std::list;
-using std::sort;
-using std::array;
-using std::deque;
-using std::queue;
-using std::stack;
-using std::tuple;
-using std::string;
-using std::vector;
-using std::unordered_map;
-using std::unordered_set;
-using std::priority_queue;
-static constexpr const char end{'\n'};
-
-using num_t = int32_t;
-using input_type = tuple;
-using output_type = num_t;
-
-inline input_type read();
-
-output_type cal(input_type data);
-
-void output(const output_type &data);
-
-int main() {
- auto input_data = read();
- auto output_data = cal(input_data);
- output(output_data);
- return 0;
-}
-
-inline input_type read() {
- num_t a{0}, b{0};
- std::cin >> a >> b;
- return std::make_tuple(a, b);
-}
-
-output_type cal(input_type data) {
- num_t a{0}, b{0};
- tie(a, b) = data;
- num_t c = a + b;
- return c;
-}
-
-void output(const output_type &data) {
- cout << data << end;
-}
-
-static const auto faster_streams = [] {
- srand(time(nullptr));
- // use time to init the random seed
- std::ios::sync_with_stdio(false);
- std::istream::sync_with_stdio(false);
- std::ostream::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::cout.tie(nullptr);
- // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
- return 0;
-}();
-#ifdef ALGORITHM_TEST_MACRO
-}
-#endif
diff --git a/algorithm/2021F/lab_02/lab_02_F/lab_02_F_test.cpp b/algorithm/2021F/lab_02/lab_02_F/lab_02_F_test.cpp
deleted file mode 100644
index efde1036..00000000
--- a/algorithm/2021F/lab_02/lab_02_F/lab_02_F_test.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-#ifdef ALGORITHM_TEST_MACRO
-
-#include
-#include
-#include
-#include
-#include
-
-#include "lab_02_F.cpp"
-
-std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_F/resource/"; }
-
-const std::string CS203_redirect::file_paths = getFilePath();
-
-namespace lab_02_F {
-
-using std::tie;
-using std::cin;
-using std::cout;
-using std::tuple;
-using std::vector;
-
-using Catch::Matchers::Equals;
-using Catch::Matchers::UnorderedEquals;
-using Catch::Matchers::Contains;
-
-TEST_CASE("test case 1", "[test 02 F]") {
- const auto output_data = cal(std::make_tuple(114, 514));
- CHECK(output_data == 628);
- CHECK(1 + 2 == 3);
- vector vec{2, 7, 11, 15};
- SECTION("CHECK_THAT 1") {
- CHECK_THAT(vec, Contains({2}));
- }SECTION("vec matcher") {
- CHECK_THAT(vec, UnorderedEquals({15, 11, 7, 2}));
- }
-}
-// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
-TEST_CASE("test case with sequence", "[test 02 F][.]") {
- CS203_sequence sequence{1, 0, 0}; // // 基础设定,[1,1]
- sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
- sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
- sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
- const auto files_name = sequence.get_files(true);
- // 获取一个std::tuple ,
- // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
- for (const auto &file_name: files_name) {
- string datain, dataout, testout; // 声明
- tie(datain, dataout, testout) = file_name; // 解包
- {
- const CS203_redirect cr{datain, testout}; // 重定向输入,输出
- main();
- // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
- }
- CHECK(compareFiles(testout, dataout));
- }
-}
-}
-#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_02/lab_02_F/main.cpp b/algorithm/2021F/lab_02/lab_02_F/main.cpp
new file mode 100644
index 00000000..68737c6c
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/main.cpp
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+// Problem F: find M-th smallest element in A where
+// A[i][j] = i^2 + 12345*i + j^2 -12345*j + i*j
+// Approach: binary search on value X, count number of pairs (i,j) with A[i][j] <= X
+
+#include
+#include
+#include
+#include
+#include
+
+#ifdef ALGORITHM_TEST_MACRO
+namespace lab_02_F {
+#endif
+
+using std::cin;
+using std::cout;
+using std::vector;
+using std::int64_t;
+using std::int32_t;
+static constexpr const char end{'\n'};
+
+// count number of pairs (i,j) with 1<=i,j<=N and A[i][j] <= X
+static inline int64_t count_leq(int N, int64_t X) {
+ int64_t cnt = 0;
+ for (int i = 1; i <= N; ++i) {
+ int64_t B = int64_t(i) - int64_t(12345);
+ int64_t C = int64_t(i) * int64_t(i) + int64_t(12345) * int64_t(i) - X;
+ long double D = (long double) B * (long double) B - 4.0L * (long double) C;
+ if (D < 0) continue;
+ long double sd = std::sqrt(D);
+ long double r1 = (-(long double) B - sd) / 2.0L;
+ long double r2 = (-(long double) B + sd) / 2.0L;
+ auto L = (int64_t) std::ceil(r1 - 1e-12L);
+ auto R = (int64_t) std::floor(r2 + 1e-12L);
+ if (R < 1 || L > N) continue;
+ if (L < 1) L = 1;
+ if (R > N) R = N;
+ if (R >= L) cnt += (R - L + 1);
+ if (cnt > (int64_t(1) << 62)) return cnt;
+ }
+ return cnt;
+}
+
+// ---- Input / Process / Output style ----
+using input_type = vector >; // (N, M)
+using output_type = vector;
+
+static input_type read_input() {
+ int T;
+ if (!(cin >> T)) return {};
+ input_type in;
+ in.reserve(T);
+ for (int i = 0; i < T; ++i) {
+ int N;
+ int64_t M;
+ cin >> N >> M;
+ in.emplace_back(N, M);
+ }
+ return in;
+}
+
+static output_type process(const input_type &in) {
+ output_type ans;
+ ans.reserve(in.size());
+ for (auto &pr: in) {
+ int N = pr.first;
+ int64_t M = pr.second;
+ int64_t lo = std::numeric_limits::min() / 4;
+ int64_t hi = std::numeric_limits::max() / 4;
+ while (lo < hi) {
+ int64_t mid = lo + ((hi - lo) >> 1);
+ int64_t cnt = count_leq(N, mid);
+ if (cnt >= M) hi = mid;
+ else lo = mid + 1;
+ }
+ ans.push_back(lo);
+ }
+ return ans;
+}
+
+static void write_output(const output_type &out) {
+ for (auto v: out) { cout << v << end; }
+}
+
+int main() {
+ auto input = read_input();
+ auto output = process(input);
+ write_output(output);
+ return 0;
+}
+
+static const auto faster_streams = [] {
+ srand(time(nullptr));
+ // use time to init the random seed
+
+ std::ios::sync_with_stdio(false);
+ std::istream::sync_with_stdio(false);
+ std::ostream::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cout.tie(nullptr);
+ // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
+ return 0;
+}();
+
+#ifdef ALGORITHM_TEST_MACRO
+}
+#endif
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/01.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/01.data.in
new file mode 100644
index 00000000..9827e27e
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/01.data.in
@@ -0,0 +1,3 @@
+2
+1 1
+2 1
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/01.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/01.data.out
new file mode 100644
index 00000000..ac88be52
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/01.data.out
@@ -0,0 +1,2 @@
+3
+-12338
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/02.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/02.data.in
new file mode 100644
index 00000000..20478c63
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/02.data.in
@@ -0,0 +1,2 @@
+1
+1 1
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/02.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/02.data.out
new file mode 100644
index 00000000..00750edc
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/02.data.out
@@ -0,0 +1 @@
+3
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/03.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/03.data.in
new file mode 100644
index 00000000..0bdc75bb
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/03.data.in
@@ -0,0 +1,2 @@
+1
+2 1
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/03.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/03.data.out
new file mode 100644
index 00000000..88a7900e
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/03.data.out
@@ -0,0 +1 @@
+-12338
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/04.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/04.data.in
new file mode 100644
index 00000000..c0fe29e3
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/04.data.in
@@ -0,0 +1,2 @@
+1
+2 4
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/04.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/04.data.out
new file mode 100644
index 00000000..1295f05c
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/04.data.out
@@ -0,0 +1 @@
+12352
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/05.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/05.data.in
new file mode 100644
index 00000000..b620873c
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/05.data.in
@@ -0,0 +1,3 @@
+2
+3 1
+3 9
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/05.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/05.data.out
new file mode 100644
index 00000000..5d0c3379
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/05.data.out
@@ -0,0 +1,2 @@
+-24677
+24703
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/06.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/06.data.in
new file mode 100644
index 00000000..2121b1db
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/06.data.in
@@ -0,0 +1,2 @@
+1
+10 50
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/06.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/06.data.out
new file mode 100644
index 00000000..78eb67ce
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/06.data.out
@@ -0,0 +1 @@
+75
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/07.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/07.data.in
new file mode 100644
index 00000000..34dd3844
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/07.data.in
@@ -0,0 +1,2 @@
+1
+100 1
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/07.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/07.data.out
new file mode 100644
index 00000000..e8d3e82f
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/07.data.out
@@ -0,0 +1 @@
+-1212054
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/08.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/08.data.in
new file mode 100644
index 00000000..fd6b5d59
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/08.data.in
@@ -0,0 +1,2 @@
+1
+100 10000
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/08.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/08.data.out
new file mode 100644
index 00000000..5e80e63a
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/08.data.out
@@ -0,0 +1 @@
+1232256
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/09.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/09.data.in
new file mode 100644
index 00000000..b252b6b9
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/09.data.in
@@ -0,0 +1,2 @@
+1
+500 125000
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/09.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/09.data.out
new file mode 100644
index 00000000..83e38f92
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/09.data.out
@@ -0,0 +1 @@
+243964
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/10.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/10.data.in
new file mode 100644
index 00000000..d48b6b12
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/10.data.in
@@ -0,0 +1,4 @@
+3
+1 1
+2 2
+3 5
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/10.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/10.data.out
new file mode 100644
index 00000000..ed6f6cdb
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/10.data.out
@@ -0,0 +1,3 @@
+3
+3
+12
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/11.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/11.data.in
new file mode 100644
index 00000000..f12d8878
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/11.data.in
@@ -0,0 +1,2 @@
+1
+50000 2500000000
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/11.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/11.data.out
new file mode 100644
index 00000000..7806b3dc
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/11.data.out
@@ -0,0 +1 @@
+7500000000
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/12.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/12.data.in
new file mode 100644
index 00000000..ff544720
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/12.data.in
@@ -0,0 +1,3 @@
+2
+5 10
+5 1
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/12.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/12.data.out
new file mode 100644
index 00000000..7accec01
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/12.data.out
@@ -0,0 +1,2 @@
+-12284
+-49349
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/13.data.in b/algorithm/2021F/lab_02/lab_02_F/resource/13.data.in
new file mode 100644
index 00000000..cd5c7a32
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/13.data.in
@@ -0,0 +1,2 @@
+1
+50 1250
diff --git a/algorithm/2021F/lab_02/lab_02_F/resource/13.data.out b/algorithm/2021F/lab_02/lab_02_F/resource/13.data.out
new file mode 100644
index 00000000..3e4bffc1
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/resource/13.data.out
@@ -0,0 +1 @@
+1875
diff --git a/algorithm/2021F/lab_02/lab_02_F/test.cpp b/algorithm/2021F/lab_02/lab_02_F/test.cpp
new file mode 100644
index 00000000..b2ad717d
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_F/test.cpp
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_F/resource/"; }
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_02_F {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 02 F]") {
+ CS203_sequence sequence{1, 13, 2}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_02/lab_02_G/CMakeLists.txt b/algorithm/2021F/lab_02/lab_02_G/CMakeLists.txt
new file mode 100644
index 00000000..768dc1e2
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.16.6)
+set(PROBLEM_ORDER G)
+project(${PROJECT_NAME}_${PROBLEM_ORDER})
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+
+enable_testing()
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
+set(CMAKE_CXX_STANDARD 17)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
+target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
+target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
+add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
+set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_02/lab_02_G/README.md b/algorithm/2021F/lab_02/lab_02_G/README.md
new file mode 100644
index 00000000..bed83ffb
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/README.md
@@ -0,0 +1,42 @@
+# Problem G (Order 1427)
+
+## Description
+
+Given an array $a$ of length $n$. You need to find a subarray $a[l..r]$ with length at least $k$ that has the largest median.
+
+A median in an array of length $x$ is the $\left\lfloor\frac{x+1}{2}\right\rfloor$-th smallest element. For example: $\mathrm{median}([1,2,3,4]) = 2$, $\mathrm{median}([3,2,1]) = 2$, $\mathrm{median}([2,1,2,1]) = 1$.
+
+## Input
+
+The 1st line contains two integers $n$ and $k$ ($1 \le k \le n \le 200000$). The 2nd line has $n$ integers: $a_1, a_2, \dots, a_n$. For each $a_i$ ($0 \le a_i \le 10^9$).
+
+## Output
+
+Output one integer $m$ — the maximum median you can get.
+
+## Sample Input
+
+```text
+5 3
+1 2 3 2 1
+```
+
+## Sample Output
+
+```text
+2
+```
+
+## Hint
+
+The max median 2 is found from the subarray [2, 3, 2].
+
+## Solution / 思路解析
+
+- 思路概述:要找到长度至少为 $k$ 的子数组中能取得的最大中位数,可用二分答案(枚举候选中位数 $x$)+判定函数的方法:判断是否存在一个长度至少为 $k$ 的子数组,其中位数不小于 $x$。对 $x$ 做二分,找到最大的可行值即为答案。
+- 判定函数(代码实现要点):把原数组元素映射为 +1(若 $a_i \ge x$)或 -1(若 $a_i < x$),计算前缀和 pref。若存在区间长度至少为 $k$,使得区间和大于 0,则说明该区间中 “不小于 $x$” 的元素严格多于“小于 $x$”的元素,从而中位数至少为 $x$。实现通过维护最小前缀值(到位置 i-k)并检查 pref[i] - min_pref > 0 来完成判定。
+- 二分搜索:对答案的搜索区间可设为题面值域(代码中用 [0, 1e9]),在每步取上中位数向上取整的方式推进(`mid = lo + (hi-lo+1)>>1`),当判定函数为真时把 lo 提升到 mid,否则把 hi 下调到 mid-1,循环结束 lo 即为答案。
+- 复杂度:判定函数为 O(n),二分搜索约 O(log V),总体 O(n log V)。空间 O(n) 用于前缀和数组。
+- 边界与注意事项:
+ - 要确保映射与前缀和的定义满足中位数判定的严格性(这里用 +1/-1,并用严格大于 0 判断);对于偶数长度的中位数定义(题中定义为 floor((len+1)/2))该映射是正确的。
+ - 注意输入值域(这里在 0..1e9)和 int32 类型的安全使用;实现中用 int32_t 存储并操作前缀和(累加范围在 [-n,n] 内),安全。
diff --git a/algorithm/2021F/lab_02/lab_02_G/main.cpp b/algorithm/2021F/lab_02/lab_02_G/main.cpp
new file mode 100644
index 00000000..9f17261b
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/main.cpp
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+// Problem G: max median of any subarray with length >= k
+
+#include
+#include
+#include
+#include
+
+#ifdef ALGORITHM_TEST_MACRO
+namespace lab_02_G {
+#endif
+
+using std::cin;
+using std::cout;
+using std::vector;
+using std::int32_t;
+using std::int64_t;
+static constexpr const char end{'\n'};
+
+struct Input {
+ int32_t n;
+ int32_t k;
+ vector a;
+};
+
+static Input read_input() {
+ Input in;
+ cin >> in.n >> in.k;
+ in.a.assign(in.n, 0);
+ for (int32_t i = 0; i < in.n; ++i) cin >> in.a[i];
+ return in;
+}
+
+// check if there exists subarray length >= k with median >= x
+static bool check_median_at_least(const vector &a, int32_t k, int32_t x) {
+ const int n = static_cast(a.size());
+ // prefix sums of transformed values: +1 if a[i]>=x, else -1
+ vector pref(n + 1, 0);
+ for (int i = 1; i <= n; ++i) {
+ pref[i] = pref[i-1] + (a[i-1] >= x ? 1 : -1);
+ }
+ int32_t min_pref = 0; // minimal prefix up to index i-k
+ for (int i = k; i <= n; ++i) {
+ // update min_pref with prefix at i-k
+ min_pref = std::min(min_pref, pref[i - k]);
+ if (pref[i] - min_pref > 0) return true;
+ }
+ return false;
+}
+
+static int32_t solve(const Input &in) {
+ int32_t lo = 0;
+ int32_t hi = 1000000000; // per constraints
+ while (lo < hi) {
+ int32_t mid = lo + ((hi - lo + 1) >> 1);
+ if (check_median_at_least(in.a, in.k, mid)) lo = mid;
+ else hi = mid - 1;
+ }
+ return lo;
+}
+
+static void write_output(int32_t ans) {
+ cout << ans << end;
+}
+
+int main() {
+ auto in = read_input();
+ auto ans = solve(in);
+ write_output(ans);
+ return 0;
+}
+static const auto faster_streams = [] {
+ srand(time(nullptr));
+ // use time to init the random seed
+ std::ios::sync_with_stdio(false);
+ std::istream::sync_with_stdio(false);
+ std::ostream::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cout.tie(nullptr);
+ // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
+ return 0;
+}();
+#ifdef ALGORITHM_TEST_MACRO
+}
+#endif
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/.gitignore b/algorithm/2021F/lab_02/lab_02_G/resource/.gitignore
new file mode 100644
index 00000000..95488964
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/.gitignore
@@ -0,0 +1,3 @@
+!*.data.in
+!*.data.out
+*.test.out
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/1.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/1.data.in
new file mode 100644
index 00000000..2d1ae650
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/1.data.in
@@ -0,0 +1,2 @@
+5 3
+1 2 3 2 1
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/1.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/1.data.out
new file mode 100644
index 00000000..0cfbf088
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/1.data.out
@@ -0,0 +1 @@
+2
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/2.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/2.data.in
new file mode 100644
index 00000000..2d1ae650
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/2.data.in
@@ -0,0 +1,2 @@
+5 3
+1 2 3 2 1
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/2.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/2.data.out
new file mode 100644
index 00000000..0cfbf088
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/2.data.out
@@ -0,0 +1 @@
+2
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/3.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/3.data.in
new file mode 100644
index 00000000..8ccff4d1
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/3.data.in
@@ -0,0 +1,2 @@
+6 1
+0 0 1000000000 5 5 3
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/3.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/3.data.out
new file mode 100644
index 00000000..770fdcfb
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/3.data.out
@@ -0,0 +1 @@
+1000000000
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/4.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/4.data.in
new file mode 100644
index 00000000..699fabc2
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/4.data.in
@@ -0,0 +1,2 @@
+7 7
+7 1 5 3 9 2 4
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/4.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/4.data.out
new file mode 100644
index 00000000..b8626c4c
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/4.data.out
@@ -0,0 +1 @@
+4
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/5.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/5.data.in
new file mode 100644
index 00000000..71509390
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/5.data.in
@@ -0,0 +1,2 @@
+8 3
+2 2 2 2 2 2 2 2
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/5.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/5.data.out
new file mode 100644
index 00000000..0cfbf088
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/5.data.out
@@ -0,0 +1 @@
+2
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/6.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/6.data.in
new file mode 100644
index 00000000..cd408e50
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/6.data.in
@@ -0,0 +1,2 @@
+10 4
+1 2 3 4 5 6 7 8 9 10
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/6.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/6.data.out
new file mode 100644
index 00000000..45a4fb75
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/6.data.out
@@ -0,0 +1 @@
+8
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/7.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/7.data.in
new file mode 100644
index 00000000..d8691427
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/7.data.in
@@ -0,0 +1,2 @@
+12 5
+5 5 5 6 7 1000000000 999999999 0 3 6 7 8
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/7.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/7.data.out
new file mode 100644
index 00000000..7f8f011e
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/7.data.out
@@ -0,0 +1 @@
+7
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/8.data.in b/algorithm/2021F/lab_02/lab_02_G/resource/8.data.in
new file mode 100644
index 00000000..065b1b50
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/8.data.in
@@ -0,0 +1,2 @@
+10 6
+10 9 8 7 6 5 4 3 2 1
diff --git a/algorithm/2021F/lab_02/lab_02_G/resource/8.data.out b/algorithm/2021F/lab_02/lab_02_G/resource/8.data.out
new file mode 100644
index 00000000..7f8f011e
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/resource/8.data.out
@@ -0,0 +1 @@
+7
diff --git a/algorithm/2021F/lab_02/lab_02_G/test.cpp b/algorithm/2021F/lab_02/lab_02_G/test.cpp
new file mode 100644
index 00000000..cdcaf816
--- /dev/null
+++ b/algorithm/2021F/lab_02/lab_02_G/test.cpp
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_02/lab_02_G/resource/"; }
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_02_G {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 02 G]") {
+ CS203_sequence sequence{1, 8, 1}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/CMakeLists.txt b/algorithm/2021F/lab_03/CMakeLists.txt
index 42d34022..2df387a9 100644
--- a/algorithm/2021F/lab_03/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/CMakeLists.txt
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-set(dependencies A B C D E F)
+set(dependencies A B C D E F G)
foreach (elementName IN LISTS dependencies)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${elementName})
endforeach ()
diff --git a/algorithm/2021F/lab_03/README.md b/algorithm/2021F/lab_03/README.md
new file mode 100644
index 00000000..d15e9de1
--- /dev/null
+++ b/algorithm/2021F/lab_03/README.md
@@ -0,0 +1,37 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# Contest1095 - CS203 2021 Fall Lab 03 Sorting
+
+> cid: 1096
+
+Welcome to CS203 2021 Fall Lab 03! Enjoy this Lab!
+
+There are six problems for you to solve.
+
+Score:
+
+- A: 10
+- B: 15/10
+- C: 15/10
+- D: 20/15
+- E: 20/15
+- F: 20/20
+- G: undefined/20
+
+Reading the samples and hints carefully can help you understand the problem.
+
+## Problems
+
+| Problem | problem id |
+|---:|---:|
+| A | 1428 |
+| B | 1429 |
+| C | 1430 |
+| D | 1431 |
+| E | 1432 |
+| F | 1433 |
+| G | 1434 |
+
+B, E, F被复用
diff --git a/algorithm/2021F/lab_03/cs203.submit.csv b/algorithm/2021F/lab_03/cs203.submit.csv
new file mode 100644
index 00000000..bbfd3695
--- /dev/null
+++ b/algorithm/2021F/lab_03/cs203.submit.csv
@@ -0,0 +1,8 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 268, 72, 640, 332, 1, 5, 90, 113, 1521, 33, 227, 1261
+B, 277, 3, 342, 314, 10, 26, 82, 136, 1190, 26, 251, 913
+C, 240, 11, 609, 14, 1, 34, 59, 968, 16, 194, 758
+D, 247, 81, 188, 2, 34, 46, 598, 10, 118, 470
+E, 224, 1, 401, 265, 2, 9, 31, 45, 978, 16, 187, 775
+F, 215, 1750, 225, 3, 1, 240, 99, 2533, 30, 573, 1930
+Total, 1471, 87, 3823, 1338, 18, 42, 511, 498, 7788, 131, 1550, 6107
diff --git a/algorithm/2021F/lab_03/cs217.submit.csv b/algorithm/2021F/lab_03/cs217.submit.csv
new file mode 100644
index 00000000..595e1bf5
--- /dev/null
+++ b/algorithm/2021F/lab_03/cs217.submit.csv
@@ -0,0 +1,9 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 32, 1, 28, 15, 3, 1, 80, 1, 25, 54
+B, 30, 1, 21, 15, 1, 4, 4, 76, 40, 36
+C, 28, 36, 1, 65, 19, 46
+D, 38, 8, 7, 1, 1, 1, 56, 32, 24
+E, 34, 29, 18, 1, 2, 3, 87, 6, 31, 50
+F, 27, 93, 8, 13, 4, 145, 50, 95
+G, 32, 63, 6, 8, 3, 112, 52, 60
+Total, 221, 2, 278, 69, 2, 1, 32, 16, 621, 7, 249, 365
diff --git a/algorithm/2021F/lab_03/lab_03_A/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_A/CMakeLists.txt
index 354751e1..40897f09 100644
--- a/algorithm/2021F/lab_03/lab_03_A/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_A/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_A/README.md b/algorithm/2021F/lab_03/lab_03_A/README.md
new file mode 100644
index 00000000..c7908db4
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_A/README.md
@@ -0,0 +1,72 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_A
+
+## Description
+
+Let $A$ and $B$ be two non-decreasing arrays of sizes $n$ and $m$ respectively.
+
+We form $C = \{A_1, A_2, \dots, A_n, B_1, B_2, \dots, B_m\}$. A pair $(C_i, C_j)$ is a reverse pair when $C_i > C_j$ and $i < j$.
+
+For each test case compute two outputs. First, the number of reverse pairs in $C$.
+
+Second, output the merged non-decreasing sequence $C$.
+
+## Input
+
+The first line contains an integer $T$ ($1 \le T \le 10$). Each test case contains three lines.
+
+The first line of a test case contains two integers $n$ and $m$.
+
+The second line contains $n$ integers $A_1, A_2, \dots, A_n$.
+
+The third line contains $m$ integers $B_1, B_2, \dots, B_m$.
+
+Constraints: $1 \le n, m \le 10^5$ and $0 \le A_i, B_j \le 10^9$.
+
+## Output
+
+For each test case print two lines. The first line is the number of reverse pairs in $C$.
+
+The second line is the merged sequence $C$: $C_1, C_2, \dots, C_{n+m}$.
+
+## Sample
+
+### Sample Input
+
+```text
+2
+2 4
+2 3
+1 2 3 4
+2 2
+3 4
+1 2
+```
+
+### Sample Output
+
+```text
+3
+1 2 2 3 3 4 4
+1
+2 3 4 1 2
+```
+
+## Notes
+
+This problem can be solved by merging the two sorted arrays with a two-pointer method.
+
+While merging, count how many elements from the remaining left part are greater than the current element taken from the right part to accumulate cross-inversions.
+
+## Analysis
+
+该解法在合并两个非递减数组的同时统计跨数组的逆序对。
+
+做法是将左数组和右数组拷贝到同一个工作缓冲区,然后用双指针进行归并;当从左半部分取出一个元素时,增加当前已被取出的右半部分元素数量,作为该元素所贡献的逆序对个数。
+
+合并后的值保存在缓冲区中,最终输出合并的非递减序列以及逆序对总数。
+
+此方法对每个测试用例的时间复杂度为 $O(n+m)$,需要 $O(n+m)$ 的额外内存用于临时缓冲区。
diff --git a/algorithm/2021F/lab_03/lab_03_A/lab_03_A.cpp b/algorithm/2021F/lab_03/lab_03_A/main.cpp
similarity index 81%
rename from algorithm/2021F/lab_03/lab_03_A/lab_03_A.cpp
rename to algorithm/2021F/lab_03/lab_03_A/main.cpp
index 7e584667..b4ff79f3 100644
--- a/algorithm/2021F/lab_03/lab_03_A/lab_03_A.cpp
+++ b/algorithm/2021F/lab_03/lab_03_A/main.cpp
@@ -1,24 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-/*
- * @题目描述
- * 在____内, 如果数组C中,有 i,j ∈N+, i < j, C[i]>C[j], 则 (C[i],C[j])为一个逆序对
- * 给出两个非递减数组,长度为ν的数组D与长度为μ的数组E
- * 首先,请给出, C{D.begin(),D.end(),E.begin(),E.end()}所组成的逆序对
- * 其次, 给出非递减数组C本身
- * 如果没有IDEA的话,可以参考lecture3的口口页
- * @输入
- * 第一行给出一个int32_t τ ∈ [1,10],代表测试样例数
- * 接下来对每个测试用例
- * 第一行有两个int32_t ν,μ 代表D,E两个非递减数组的长度
- * 第二行有 ν 个int32_t, {D.begin(),D.end()}
- * 第三行有 μ 个int32_t, {E.begin(),E.end()}
- * ν ∈ [1,10^5], μ ∈ [1,10^5] ,D,E中的每个元素都 ∈ [1,10^9]
- * @输出
- * 对每个测试用例来说
- * 第一行输出逆序对数量
- * 第二行输出排好序的C
- * */
//@Tag Done
#include
#include
diff --git a/algorithm/2021F/lab_03/lab_03_A/lab_03_A_test.cpp b/algorithm/2021F/lab_03/lab_03_A/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_03/lab_03_A/lab_03_A_test.cpp
rename to algorithm/2021F/lab_03/lab_03_A/test.cpp
index dab569ba..7a47c13f 100644
--- a/algorithm/2021F/lab_03/lab_03_A/lab_03_A_test.cpp
+++ b/algorithm/2021F/lab_03/lab_03_A/test.cpp
@@ -12,7 +12,7 @@
#include
#include
-#include "lab_03_A.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_A/resource/"; }
diff --git a/algorithm/2021F/lab_03/lab_03_B/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_B/CMakeLists.txt
index c490830a..dac0839a 100644
--- a/algorithm/2021F/lab_03/lab_03_B/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_B/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_B/README.md b/algorithm/2021F/lab_03/lab_03_B/README.md
new file mode 100644
index 00000000..6d444589
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_B/README.md
@@ -0,0 +1,47 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_B — Median (double form)
+
+## Description
+
+Given an array α of length ν, output the "median in double form" defined as:
+
+- If ν is even: return (α[ν/2 - 1] + α[ν/2])
+- If ν is odd: return 2 * α[ν/2]
+
+The array is 0-indexed in the definition above.
+
+The result is an integer (not a floating point number): when ν is odd we return twice the middle element, when ν is even we return the sum of the two middle elements.
+
+## Input
+
+The first line contains an integer n (2 ≤ n ≤ 5*10^6).
+
+The second line contains n integers (each in range [0, 2^31 - 1]).
+
+## Output
+
+Output a single integer: the median in the double form defined above.
+
+## Notes
+
+- Be careful with overflow: 2 * int32_t can overflow a 32-bit signed integer. Use 64-bit integers (e.g., `int64_t`) when computing the result.
+
+## Analysis
+
+该程序使用自定义的归并排序对输入数组进行排序,以便可以确定性地访问中间元素。
+
+排序完成后,当数组长度为偶数时返回中间两个元素之和;当长度为奇数时返回中间元素的两倍,从而实现“double median”的定义。
+
+所有计算使用 64 位整数,以避免对 32 位整数执行加倍或相加时发生溢出。
+
+归并排序为主要耗时步骤,时间复杂度为 $O(n\log n)$,并需要 $O(n)$ 的辅助内存作为临时缓冲区。
+
+## 复用信息
+
+- Contest 1095:CS203 2021 Fall Lab 03 Sorting
+- Contest 1096:CS217 2021 Fall Lab 03 Sorting
+- Contest 1137:CS203 2023 Fall Lab 2 Sorting
+- Contest 1187:CS203 2025 Fall Lab 2 Sorting
diff --git a/algorithm/2021F/lab_03/lab_03_B/lab_03_B.cpp b/algorithm/2021F/lab_03/lab_03_B/main.cpp
similarity index 85%
rename from algorithm/2021F/lab_03/lab_03_B/lab_03_B.cpp
rename to algorithm/2021F/lab_03/lab_03_B/main.cpp
index a0cd5b2d..e0ed633c 100644
--- a/algorithm/2021F/lab_03/lab_03_B/lab_03_B.cpp
+++ b/algorithm/2021F/lab_03/lab_03_B/main.cpp
@@ -1,25 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-/*
- * @题目描述
- * 数组α具有ν个元素,请输出数组a `中位数的double形式`
- * `中位数的double形式` 定义为,
- * match (ν % 2) {
- * 0 => { return (α[ν/2-1]+α[ν/2]); }
- * 1 => { return 2*α[ν/2]; }
- * };
- * @输入
- * 第一行输入 int32_t n, 大于1,小于5*10^6
- * 第二行有n个int32_t, [0,2^31-1]
- * @输出
- * 一个 `中位数的double形式`
- * @解析,此处坑点在于 int32_t *2会爆掉,所以都需要使用int64_t
- * */
+
//@Tag Done
-#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
-#pragma GCC optimize("inline-small-functions")
-#pragma GCC optimize("-finline-small-functions")
-#pragma GCC target("mmx")
+
#include
#include
@@ -27,7 +10,12 @@
#include
#include
-#ifdef ALGORITHM_TEST_MACRO
+#ifndef ALGORITHM_TEST_MACRO
+#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
+#pragma GCC optimize("inline-small-functions")
+#pragma GCC optimize("-finline-small-functions")
+#pragma GCC target("tune=native")
+#else
namespace lab_03_B{
#endif
diff --git a/algorithm/2021F/lab_03/lab_03_B/lab_03_B_test.cpp b/algorithm/2021F/lab_03/lab_03_B/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_03/lab_03_B/lab_03_B_test.cpp
rename to algorithm/2021F/lab_03/lab_03_B/test.cpp
index 7fa0a73f..a74cd920 100644
--- a/algorithm/2021F/lab_03/lab_03_B/lab_03_B_test.cpp
+++ b/algorithm/2021F/lab_03/lab_03_B/test.cpp
@@ -8,7 +8,7 @@
#include
#include
-#include "lab_03_B.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_B/resource/"; }
diff --git a/algorithm/2021F/lab_03/lab_03_C/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_C/CMakeLists.txt
index 5de3f24f..f07d2e4c 100644
--- a/algorithm/2021F/lab_03/lab_03_C/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_C/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_C/README.md b/algorithm/2021F/lab_03/lab_03_C/README.md
new file mode 100644
index 00000000..b00ec478
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_C/README.md
@@ -0,0 +1,64 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_C
+
+## Description
+
+Given an unsorted sequence, sort it using insertion sort and selection sort respectively.
+
+Compare which algorithm performs fewer operations.
+
+We count both "swap two numbers" and "compare two numbers" as one operation each.
+
+The algorithm with fewer total operations is considered better.
+
+It is guaranteed that the number of operations differs between the two algorithms for all test cases.
+
+## Input
+
+The first line contains an integer $T$ ($1 \le T \le 10$), the number of test cases.
+
+For each test case the first line contains an integer $n$ ($2 \le n \le 10^3$), followed by $n$ integers.
+
+## Output
+
+For each test case, output two lines. The first line is the sorted array in ascending order.
+
+The second line is the winner message.
+
+Print "Insertion Sort wins!" if insertion sort uses fewer operations.
+
+Otherwise print "Selection Sort wins!".
+
+## Analysis
+
+实现通过分别模拟插入排序和选择排序的操作计数来比较两者的开销。
+
+插入排序在每次将当前元素向前移动时,对每次比较计数一次;当发生相邻元素交换时再计数一次交换操作。
+
+选择排序在每次扫描后缀寻找最小值时,对每次比较计数一次;若找到的最小值需要与当前位置交换,则计数一次交换操作。
+
+在得到两者的操作计数后,程序用 `std::sort` 对序列进行排序以打印最终结果,并根据计数比较决定胜者。
+
+两种计数例程的最坏情况时间复杂度均为 $O(n^2)$,额外空间开销仅为对输入序列的临时拷贝($O(n)$)。
+
+## Sample
+
+### Sample Input
+
+```text
+2
+3 1 3 2
+3 3 2 1
+```
+
+### Sample Output
+
+```text
+1 2 3
+Insertion Sort wins!
+1 2 3
+Selection Sort wins!
+```
diff --git a/algorithm/2021F/lab_03/lab_03_C/lab_03_C.cpp b/algorithm/2021F/lab_03/lab_03_C/main.cpp
similarity index 67%
rename from algorithm/2021F/lab_03/lab_03_C/lab_03_C.cpp
rename to algorithm/2021F/lab_03/lab_03_C/main.cpp
index fd6f742b..a227a82d 100644
--- a/algorithm/2021F/lab_03/lab_03_C/lab_03_C.cpp
+++ b/algorithm/2021F/lab_03/lab_03_C/main.cpp
@@ -1,24 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-/*
- *@题目描述
- * 输入为一个无序序列, 请分别插入排序和选择排序,并比较哪一个更好
- * 只有"交换两个数字"和"比较两个数字"是一个操作.
- * 更好的排序算法,意味着操作数更少. 输入中不会有 操作数相同 的情况.
- *@输入
- * 第一行输入一个int32_t Τ(希腊字母 tau),代表测试用例数量
- * 之后每个测试用例
- * 第一行有size_t ν(希腊字母 Nu),代表数组长度
- * 接下来是ν个int32_t
- *@输出
- * 对每个测试用例来讲
- * 第一行输出排序好的数组(注:最后一个数字后没有空格)
- * 第二行 match (winner) {
- * "insertion" => {"Insertion Sort wins!"}
- * "selection" => {"Selection Sort wins!"}
- * }
- *@注: 不给样例解析的屑题目
- * */
+
#include
#include
@@ -39,7 +21,7 @@
#ifdef ALGORITHM_TEST_MACRO
namespace lab_03_C{
#endif
-// TODO
+
using std::cin;
using std::tie;
using std::cout;
@@ -93,43 +75,43 @@ inline input_type read() {
}
int32_t selection_sort(vector nums) {
- int32_t will_return{0};
- const auto nums_size = static_cast(nums.size());
- for (int i = 0; i < nums_size - 1; ++i) {
- //will_return++;
+ int32_t ops = 0;
+ const int n = static_cast(nums.size());
+ for (int i = 0; i + 1 < n; ++i) {
int k = i;
- for (int j = i + 1; j < nums_size; ++j) {
- //will_return++;
- will_return++;
- if (nums[k] > nums[j]) {
- k = j;
- }
+ for (int j = i + 1; j < n; ++j) {
+ // compare nums[k] and nums[j]
+ ++ops;
+ if (nums[j] < nums[k]) k = j;
+ }
+ if (k != i) {
+ // one swap operation
+ ++ops;
+ std::swap(nums[i], nums[k]);
}
- will_return++;
- will_return++;
- std::swap(nums[i], nums[k]);
}
- will_return++;
- return will_return;
+ return ops;
}
int32_t insert_sort(vector nums) {
- int32_t will_return{0};
- const auto nums_size = static_cast(nums.size());
- for (int i = 0; i < nums_size; i++) {
- //will_return++;
- for (int j = i; j >= 1; j--) {
- //will_return++;
- will_return++;
+ int32_t ops = 0;
+ const int n = static_cast(nums.size());
+ for (int i = 1; i < n; ++i) {
+ int j = i;
+ while (j > 0) {
+ // compare nums[j-1] and nums[j]
+ ++ops;
if (nums[j - 1] > nums[j]) {
- will_return++;
+ // swap
+ ++ops;
std::swap(nums[j - 1], nums[j]);
+ --j;
+ } else {
+ break;
}
}
- will_return++;
}
- will_return++;
- return will_return;
+ return ops;
}
output_type cal(const input_type &data) {
diff --git a/algorithm/2021F/lab_03/lab_03_C/lab_03_C_test.cpp b/algorithm/2021F/lab_03/lab_03_C/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_03/lab_03_C/lab_03_C_test.cpp
rename to algorithm/2021F/lab_03/lab_03_C/test.cpp
index 0771abb8..6c17c4a1 100644
--- a/algorithm/2021F/lab_03/lab_03_C/lab_03_C_test.cpp
+++ b/algorithm/2021F/lab_03/lab_03_C/test.cpp
@@ -8,7 +8,7 @@
#include
#include
-#include "lab_03_C.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_C/resource/"; }
diff --git a/algorithm/2021F/lab_03/lab_03_D/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_D/CMakeLists.txt
index dce330ce..564aeae5 100644
--- a/algorithm/2021F/lab_03/lab_03_D/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_D/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_D/README.md b/algorithm/2021F/lab_03/lab_03_D/README.md
new file mode 100644
index 00000000..6bb348eb
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_D/README.md
@@ -0,0 +1,58 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_D
+
+## Description
+
+Given a sequence $\{a\}$ of $n$ distinct numbers, where $n$ is even.
+
+Permute the sequence to form a circle such that no element equals the average of its two neighbors.
+
+Formally, find a permutation $\{b\}$ of $\{a\}$ satisfying:
+
+- For all $i \in \{2,3,\dots,n-1\}$: $b_i \ne (b_{i-1}+b_{i+1})/2$.
+- Also $b_1 \ne (b_2 + b_n)/2$
+- $b_n \ne (b_{n-1} + b_1)/2$.
+
+There may be multiple valid answers. Print any one.
+
+A solution is guaranteed to exist for every test case.
+
+## Input
+
+The first line contains an integer $n$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$.
+
+Constraints: $n$ is even and $3 \le n \le 10^6$, $0 \le a_i \le 10^9$.
+
+## Output
+
+Print one line with $n$ integers: $b_1, b_2, \dots, b_n$.
+
+## Analysis
+
+算法先对数组进行排序,然后将前半部分和后半部分交替放置以构造圆排列。
+
+每个较小的元素都会与较大的元素相邻,因此其值不可能等于两邻居的平均值;对较大元素亦然。
+
+由于元素互不相同且 n 为偶数,两个半部分不会重叠,交错放置得到的序列是原序列的一个合法置换。
+
+排序步骤时间复杂度为 $O(n\log n)$,交错拼接为线性时间,额外空间仅为输出数组(或常数工作空间)。
+
+## Sample
+
+### Sample Input
+
+```text
+6
+3 2 1 4 5 6
+```
+
+### Sample Output
+
+```text
+1 2 4 3 5 6
+```
+
+> 似乎样例有错? 应该是 `1 4 2 5 3 6`
diff --git a/algorithm/2021F/lab_02/lab_02_E/lab_02_E.cpp b/algorithm/2021F/lab_03/lab_03_D/main.cpp
similarity index 63%
rename from algorithm/2021F/lab_02/lab_02_E/lab_02_E.cpp
rename to algorithm/2021F/lab_03/lab_03_D/main.cpp
index a1939137..6900c564 100644
--- a/algorithm/2021F/lab_02/lab_02_E/lab_02_E.cpp
+++ b/algorithm/2021F/lab_03/lab_03_D/main.cpp
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-//@Tag TODO
#include
#include
#include
@@ -17,7 +16,7 @@
#include
#ifdef ALGORITHM_TEST_MACRO
-namespace lab_02_E{
+namespace lab_03_D{
#endif
using std::cin;
@@ -37,9 +36,9 @@ using std::unordered_set;
using std::priority_queue;
static constexpr const char end{'\n'};
-using num_t = int32_t;
-using input_type = tuple, vector, vector>>;
-using output_type = vector;
+using num_t = int64_t;
+using input_type = std::vector;
+using output_type = std::vector;
inline input_type read();
@@ -55,36 +54,41 @@ int main() {
}
inline input_type read() {
- num_t n{0}, T{0};
- std::cin >> n >> T;
- vector A(n, -1), B(n, -1);
- for (int32_t i{0}; i < n; i++) {
- std::cin >> A[i];
+ int32_t n;
+ if (!(std::cin >> n)) return {};
+ input_type a;
+ a.reserve(n);
+ for (int32_t i = 0; i < n; ++i) {
+ num_t v;
+ std::cin >> v;
+ a.push_back(v);
}
- for (int32_t i{0}; i < n; i++) {
- std::cin >> B[i];
- }
- num_t tempa, tempb;
- vector> pairs;
- for (int32_t i{0}; i < T; ++i) {
- std::cin >> tempa >> tempb;
- pairs.emplace_back(tempa, tempb);
- }
- return std::make_tuple(A, B, pairs);
+ return a;
}
output_type cal(const input_type &data) {
- vector A, B;
- vector> pairs;
- tie(A, B, pairs) = data;
- // TODO
- return {6, 4};
+ const int n = static_cast(data.size());
+ if (n == 0) return {};
+ // copy and sort
+ auto s = data;
+ std::sort(s.begin(), s.end());
+ output_type b(n);
+ const int half = n / 2;
+ // Interleave: small, large, small2, large2, ...
+ for (int i = 0; i < half; ++i) {
+ b[2 * i] = s[i];
+ b[2 * i + 1] = s[i + half];
+ }
+ return b;
}
void output(const output_type &data) {
- for (auto &&dat: data) {
- cout << dat << end;
+ const int n = static_cast(data.size());
+ for (int i = 0; i < n; ++i) {
+ if (i) std::cout << ' ';
+ std::cout << data[i];
}
+ std::cout << end;
}
static const auto faster_streams = [] {
diff --git a/algorithm/2021F/lab_03/lab_03_D/resource/01.data.in b/algorithm/2021F/lab_03/lab_03_D/resource/01.data.in
new file mode 100644
index 00000000..d2cac55f
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_D/resource/01.data.in
@@ -0,0 +1,2 @@
+6
+3 2 1 4 5 6
diff --git a/algorithm/2021F/lab_03/lab_03_D/resource/01.data.out b/algorithm/2021F/lab_03/lab_03_D/resource/01.data.out
new file mode 100644
index 00000000..35eb7db0
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_D/resource/01.data.out
@@ -0,0 +1 @@
+1 4 2 5 3 6
diff --git a/algorithm/2021F/lab_03/lab_03_D/test.cpp b/algorithm/2021F/lab_03/lab_03_D/test.cpp
new file mode 100644
index 00000000..7f2cbc69
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_D/test.cpp
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_D/resource/"; }
+
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_03_D {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 03 D]") {
+ CS203_sequence sequence{1, 1, 2}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/lab_03_E/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_E/CMakeLists.txt
index c43d3c98..f6252ecc 100644
--- a/algorithm/2021F/lab_03/lab_03_E/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_E/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_E/README.md b/algorithm/2021F/lab_03/lab_03_E/README.md
new file mode 100644
index 00000000..906a1ca0
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_E/README.md
@@ -0,0 +1,53 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_E
+
+## Description
+
+You are given a random sequence $\{a\}$ of $n$ distinct elements.
+
+You can swap adjacent elements, and the cost of swapping $(a_i,a_{i+1})$ equals $\min(a_i,a_{i+1})$.
+
+Compute the minimum total cost to sort the sequence in ascending order by performing adjacent swaps.
+
+## Input
+
+The first line contains an integer $n$.
+
+The second line contains $n$ integers $a_1,a_2,\dots,a_n$.
+
+Constraints: $1 \le n \le 10^5$, $0 \le a_i \le 10^9$.
+
+## Output
+
+Output a single integer: the minimum total cost required to sort the sequence.
+
+## Sample
+
+### Sample Input
+
+```text
+5
+1 3 2 4 5
+```
+
+### Sample Output
+
+```text
+2
+```
+
+## 复用信息
+
++ Contest 1095:CS203 2021 Fall Lab 03 Sorting
++ Contest 1096:CS217 2021 Fall Lab 03 Sorting
++ Contest 1163:CS203 2024 Fall Lab 2
+
+## Algorithm Analysis (实现说明)
+
++ 思路: 题目要求通过相邻交换把数组排序, 交换 (a_i, a_{i+1}) 的代价为 min(a_i, a_{i+1})。将数组排序所需的代价等于对每次把较大的元素向右移动时累加被它跨越的较小元素的值。一个等价的视角是: 对于当前元素 x,考虑其左侧比 x 大的元素的个数 cnt,大元素每次与 x 交换时的最小值贡献为 x,因此该元素对答案的贡献为 x * cnt。
++ 实现要点: 先做坐标压缩得到秩 rank;使用 Fenwick 树维护已遍历元素的计数。遍历原数组,从左到右查询当前值右侧的已出现更大的元素数 greater = i - leq(其中 leq 是 <= 当前值 的计数),把 a[i] * greater 累加到答案,再把当前值加入 Fenwick 树。
++ 复杂度: 坐标压缩 O(n log n),遍历与 Fenwick 操作 O(n log n)。总体 O(n log n),空间 O(n)。
++ 边界情况: n<=1 时直接返回 0;重复元素的处理通过压缩与 <= 计数保证正确性。
diff --git a/algorithm/2021F/lab_03/lab_03_E/lab_03_E.cpp b/algorithm/2021F/lab_03/lab_03_E/lab_03_E.cpp
deleted file mode 100644
index 75c4a0a6..00000000
--- a/algorithm/2021F/lab_03/lab_03_E/lab_03_E.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-
-#ifdef ALGORITHM_TEST_MACRO
-namespace lab_03_E{
-#endif
-
-using std::cin;
-using std::tie;
-using std::cout;
-using std::list;
-using std::sort;
-using std::array;
-using std::deque;
-using std::queue;
-using std::stack;
-using std::tuple;
-using std::string;
-using std::vector;
-using std::unordered_map;
-using std::unordered_set;
-using std::priority_queue;
-static constexpr const char end{'\n'};
-
-using num_t = int32_t;
-using input_type = tuple;
-using output_type = num_t;
-
-inline input_type read();
-
-output_type cal(input_type data);
-
-void output(const output_type &data);
-
-int main() {
- auto input_data = read();
- auto output_data = cal(input_data);
- output(output_data);
- return 0;
-}
-
-inline input_type read() {
- num_t a{0}, b{0};
- std::cin >> a >> b;
- return std::make_tuple(a, b);
-}
-
-output_type cal(input_type data) {
- num_t a{0}, b{0};
- tie(a, b) = data;
- num_t c = a + b;
- return c;
-}
-
-void output(const output_type &data) {
- cout << data << end;
-}
-
-static const auto faster_streams = [] {
- srand(time(nullptr));
- // use time to init the random seed
- std::ios::sync_with_stdio(false);
- std::istream::sync_with_stdio(false);
- std::ostream::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::cout.tie(nullptr);
- // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
- return 0;
-}();
-#ifdef ALGORITHM_TEST_MACRO
-}
-#endif
diff --git a/algorithm/2021F/lab_03/lab_03_E/lab_03_E_test.cpp b/algorithm/2021F/lab_03/lab_03_E/lab_03_E_test.cpp
deleted file mode 100644
index 2ed5a30f..00000000
--- a/algorithm/2021F/lab_03/lab_03_E/lab_03_E_test.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-#ifdef ALGORITHM_TEST_MACRO
-
-#include
-#include
-#include
-#include
-#include
-
-#include "lab_03_E.cpp"
-
-std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_E/resource/"; }
-
-
-const std::string CS203_redirect::file_paths = getFilePath();
-
-namespace lab_03_E {
-
-using std::tie;
-using std::cin;
-using std::cout;
-using std::tuple;
-using std::vector;
-
-using Catch::Matchers::Equals;
-using Catch::Matchers::UnorderedEquals;
-using Catch::Matchers::Contains;
-
-TEST_CASE("test case 1", "[test 03 E]") {
- const auto output_data = cal(std::make_tuple(114, 514));
- CHECK(output_data == 628);
- CHECK(1 + 2 == 3);
- vector vec{2, 7, 11, 15};
- SECTION("CHECK_THAT 1") {
- CHECK_THAT(vec, Contains({2}));
- }SECTION("vec matcher") {
- CHECK_THAT(vec, UnorderedEquals({15, 11, 7, 2}));
- }
-}
-// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
-TEST_CASE("test case with sequence", "[test 03 E][.]") {
- CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1]
- sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
- sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
- sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
- const auto files_name = sequence.get_files(true);
- // 获取一个std::tuple ,
- // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
- for (const auto &file_name: files_name) {
- string datain, dataout, testout; // 声明
- tie(datain, dataout, testout) = file_name; // 解包
- {
- const CS203_redirect cr{datain, testout}; // 重定向输入,输出
- main();
- // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
- }
- CHECK(compareFiles(testout, dataout));
- }
-}
-}
-#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/lab_03_E/main.cpp b/algorithm/2021F/lab_03/lab_03_E/main.cpp
new file mode 100644
index 00000000..881b9167
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_E/main.cpp
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#ifndef ALGORITHM_TEST_MACRO
+#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
+#pragma GCC optimize("inline-small-functions")
+#pragma GCC optimize("-finline-small-functions")
+#pragma GCC target("tune=native")
+#else
+namespace lab_03_E{
+#endif
+
+using std::cin;
+using std::tie;
+using std::cout;
+using std::list;
+using std::sort;
+using std::array;
+using std::deque;
+using std::queue;
+using std::stack;
+using std::tuple;
+using std::string;
+using std::vector;
+using std::unordered_map;
+using std::unordered_set;
+using std::priority_queue;
+static constexpr const char end{'\n'};
+
+using i32 = int32_t;
+using i64 = int64_t;
+
+using input_type = std::pair>;
+using output_type = i64;
+
+inline input_type read_input();
+
+output_type cal(const input_type &data);
+
+void output(const output_type &data);
+
+int main() {
+ const auto input_data = read_input();
+ const auto output_data = cal(input_data);
+ output(output_data);
+ return 0;
+}
+
+inline input_type read_input() {
+ i32 n{0};
+ std::cin >> n;
+ vector a;
+ a.resize(n);
+ for (i32 i = 0; i < n; ++i) std::cin >> a[i];
+ return {n, a};
+}
+
+// Fenwick tree for counts
+struct Fenwick {
+ int n;
+ vector bit;
+ Fenwick(int _n = 0) { init(_n); }
+ void init(int _n) { n = _n; bit.assign(n+1, 0); }
+ void add(int idx, i32 val) {
+ for (; idx <= n; idx += idx & -idx) bit[idx] += val;
+ }
+ i32 sumPrefix(int idx) const {
+ i32 r = 0;
+ for (; idx > 0; idx -= idx & -idx) r += bit[idx];
+ return r;
+ }
+};
+
+output_type cal(const input_type &data) {
+ const i32 n = data.first;
+ const auto &a = data.second;
+ if (n <= 1) return 0;
+ // coordinate compression
+ vector vals = a;
+ std::sort(vals.begin(), vals.end());
+ vals.erase(std::unique(vals.begin(), vals.end()), vals.end());
+ Fenwick fw(static_cast(vals.size()));
+ i64 ans = 0;
+ for (i32 i = 0; i < n; ++i) {
+ int rk = static_cast(std::lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()) + 1; // 1-based
+ i32 leq = fw.sumPrefix(rk); // number of previous <= a[i]
+ i32 prev = i; // number of previous elements
+ i32 greater = prev - leq; // previous elements > a[i]
+ ans += a[i] * static_cast(greater);
+ fw.add(rk, 1);
+ }
+ return ans;
+}
+
+void output(const output_type &data) {
+ cout << data << end;
+}
+
+static const auto faster_streams = [] {
+ srand(time(nullptr));
+ // use time to init the random seed
+ std::ios::sync_with_stdio(false);
+ std::istream::sync_with_stdio(false);
+ std::ostream::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cout.tie(nullptr);
+ // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
+ return 0;
+}();
+#ifdef ALGORITHM_TEST_MACRO
+}
+#endif
diff --git a/algorithm/2021F/lab_03/lab_03_E/resource/01.data.in b/algorithm/2021F/lab_03/lab_03_E/resource/01.data.in
new file mode 100644
index 00000000..a0aa4402
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_E/resource/01.data.in
@@ -0,0 +1,2 @@
+5
+1 3 2 4 5
diff --git a/algorithm/2021F/lab_03/lab_03_E/resource/01.data.out b/algorithm/2021F/lab_03/lab_03_E/resource/01.data.out
new file mode 100644
index 00000000..0cfbf088
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_E/resource/01.data.out
@@ -0,0 +1 @@
+2
diff --git a/algorithm/2021F/lab_03/lab_03_E/test.cpp b/algorithm/2021F/lab_03/lab_03_E/test.cpp
new file mode 100644
index 00000000..3c831882
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_E/test.cpp
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_E/resource/"; }
+
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_03_E {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 03 E]") {
+ CS203_sequence sequence{1, 1, 2}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/lab_03_F/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_F/CMakeLists.txt
index 7bd14a82..3bd8bb2b 100644
--- a/algorithm/2021F/lab_03/lab_03_F/CMakeLists.txt
+++ b/algorithm/2021F/lab_03/lab_03_F/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_F/README.md b/algorithm/2021F/lab_03/lab_03_F/README.md
new file mode 100644
index 00000000..2bcee0e8
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_F/README.md
@@ -0,0 +1,65 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_F
+
+## Description
+
+You have $n$ plants.
+
+The $i$-th plant has height $h_i$ and strength $s_i$.
+
+There are two fertilizers: $F_h$ and $F_s$.
+
+Applying one $F_h$ to a plant doubles its height.
+
+Applying one $F_s$ sets the plant's strength equal to its height.
+
+You may use at most $p$ bags of $F_h$ and at most $q$ bags of $F_s$.
+
+Maximize the sum of strengths $\sum s_i$ after applying at most $p$ of $F_h$ and at most $q$ of $F_s$.
+
+## Input
+
+The first line contains three integers $n, p, q$.
+
+Then follow $n$ lines.
+
+Each of the next $n$ lines contains two integers $h_i, s_i$.
+
+Constraints: $1 \le n \le 2\cdot 10^5$, $0 \le p \le 20$, $0 \le q \le 2\cdot 10^5$, $1 \le h_i, s_i \le 10^9$.
+
+## Output
+
+Print one integer: the maximum possible sum $\sum s_i$.
+
+## Sample
+
+### Sample Input
+
+```text
+2 1 1
+10 8
+6 1
+```
+
+### Sample Output
+
+```text
+21
+```
+
+## 复用信息
+
++ Contest 1095:CS203 2021 Fall Lab 03 Sorting
++ Contest 1096:CS217 2021 Fall Lab 03 Sorting
++ Contest 1137:CS203 2023 Fall Lab 2 Sorting
++ Contest 1187:CS203 2025 Fall Lab 2 Sorting
+
+## Algorithm Analysis (实现说明)
+
++ 思路: 将两种操作(对高度使用 F_h 翻倍, 使用 F_s 将强度设为高度)组合来最大化总强度。先把每个植物不使用额外肥料时的基线强度 base = sum s_i 记下。对每个植物,定义 g0 = max(h_i - s_i, 0) 表示使用 F_s 可带来的净增量。使用最多 q 个 F_s 时,优先把 q 个 g0 最大的植物设为 s= h。
++ 对于最多 p 次将某一植物的高度翻倍的操作(p<=20),可以枚举把所有 p 放在某一植物上的情况: 计算该植物翻倍后的 g1 = max(h_i * 2^p - s_i, 0),并把 g0 的排序中替换原位置后计算前 q 个的和得到候选值。取所有枚举中的最大值。为高效实现, 先对 g0 做降序排序并构建前缀和; 枚举时用二分/插入位置快速计算替换后前 q 个之和。
++ 复杂度: 排序 O(n log n);枚举 n 个位置并在 O(log n) 或 O(1) 时间内计算新前缀和,总体 O(n log n)。空间 O(n)。
++ 边界情况: p=0 时相当于只使用 F_s;q=0 时只考虑翻倍的增益;当 q>n 时取 n 个最大增益。
diff --git a/algorithm/2021F/lab_03/lab_03_F/lab_03_F_test.cpp b/algorithm/2021F/lab_03/lab_03_F/lab_03_F_test.cpp
deleted file mode 100644
index eed57d8f..00000000
--- a/algorithm/2021F/lab_03/lab_03_F/lab_03_F_test.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-#ifdef ALGORITHM_TEST_MACRO
-
-#include
-#include
-#include
-#include
-#include
-
-#include "lab_03_F.cpp"
-
-std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_F/resource/"; }
-
-
-const std::string CS203_redirect::file_paths = getFilePath();
-
-namespace lab_03_F {
-
-using std::tie;
-using std::cin;
-using std::cout;
-using std::tuple;
-using std::vector;
-
-using Catch::Matchers::Equals;
-using Catch::Matchers::UnorderedEquals;
-using Catch::Matchers::Contains;
-
-TEST_CASE("test case 1", "[test 03 F]") {
- const auto output_data = cal(std::make_tuple(114, 514));
- CHECK(output_data == 628);
- CHECK(1 + 2 == 3);
- vector vec{2, 7, 11, 15};
- SECTION("CHECK_THAT 1") {
- CHECK_THAT(vec, Contains({2}));
- }SECTION("vec matcher") {
- CHECK_THAT(vec, UnorderedEquals({15, 11, 7, 2}));
- }
-}
-// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
-TEST_CASE("test case with sequence", "[test 03 F][.]") {
- CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1]
- sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
- sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
- sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
- const auto files_name = sequence.get_files(true);
- // 获取一个std::tuple ,
- // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
- for (const auto &file_name: files_name) {
- string datain, dataout, testout; // 声明
- tie(datain, dataout, testout) = file_name; // 解包
- {
- const CS203_redirect cr{datain, testout}; // 重定向输入,输出
- main();
- // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
- }
- CHECK(compareFiles(testout, dataout));
- }
-}
-}
-#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/lab_03_F/main.cpp b/algorithm/2021F/lab_03/lab_03_F/main.cpp
new file mode 100644
index 00000000..c5752bd5
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_F/main.cpp
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#ifndef ALGORITHM_TEST_MACRO
+#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
+#pragma GCC optimize("inline-small-functions")
+#pragma GCC optimize("-finline-small-functions")
+#pragma GCC target("tune=native")
+#else
+namespace lab_03_F{
+#endif
+
+using std::cin;
+using std::tie;
+using std::cout;
+using std::pair;
+using std::list;
+using std::sort;
+using std::array;
+using std::deque;
+using std::queue;
+using std::stack;
+using std::tuple;
+using std::string;
+using std::vector;
+using std::unordered_map;
+using std::unordered_set;
+using std::priority_queue;
+static constexpr const char end{'\n'};
+
+using i32 = int32_t;
+using i64 = int64_t;
+
+using input_type = tuple, vector>; // n,p,q,h[],s[]
+using output_type = i64;
+
+inline input_type read_input();
+
+output_type cal(const input_type &data);
+
+void output(const output_type &data);
+
+int main() {
+ const auto input_data = read_input();
+ const auto output_data = cal(input_data);
+ output(output_data);
+ return 0;
+}
+
+inline input_type read_input() {
+ i32 n; i32 p; i32 q;
+ std::cin >> n >> p >> q;
+ vector h(n), s(n);
+ for (i32 i = 0; i < n; ++i) std::cin >> h[i] >> s[i];
+ return std::make_tuple(n, p, q, h, s);
+}
+
+output_type cal(const input_type &data) {
+ i32 n, p, q;
+ vector h, s;
+ tie(n, p, q, h, s) = data;
+ i64 base = 0;
+ vector g0(n);
+ for (i32 i = 0; i < n; ++i) {
+ base += s[i];
+ g0[i] = std::max(h[i] - s[i], 0);
+ }
+ i32 cnt = std::min(q, n);
+ // sort g0 descending with indices
+ vector> vec;
+ vec.reserve(n);
+ for (int i = 0; i < n; ++i) vec.emplace_back(g0[i], i);
+ sort(vec.begin(), vec.end(), [](const pair&a, const pair&b){
+ if (a.first != b.first) return a.first > b.first;
+ return a.second < b.second;
+ });
+ vector gs(n);
+ vector pos(n);
+ for (int i = 0; i < n; ++i) {
+ gs[i] = vec[i].first;
+ pos[ vec[i].second ] = i;
+ }
+ vector pref(n+1, 0);
+ for (int i = 0; i < n; ++i) pref[i+1] = pref[i] + gs[i];
+
+ i64 best = base + pref[cnt];
+
+ // Precompute multiplier 2^p
+ i64 mul = 1;
+ for (int i = 0; i < p; ++i) mul <<= 1;
+
+ for (int idx = 0; idx < n; ++idx) {
+ // value if we apply all p F_h to idx
+ i64 hi = h[idx] * mul;
+ i64 g1 = std::max(hi - s[idx], 0);
+ // compute insertion position in gs (descending)
+ auto it = std::lower_bound(gs.begin(), gs.end(), g1, std::greater());
+ int pos_insert = static_cast(it - gs.begin());
+ int orig_pos = pos[idx];
+ int pos_excl = (pos_insert <= orig_pos) ? pos_insert : pos_insert - 1;
+
+ // sum_top_excl: sum of top cnt values after removing original g0[idx]
+ i64 sum_top_excl = 0;
+ if (cnt == 0) {
+ sum_top_excl = 0;
+ } else if (orig_pos >= cnt) {
+ sum_top_excl = pref[cnt];
+ } else {
+ // orig_pos < cnt
+ sum_top_excl = pref[cnt] - gs[orig_pos];
+ if (cnt < n) sum_top_excl += gs[cnt];
+ }
+
+ i64 total_gain_top = 0;
+ if (pos_excl >= cnt) {
+ total_gain_top = sum_top_excl;
+ } else {
+ // g1 enters top cnt, drop element at index cnt-1 of multiset_excl
+ i64 drop = 0;
+ if (cnt == 0) drop = 0;
+ else if (orig_pos >= cnt) {
+ drop = gs[cnt-1];
+ } else {
+ // orig_pos < cnt
+ if (cnt < n) drop = gs[cnt]; else drop = 0;
+ }
+ total_gain_top = sum_top_excl + g1 - drop;
+ }
+
+ i64 candidate = base + total_gain_top;
+ if (candidate > best) best = candidate;
+ }
+
+ return best;
+}
+
+void output(const output_type &data) {
+ cout << data << end;
+}
+
+static const auto faster_streams = [] {
+ srand(time(nullptr));
+ // use time to init the random seed
+ std::ios::sync_with_stdio(false);
+ std::istream::sync_with_stdio(false);
+ std::ostream::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cout.tie(nullptr);
+ // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
+ return 0;
+}();
+#ifdef ALGORITHM_TEST_MACRO
+}
+#endif
diff --git a/algorithm/2021F/lab_03/lab_03_F/resource/01.data.in b/algorithm/2021F/lab_03/lab_03_F/resource/01.data.in
new file mode 100644
index 00000000..8b31598c
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_F/resource/01.data.in
@@ -0,0 +1,3 @@
+2 1 1
+10 8
+6 1
diff --git a/algorithm/2021F/lab_03/lab_03_F/resource/01.data.out b/algorithm/2021F/lab_03/lab_03_F/resource/01.data.out
new file mode 100644
index 00000000..aabe6ec3
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_F/resource/01.data.out
@@ -0,0 +1 @@
+21
diff --git a/algorithm/2021F/lab_03/lab_03_F/test.cpp b/algorithm/2021F/lab_03/lab_03_F/test.cpp
new file mode 100644
index 00000000..716d221a
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_F/test.cpp
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_F/resource/"; }
+
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_03_F {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 03 F]") {
+ CS203_sequence sequence{1, 1, 2}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_03/lab_03_G/CMakeLists.txt b/algorithm/2021F/lab_03/lab_03_G/CMakeLists.txt
new file mode 100644
index 00000000..768dc1e2
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_G/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.16.6)
+set(PROBLEM_ORDER G)
+project(${PROJECT_NAME}_${PROBLEM_ORDER})
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+
+enable_testing()
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
+set(CMAKE_CXX_STANDARD 17)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
+target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
+target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
+add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
+set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_03/lab_03_G/README.md b/algorithm/2021F/lab_03/lab_03_G/README.md
new file mode 100644
index 00000000..4e4a561c
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_G/README.md
@@ -0,0 +1,58 @@
+---
+SPDX-License-Identifier: CC-BY-NC-SA-4.0
+---
+
+# lab_03_G
+
+## Description
+
+There are too many cables under your table, and you want to tidy them.
+
+You designed a simple cabling scheme: There are n cables, each cable is a line in form of $y=k_i∗x+b_i$, your table is the region between $x=x1$ and $x=x2$
+
+If any two cables intersect on your table region (not include the edges), your table is not tidy and you need to design another scheme.
+
+More formally, there are n lines in form of $y=k_i∗x+b_i$, if there are two lines intersect on $(x_p,y_p)$, and $x1
#ifdef ALGORITHM_TEST_MACRO
-namespace lab_03_F{
+namespace lab_03_G{
#endif
using std::cin;
@@ -37,8 +37,17 @@ using std::priority_queue;
static constexpr const char end{'\n'};
using num_t = int32_t;
-using input_type = tuple;
-using output_type = num_t;
+using i64 = int64_t;
+
+struct Input {
+ int32_t n;
+ i64 x1;
+ i64 x2;
+ vector > lines; // (k, b)
+};
+
+using input_type = Input;
+using output_type = string;
inline input_type read();
@@ -54,16 +63,52 @@ int main() {
}
inline input_type read() {
- num_t a{0}, b{0};
- std::cin >> a >> b;
- return std::make_tuple(a, b);
+ Input in;
+ int32_t n;
+ i64 x1, x2;
+ cin >> n >> x1 >> x2;
+ in.n = n;
+ in.x1 = x1;
+ in.x2 = x2;
+ in.lines.reserve(static_cast(n));
+ for (int i = 0; i < n; ++i) {
+ i64 k, b;
+ cin >> k >> b;
+ in.lines.emplace_back(k, b);
+ }
+ return in;
}
output_type cal(input_type data) {
- num_t a{0}, b{0};
- tie(a, b) = data;
- num_t c = a + b;
- return c;
+ const int n = data.n;
+ const i64 x1 = data.x1;
+ const i64 x2 = data.x2;
+ // compute values at x1 and x2
+ vector > ys;
+ ys.reserve(static_cast(n));
+ for (int i = 0; i < n; ++i) {
+ const i64 k = data.lines[i].first;
+ const i64 b = data.lines[i].second;
+ const i64 y1 = k * x1 + b;
+ const i64 y2 = k * x2 + b;
+ ys.emplace_back(y1, y2);
+ }
+
+ // order indices by (y1, y2) to ensure ties at x1 are resolved by x2
+ vector ord(n);
+ for (int i = 0; i < n; ++i) ord[i] = i;
+ sort(ord.begin(), ord.end(), [&](int a, int b) {
+ if (ys[a].first != ys[b].first) return ys[a].first < ys[b].first;
+ return ys[a].second < ys[b].second;
+ });
+
+ // check for inversion in y2 sequence
+ for (int i = 0; i + 1 < n; ++i) {
+ const i64 y2_cur = ys[ord[i]].second;
+ const i64 y2_nxt = ys[ord[i + 1]].second;
+ if (y2_cur > y2_nxt) return string("YES");
+ }
+ return string("NO");
}
void output(const output_type &data) {
diff --git a/algorithm/2021F/lab_03/lab_03_G/resource/01.data.in b/algorithm/2021F/lab_03/lab_03_G/resource/01.data.in
new file mode 100644
index 00000000..5b9cc938
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_G/resource/01.data.in
@@ -0,0 +1,4 @@
+2
+1 3
+1 0
+-1 3
diff --git a/algorithm/2021F/lab_03/lab_03_G/resource/01.data.out b/algorithm/2021F/lab_03/lab_03_G/resource/01.data.out
new file mode 100644
index 00000000..f033a501
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_G/resource/01.data.out
@@ -0,0 +1 @@
+YES
diff --git a/algorithm/2021F/lab_03/lab_03_G/test.cpp b/algorithm/2021F/lab_03/lab_03_G/test.cpp
new file mode 100644
index 00000000..ea444cc9
--- /dev/null
+++ b/algorithm/2021F/lab_03/lab_03_G/test.cpp
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2020-2025 nanoseeds
+#ifdef ALGORITHM_TEST_MACRO
+
+#include
+#include
+#include
+#include
+#include
+
+#include "main.cpp"
+
+std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_03/lab_03_G/resource/"; }
+
+
+const std::string CS203_redirect::file_paths = getFilePath();
+
+namespace lab_03_G {
+
+using std::tie;
+using std::cin;
+using std::cout;
+using std::tuple;
+using std::vector;
+
+using Catch::Matchers::Equals;
+using Catch::Matchers::UnorderedEquals;
+using Catch::Matchers::Contains;
+
+// 因为[.],所以下面这个被隐藏了,确保需要重定向输入输出时,请删除`[.]`
+TEST_CASE("test case with sequence", "[test 03 F]") {
+ CS203_sequence sequence{1, 1, 2}; // // 基础设定,[1,1]
+ sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in
+ sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out
+ sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out
+ const auto files_name = sequence.get_files(true);
+ // 获取一个std::tuple ,
+ // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名.
+ for (const auto &file_name: files_name) {
+ string datain, dataout, testout; // 声明
+ tie(datain, dataout, testout) = file_name; // 解包
+ {
+ const CS203_redirect cr{datain, testout}; // 重定向输入,输出
+ main();
+ // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向
+ }
+ CHECK(compareFiles(testout, dataout));
+ }
+}
+}
+#endif //ALGORITHM_TEST_MACRO
diff --git a/algorithm/2021F/lab_04/CMakeLists.txt b/algorithm/2021F/lab_04/CMakeLists.txt
index ebb3bfc9..74cdbbc1 100644
--- a/algorithm/2021F/lab_04/CMakeLists.txt
+++ b/algorithm/2021F/lab_04/CMakeLists.txt
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-set(dependencies A 2A B C D) # E F
+set(dependencies A 2A B C D E F G) # E F
foreach (elementName IN LISTS dependencies)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${elementName})
endforeach ()
diff --git a/algorithm/2021F/lab_04/README.md b/algorithm/2021F/lab_04/README.md
new file mode 100644
index 00000000..bca1bbb1
--- /dev/null
+++ b/algorithm/2021F/lab_04/README.md
@@ -0,0 +1,33 @@
+# CS203 2021 Fall — Lab 04: LinkedList
+
+``` meta
+cid=1098
+```
+
+Welcome to CS203 2021 Fall Lab 04! This lab focuses on linked-list related problems.
+
+There are seven problems in this lab. Read samples and hints carefully — they often contain important details.
+
+Score:
+
+- A: 10
+- B: 15/10
+- C: 15/10
+- D: 20/15
+- E: 20/15
+- F: 20
+- G: undefined/20
+
+## Problems
+
+| Problem | problem id |
+|---:|---:|
+| A | 1435 |
+| B | 1436 |
+| C | 1442 |
+| D | 1439 |
+| E | 1440 |
+| F | 1438 |
+| G | 1441 |
+
+A, D, E题被复用
diff --git a/algorithm/2021F/lab_04/cs203.submit.csv b/algorithm/2021F/lab_04/cs203.submit.csv
new file mode 100644
index 00000000..74ce7906
--- /dev/null
+++ b/algorithm/2021F/lab_04/cs203.submit.csv
@@ -0,0 +1,8 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 277, 1, 360, 339, 1, 14, 187, 90, 1269, 22, 404, 843
+B, 247, 1, 239, 339, 7, 74, 108, 1015, 32, 148, 835
+C, 229, 197, 271, 29, 104, 117, 44, 991, 31, 111, 849
+D, 242, 1, 364, 500, 16, 50, 64, 62, 1299, 11, 239, 1049
+E, 166, 73, 381, 602, 13, 492, 369, 76, 2172, 42, 405, 1725
+F, 141, 1, 129, 400, 27, 13, 203, 37, 951, 3, 253, 695
+Total, 1302, 77, 1670, 2451, 86, 680, 1014, 417, 7697, 141, 1560, 5996
diff --git a/algorithm/2021F/lab_04/cs217.submit.csv b/algorithm/2021F/lab_04/cs217.submit.csv
new file mode 100644
index 00000000..30ba0bd0
--- /dev/null
+++ b/algorithm/2021F/lab_04/cs217.submit.csv
@@ -0,0 +1,9 @@
+problem, AC, PE, WA, TLE, MLE, OLE, RE, CE, TR, Total , C, C++, Java
+A, 46, 58, 51, 14, 14, 183, 6, 115, 62
+B, 53, 1, 15, 20, 4, 8, 101, 6, 42, 53
+C, 44, 13, 18, 1, 6, 1, 2, 85, 14, 23, 48
+D, 55, 8, 42, 6, 22, 11, 144, 12, 67, 65
+E, 43, 38, 46, 4, 66, 21, 9, 227, 1, 83, 143
+F, 68, 22, 59, 11, 1, 16, 9, 186, 119, 67
+G, 65, 60, 32, 15, 7, 179, 3, 98, 78
+Total, 374, 1, 214, 268, 16, 79, 93, 60, 1105, 42, 547, 516
diff --git a/algorithm/2021F/lab_04/lab_04_2A/CMakeLists.txt b/algorithm/2021F/lab_04/lab_04_2A/CMakeLists.txt
index a78119fe..da52f0d3 100644
--- a/algorithm/2021F/lab_04/lab_04_2A/CMakeLists.txt
+++ b/algorithm/2021F/lab_04/lab_04_2A/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_04/lab_04_2A/lab_04_2A.cpp b/algorithm/2021F/lab_04/lab_04_2A/main.cpp
similarity index 100%
rename from algorithm/2021F/lab_04/lab_04_2A/lab_04_2A.cpp
rename to algorithm/2021F/lab_04/lab_04_2A/main.cpp
diff --git a/algorithm/2021F/lab_04/lab_04_2A/lab_04_2A_test.cpp b/algorithm/2021F/lab_04/lab_04_2A/test.cpp
similarity index 98%
rename from algorithm/2021F/lab_04/lab_04_2A/lab_04_2A_test.cpp
rename to algorithm/2021F/lab_04/lab_04_2A/test.cpp
index 70c6c80c..ced558de 100644
--- a/algorithm/2021F/lab_04/lab_04_2A/lab_04_2A_test.cpp
+++ b/algorithm/2021F/lab_04/lab_04_2A/test.cpp
@@ -8,7 +8,7 @@
#include
#include
-#include "lab_04_2A.cpp"
+#include "main.cpp"
std::string getFilePath() noexcept { return "./../../../../../algorithm/2021F/lab_04/lab_04_A/resource/"; }
diff --git a/algorithm/2021F/lab_04/lab_04_A/CMakeLists.txt b/algorithm/2021F/lab_04/lab_04_A/CMakeLists.txt
index 354751e1..40897f09 100644
--- a/algorithm/2021F/lab_04/lab_04_A/CMakeLists.txt
+++ b/algorithm/2021F/lab_04/lab_04_A/CMakeLists.txt
@@ -6,11 +6,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
enable_testing()
-add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}.cpp)
+add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(CMAKE_CXX_STANDARD 17)
-add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp)
+add_executable(${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE ALGORITHM_TEST_MACRO)
target_link_libraries(${PROJECT_NAME}_test PRIVATE algorithm_template_INCLUDE)
-MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_ORDER}_${PROBLEM_ORDER}_test.cpp")
+MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp")
add_test(${PROJECT_NAME}_CTEST ${PROJECT_NAME}_test)
set(CMAKE_CXX_STANDARD 11)
diff --git a/algorithm/2021F/lab_04/lab_04_A/README.md b/algorithm/2021F/lab_04/lab_04_A/README.md
new file mode 100644
index 00000000..862d5f3d
--- /dev/null
+++ b/algorithm/2021F/lab_04/lab_04_A/README.md
@@ -0,0 +1,88 @@
+# lab_04_A — Polynomial Sum
+
+## Description
+
+Given two polynomials with n and m terms, please calculate the sum of the two polynomials.
+
+Inputs and outputs are exponentially larger to smaller
+
+### Input
+
+The first line has two numbers: n and m $(0` 是一个理想的选择. 在这个映射中:
+
++ 键 (Key): 存储多项式的 指数 (exponent).
++ 值 (Value): 存储对应项的 系数 (coefficient).
+
+`std::map` 的主要优势在于它是一个基于红黑树的有序关联容器, 它会根据键(在此即指数)自动进行升序排序. 这极大地简化了后续按指数顺序输出的需求.
+
+### 算法流程
+
+1. 初始化: 创建一个 `std::map` 实例, 用于存放最终合并后的多项式.
+
+2. 读取多项式:
+ + 遍历第一个多项式的所有项. 对于每一个读入的 "系数-指数" 对, 将其存入 map 中. `map[指数] = 系数`.
+ + 遍历第二个多项式的所有项. 对于每一个读入的 "系数-指数" 对, 将其系数累加到 map 中对应指数的项上. `map[指数] += 系数`. `std::map` 的 `[]` 操作符在这里非常方便: 如果 map 中已存在该指数, 它会直接访问并允许修改对应的系数值; 如果不存在, 它会自动创建一个新条目, 并将系数初始化为0, 然后再执行加法.
+
+3. 处理零系数项: 在合并过程中, 某些项的系数可能变为零. `std::map` 中会保留这些条目. 虽然本题的实现没有显式移除它们, 但在输出时可以根据需要进行过滤.
+
+4. 输出结果:
+ + 首先, 输出 map 的大小 `map.size()`, 这代表了结果多项式的项数.
+ + 由于 `std::map` 按键(指数)升序存储, 而题目要求按指数降序输出, 因此需要使用反向迭代器 (`crbegin()` 到 `crend()`) 来遍历 map.
+ + 在遍历过程中, 打印每个项的系数和指数. 代码中包含一个 `if (iter->first != 0)` 的判断, 这意味着指数为0的项(即常数项)不会被输出. 根据题目要求和样例, 这可能是特定题设下的特殊规则.
+
+这种方法利用了 `std::map` 的特性, 以简洁高效的方式完成了多项式的合并与排序, 完全符合 `AGENTS.md` 中提倡的利用标准库算法和数据结构的原则.
diff --git a/algorithm/2021F/lab_04/lab_04_A/lab_04_A.cpp b/algorithm/2021F/lab_04/lab_04_A/main.cpp
similarity index 78%
rename from algorithm/2021F/lab_04/lab_04_A/lab_04_A.cpp
rename to algorithm/2021F/lab_04/lab_04_A/main.cpp
index 1202c376..1e1dcd4b 100644
--- a/algorithm/2021F/lab_04/lab_04_A/lab_04_A.cpp
+++ b/algorithm/2021F/lab_04/lab_04_A/main.cpp
@@ -1,31 +1,17 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2020-2025 nanoseeds
-/*
- *@题目描述
- * 给出两个各有ν(希腊字母Nu),μ(希腊字母Mu)长的多项式Ν(大写希腊字母Nu),Μ(大写希腊字母Mu),请计算两者之和
- *@输入
- * 第一行输入两个int32_t,(0,10^6)
- * 之后ν行输入,分别是Ν的 ${系数},${指数}
- * 之后μ行输入,分别是Μ的 ${系数},${指数}
- * 所有系数指数都在(-10^9,10^9)
- * PS: 输入全为int32_t
- *@输出
- * 第一行int32_t, 输出项数
- * 之后分行输出 ${系数},${指数}
- * */
// DONE
-#ifndef ALGORITHM_TEST_MACRO
-#pragma GCC optimize(3, "Ofast", "inline", "no-stack-protector", "unroll-loops")
-#pragma GCC optimize("inline-small-functions")
-#pragma GCC optimize("-finline-small-functions")
-#pragma GCC target("mmx")
-#endif
#include