diff --git a/.idea/editor.xml b/.idea/editor.xml
index 8019d75..0fea8c4 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -206,7 +206,7 @@
-
+
@@ -230,7 +230,7 @@
-
+
diff --git a/.idea/exercises.iml b/.idea/exercises.iml
index 4c94235..5bda40d 100644
--- a/.idea/exercises.iml
+++ b/.idea/exercises.iml
@@ -1,2 +1,10 @@
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
index 7d2682d..47ac8a5 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -3,6 +3,7 @@
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4ae2e93..7cf4251 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -21,9 +21,11 @@
+
+
@@ -31,13 +33,17 @@
+
+
+
+
@@ -48,20 +54,21 @@
-
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
@@ -137,14 +144,20 @@
"C/C++ 文件.pojcs101_M02692_12_coins.cpp.executor": "Run",
"C/C++ 文件.pojcs101_M02694_polish_notation_tree.cpp.executor": "Run",
"C/C++ 文件.pojcs101_M03247_palindromic_prime.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M03441_sum_zero.cpp.executor": "Run",
"CMake 应用程序.exercises.executor": "Run",
+ "CMake 应用程序.lc_135_candy.executor": "Run",
"CMake 应用程序.pojcs101_M02694_polish_notation_tree.executor": "Run",
+ "CMake 应用程序.pojcs101_M03441_sum_zero.executor": "Run",
+ "CMake 应用程序.pojcs101_M03532.executor": "Run",
+ "CMake 应用程序.pojcs101_M03704_brackets.executor": "Run",
"CMake 应用程序.pojcs101_T01011_sticks.executor": "Run",
"CMake 调试.exercises.executor": "Debug",
"CidrDebugProfileConversion": "true",
"ModuleVcsDetector.initialDetectionPerformed": "true",
"Python.lc_127.executor": "Run",
- "Python.pojcs101_M03247_palindromic_prime_init1.executor": "Run",
+ "Python.noi2001_food_chain_dsu.executor": "Run",
+ "Python.pojcs101_M03247_palindromic_prime_init1.executor": "Debug",
"RunOnceActivity.RadMigrateCodeStyle": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.cidr.known.project.marker": "true",
@@ -170,7 +183,12 @@
-
+
+
+
+
+
+
@@ -196,7 +214,7 @@
-
+
@@ -211,6 +229,11 @@
+
+
+
+
+
@@ -311,17 +334,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -348,11 +410,11 @@
-
-
+
+
@@ -371,13 +433,19 @@
+
+
+
+
+
+
-
+
@@ -399,6 +467,8 @@
1784800801763
+
+
@@ -419,15 +489,17 @@
-
- file://$PROJECT_DIR$/CMakeLists.txt
- 12
-
+
+ file://$PROJECT_DIR$/noi2001_food_chain_dsu.py
+ 75
+
+
+
+ file://$PROJECT_DIR$/pojcs101_M03441_sum_zero.cpp
+ 32
+
-
-
-
@@ -435,6 +507,7 @@
+
diff --git a/lc_135_candy.cpp b/lc_135_candy.cpp
new file mode 100644
index 0000000..8510b31
--- /dev/null
+++ b/lc_135_candy.cpp
@@ -0,0 +1,53 @@
+#include
+#include
+
+using namespace std;
+
+class Solution {
+ static vector get_upstairs_lengths(vector& ratings) {
+ vector upstairs(ratings.size(), 1);
+ for (int i = 1; i < ratings.size(); i++) {
+ if (ratings[i] > ratings[i - 1]) {
+ upstairs[i] = upstairs[i - 1] + 1;
+ }
+ }
+ return upstairs;
+ }
+
+ static vector get_downstairs_lengths(vector& ratings) {
+ vector downstairs(ratings.size(), 1);
+ for (int i = ratings.size() - 2; i >= 0; i--) {
+ if (ratings[i] > ratings[i + 1]) {
+ downstairs[i] = downstairs[i + 1] + 1;
+ }
+ }
+ return downstairs;
+ }
+
+public:
+ int candy(vector& ratings) {
+ vector upstairs = get_upstairs_lengths(ratings);
+ vector downstairs = get_downstairs_lengths(ratings);
+ int ans = 0;
+ for (int i = 0; i < ratings.size(); i++) {
+ ans += max(upstairs[i], downstairs[i]);
+ }
+ return ans;
+ }
+};
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ size_t ratings_amount;
+ cin >> ratings_amount;
+ vector ratings(ratings_amount);
+ for (int i = 0; i < ratings_amount; i++) {
+ cin >> ratings[i];
+ }
+ Solution solution;
+ cout << solution.candy(ratings) << endl;
+
+ return 0;
+}
diff --git a/noi2001_food_chain_dsu.py b/noi2001_food_chain_dsu.py
new file mode 100644
index 0000000..4b75616
--- /dev/null
+++ b/noi2001_food_chain_dsu.py
@@ -0,0 +1,77 @@
+import dataclasses
+import sys
+
+
+class Dsu:
+ @dataclasses.dataclass
+ class Node:
+ parent: int
+ tree_size: int = 1
+ weight: int = 0
+
+ def __init__(self, n: int):
+ self.nodes: list["Dsu.Node"] = [self.Node(i) for i in range(n)]
+
+ def find(self, x: int) -> int:
+ if self.nodes[x].parent == x:
+ return x
+ parent = self.find(self.nodes[x].parent)
+ self.nodes[x].weight \
+ = (self.nodes[self.nodes[x].parent].weight + self.nodes[x].weight + 12) % 3
+ self.nodes[x].parent = parent
+ return parent
+
+ def union(self, x: int, y: int, weight_offset: int):
+ """
+ x 的父亲为 y;x 的权重为 y 的权重 + weight_offset
+ :param x:
+ :param y:
+ :param weight_offset:
+ :return:
+ """
+ self.find(x)
+ self.find(y)
+ if x == y:
+ return
+ if self.nodes[x].tree_size > self.nodes[y].tree_size:
+ x, y = y, x
+ weight_offset = -weight_offset
+ wx, wy = self.nodes[x].weight, self.nodes[y].weight
+ weight_offset += wy - wx
+ x,y = self.nodes[x].parent, self.nodes[y].parent
+ self.nodes[x].parent = y
+ self.nodes[x].weight = weight_offset % 3
+ self.nodes[y].tree_size += self.nodes[x].tree_size
+
+
+if __name__ == '__main__':
+ animals_amount, instructions_amount = map(int, input().split())
+ dsu = Dsu(animals_amount)
+ lies_amount = 0
+ for _ in range(instructions_amount):
+ instruction_type, animal1, animal2 = map(int, input().split())
+ # print(instruction_type, animal1, animal2, file=sys.stderr)
+ if animal1 > animals_amount or animal2 > animals_amount:
+ print("Lie: No animal", file=sys.stderr)
+ lies_amount += 1
+ continue
+ if animal1 == animal2:
+ if instruction_type == 2:
+ print("Lie: Eats oneself", file=sys.stderr)
+ lies_amount += 1
+ continue
+ animal1 -= 1
+ animal2 -= 1
+ if dsu.find(animal1) == dsu.find(animal2):
+ w1 = dsu.nodes[animal1].weight
+ w2 = dsu.nodes[animal2].weight
+ if instruction_type == 1 and w1 != w2:
+ print("Lie: different type", w1, w2, file=sys.stderr)
+ lies_amount += 1
+ if instruction_type == 2 and (w1 - w2 + 12) % 3 != 1:
+ print("Lie: cannot eat", w1, w2, file=sys.stderr)
+ lies_amount += 1
+ else:
+ dsu.union(animal1, animal2, instruction_type - 1)
+ # print("Success", file=sys.stderr)
+ print(lies_amount)
diff --git a/noip2012s_kings_game.cpp b/noip2012s_kings_game.cpp
new file mode 100644
index 0000000..4ab5afd
--- /dev/null
+++ b/noip2012s_kings_game.cpp
@@ -0,0 +1,10 @@
+#include
+
+using namespace std;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/pojcs101_M03441_sum_zero.cpp b/pojcs101_M03441_sum_zero.cpp
new file mode 100644
index 0000000..f688ac6
--- /dev/null
+++ b/pojcs101_M03441_sum_zero.cpp
@@ -0,0 +1,56 @@
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+constexpr size_t MAX_N = 4005;
+long long numbers_amount, numbers[4][MAX_N];
+vector sums[2];
+unordered_map sum_to_amount[2];
+
+template
+void log_vector(vector vec, const string& prefix = "") {
+ clog << prefix;
+ for (const T& n : vec) {
+ clog << n << " ";
+ }
+ clog << endl;
+}
+
+void build_sums(const size_t sums_idx) {
+ for (int i = 0; i < numbers_amount; i++) {
+ for (int j = 0; j < numbers_amount; j++) {
+ long long sum = numbers[sums_idx][i] + numbers[sums_idx + 1][j];
+ if (!sum_to_amount[sums_idx / 2][sum]++) {
+ sums[sums_idx / 2].push_back(sum);
+ }
+ }
+ }
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> numbers_amount;
+ for (int i = 0; i < numbers_amount; i++) {
+ cin >> numbers[0][i] >> numbers[1][i] >> numbers[2][i] >> numbers[3][i];
+ }
+ build_sums(0), build_sums(2);
+ sort(sums[0].begin(), sums[0].end()), sort(sums[1].begin(), sums[1].end());
+ // log_vector(sums[0], "sums[0]: ");
+ // log_vector(sums[1], "sums[1]: ");
+
+ unsigned int ans = 0;
+ for (const long long n : sums[0]) {
+ auto it = lower_bound(sums[1].begin(), sums[1].end(), -n);
+ if (it != sums[1].end() && *it == -n) {
+ ans += sum_to_amount[1][-n] * sum_to_amount[0][n];
+ }
+ }
+ cout << ans << endl;
+
+ return 0;
+}
diff --git a/pojcs101_M03532.cpp b/pojcs101_M03532.cpp
new file mode 100644
index 0000000..f0cdac1
--- /dev/null
+++ b/pojcs101_M03532.cpp
@@ -0,0 +1,35 @@
+#include
+#include
+#include
+
+using namespace std;
+
+int get_max_sum(const vector& vec) {
+ vector max_sums(vec.size());
+ max_sums[0] = vec[0];
+ for (int i = 1; i < vec.size(); i++) {
+ int max_sum = 0;
+ for (int j = 0; j < i; j++) {
+ if (vec[j] < vec[i]) {
+ max_sum = max(max_sum, max_sums[j]);
+ }
+ }
+ max_sums[i] = max_sum + vec[i];
+ }
+ return *max_element(max_sums.begin(), max_sums.end());
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int numbers_amount;
+ cin >> numbers_amount;
+ vector numbers(numbers_amount);
+ for (int i = 0; i < numbers_amount; i++) {
+ cin >> numbers[i];
+ }
+ cout << get_max_sum(numbers) << endl;
+
+ return 0;
+}
diff --git a/pojcs101_M03704_brackets.cpp b/pojcs101_M03704_brackets.cpp
new file mode 100644
index 0000000..fd35fea
--- /dev/null
+++ b/pojcs101_M03704_brackets.cpp
@@ -0,0 +1,51 @@
+#include
+#include
+#include
+
+using namespace std;
+
+struct Bracket {
+ bool is_right;
+ size_t index;
+
+ bool operator<(const Bracket& other) const {
+ return index < other.index;
+ }
+};
+
+string get_unmatched_brackets_string(const string& s) {
+ stack brackets;
+ vector unmatched_brackets;
+ string printed_string(s.size(), ' ');
+ for (size_t i = 0; i < s.size(); i++) {
+ if (s[i] == '(') {
+ brackets.push({.is_right = false, .index = i});
+ } else if (s[i] == ')') {
+ if (brackets.empty()) {
+ unmatched_brackets.push_back({.is_right = true, .index = i});
+ } else {
+ brackets.pop();
+ }
+ }
+ }
+ while (!brackets.empty()) {
+ unmatched_brackets.push_back(brackets.top());
+ brackets.pop();
+ }
+ for (const auto& [is_right, index] : unmatched_brackets) {
+ printed_string[index] = is_right ? '?' : '$';
+ }
+ return printed_string;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ string s;
+ while (getline(cin, s) && !s.empty()) {
+ cout << s << endl << get_unmatched_brackets_string(s) << endl;
+ }
+
+ return 0;
+}
diff --git a/pojcs101_T01011_sticks.cpp b/pojcs101_T01011_sticks.cpp
index 52835b1..385c353 100644
--- a/pojcs101_T01011_sticks.cpp
+++ b/pojcs101_T01011_sticks.cpp
@@ -29,42 +29,54 @@ vector get_divisors(const int n) {
return small_divisors;
}
-/// 需要保证 sorted_cut_sticks 有序;需要保证 target_length 是总长度的因数
bool verify_length_dfs(const vector& sorted_cut_sticks,
const int target_length,
+ const int remaining_length,
+ const int original_sticks,
const int remaining_sticks,
- vector& used) {
- if (target_length == 0) { return true; }
- if (target_length < 0) { return false; }
- // if (sorted_cut_sticks.empty()) { throw logic_error("Invalid target_times"); }
- // if (sorted_cut_sticks.empty()) { return true; }
- // if (sorted_cut_sticks.back() > target_length) { return false; }
- // while (!sorted_cut_sticks.empty() && sorted_cut_sticks.back() >= target_length) {
- // sorted_cut_sticks.pop_back(); // 直接用
- // }
- // if (sorted_cut_sticks.empty()) { return true; }
-
- const int remaining_length = target_length;
- for (int i = static_cast(sorted_cut_sticks.size()) - 1; i >= 0; i--) {
- if (sorted_cut_sticks[i] > target_length) { return false; }
+ size_t global_right_idx,
+ const size_t right_idx,
+ vector& used
+) {
+ // clog << "verify_length_dfs(" << target_length << ") " << remaining_length << " " << remaining_sticks << "x " <<
+ // global_right_idx << "->" << right_idx << endl;
+ // log_vector(used, "used: ");
+ if (remaining_sticks == 0) { return true; }
+ bool result = false;
+ for (int i = static_cast(right_idx); i >= 0; i--) {
+ if (target_length < sorted_cut_sticks[i]) { return false; }
if (used[i]) { continue; }
- if (sorted_cut_sticks[i] > remaining_length) { continue; }
+ if (remaining_length < sorted_cut_sticks[i]) { continue; }
used[i] = true;
- if (verify_length_dfs(sorted_cut_sticks, remaining_length - sorted_cut_sticks[i], remaining_sticks, used)) {
- return true;
+ if (remaining_length == sorted_cut_sticks[i]) { // 下一根
+ result = verify_length_dfs(sorted_cut_sticks, target_length, target_length, original_sticks,
+ remaining_sticks - 1, global_right_idx,
+ global_right_idx, used);
+ } else { // 这一根
+ if (remaining_length == target_length) { // 这一根的第一节
+ global_right_idx = i;
+ }
+ result = verify_length_dfs(sorted_cut_sticks, target_length,
+ remaining_length - sorted_cut_sticks[i], original_sticks,
+ remaining_sticks, global_right_idx, i, used);
}
+ if (result) { return true; }
used[i] = false;
- // 剪枝
+ while (i >= 0 && sorted_cut_sticks[i] == sorted_cut_sticks[i - 1]) { i--; } // 剪枝
+ // if (remaining_sticks == original_sticks && remaining_length == target_length) { return false; } // 剪枝
+ if (remaining_length == target_length) { return false; } // 剪枝
}
return false;
}
+/// 需要保证 sorted_cut_sticks 有序;需要保证 target_length * remaining_sticks = 总长度
bool verify_length_dfs(const vector& sorted_cut_sticks,
const int target_length,
const int remaining_sticks) {
vector used(sorted_cut_sticks.size(), false);
- bool result = verify_length_dfs(sorted_cut_sticks, target_length, remaining_sticks, used);
- log_vector(used, "used: ");
+ bool result = verify_length_dfs(sorted_cut_sticks, target_length, target_length, remaining_sticks,
+ remaining_sticks, sorted_cut_sticks.size() - 1, sorted_cut_sticks.size() - 1, used);
+ // log_vector(used, "used: ");
return result;
}