diff --git a/.gitignore b/.gitignore
index 424afbe..0c3d9d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,7 @@
.venv
-*.exe
+/*.exe
+
+# 忽略 linux 编译产物
+/*
+!/*.*
+!*/
\ No newline at end of file
diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..e402e4d
--- /dev/null
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/editor.xml b/.idea/editor.xml
index 3f164bc..69ae789 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -206,7 +206,7 @@
-
+
@@ -250,101 +250,18 @@
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/exercises.iml b/.idea/exercises.iml
new file mode 100644
index 0000000..4c94235
--- /dev/null
+++ b/.idea/exercises.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index 2ca729b..b4af121 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -1,6 +1,7 @@
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index c9bc359..f81f1bc 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,19 +4,46 @@
+
+ {
+ "useNewFormat": true
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
@@ -25,10 +52,12 @@
+
-
+
+
+
+
+
+
+
+
+
{
"customColor": "",
"associatedIndex": 6
@@ -54,6 +94,7 @@
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
@@ -142,10 +187,27 @@
+
+
+
+
+
+
+
+
+
+
+ file://$PROJECT_DIR$/pojcs101_M01321_chest_grid.cpp
+ 44
+
+
+
+
+
diff --git a/pojcs101_M01002_memorable_number.cpp b/pojcs101_M01002_memorable_number.cpp
index c3c26a4..9976c06 100644
--- a/pojcs101_M01002_memorable_number.cpp
+++ b/pojcs101_M01002_memorable_number.cpp
@@ -40,7 +40,6 @@ void runDataset() {
if (noDuplicate) {
std::cout << "No duplicates." << std::endl;
}
- // std::cout << std::endl;
}
int main() {
diff --git a/pojcs101_M01088_ski.cpp b/pojcs101_M01088_ski.cpp
new file mode 100644
index 0000000..8ca0542
--- /dev/null
+++ b/pojcs101_M01088_ski.cpp
@@ -0,0 +1,76 @@
+#include
+#include
+
+using namespace std;
+
+constexpr int DX[] = {0, 1, 0, -1};
+constexpr int DY[] = {1, 0, -1, 0};
+
+int path_length = 0;
+
+void log_grid(const vector>& grid, string name) {
+ for (const auto& row : grid) {
+ clog << name << ": ";
+ for (const auto& cell : row) {
+ clog << cell << " ";
+ }
+ clog << endl;
+ }
+}
+
+int dfs(const vector>& grid, const int x, const int y,
+ vector>& calculated_depths, const int _depth = 1) {
+ const unsigned int r = grid.size(), c = grid[0].size();
+
+ const int current_num = grid[x][y];
+ if (calculated_depths[x][y] > 0) { return calculated_depths[x][y] + _depth - 1; }
+
+ if (_depth + current_num <= path_length) {
+ // clog << "CUT " << x << "," << y << ":" << current_num << ", " << _depth << endl;
+ return -1;
+ } // 剪枝
+ // if (current_num == 1) { return _depth; }
+ // clog << x << "," << y << ":" << current_num << ", " << _depth << endl;
+
+ int best_depth = _depth;
+
+ for (int i = 0; i < 4; i++) {
+ const int nx = x + DX[i], ny = y + DY[i];
+ if (nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] < current_num) {
+ int next_depth = dfs(grid, nx, ny, calculated_depths, _depth + 1);
+ best_depth = max(best_depth, next_depth);
+ }
+ }
+ path_length = max(path_length, best_depth);
+ calculated_depths[x][y] = best_depth - _depth + 1;
+ return best_depth;
+}
+
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int r, c;
+ cin >> r >> c;
+
+ vector> grid(r, vector(c));
+ vector> calculated_depths(r, vector(c, 0));
+
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ cin >> grid[i][j];
+ }
+ }
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ dfs(grid, i, j, calculated_depths);
+ // clog << "---" << endl;
+ // log_grid(calculated_depths, "calculated_depths");
+ // clog << "---" << endl;
+ }
+ }
+ cout << path_length << endl;
+
+ return 0;
+}
diff --git a/pojcs101_M01088_ski_WRONG.cpp b/pojcs101_M01088_ski_WRONG.cpp
new file mode 100644
index 0000000..9d94529
--- /dev/null
+++ b/pojcs101_M01088_ski_WRONG.cpp
@@ -0,0 +1,49 @@
+#include
+#include
+
+using namespace std;
+
+constexpr int DX[] = {0, 1, 0, -1};
+constexpr int DY[] = {1, 0, -1, 0};
+
+int longest_path(const vector > &grid, int x = -1, int y = -1, int depth = 0) {
+ const unsigned int r = grid.size(), c = grid[0].size();
+ if (x == -1 && y == -1) {
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ if (grid[i][j] == r * c) {
+ x = i, y = j, depth = 1;
+ break;
+ }
+ }
+ }
+ }
+
+ for (int i = 0; i < 4; i++) {
+ int nx = x + DX[i], ny = y + DY[i];
+ if (nx >= 0 && nx < grid.size() && ny >= 0 && ny < grid[0].size()
+ && grid[nx][ny] == grid[x][y] - 1) {
+ return longest_path(grid, nx, ny, depth + 1);
+ }
+ }
+ return depth;
+}
+
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int r, c;
+ cin >> r >> c;
+
+ vector > grid(r, vector(c));
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ cin >> grid[i][j];
+ }
+ }
+ cout << longest_path(grid) << endl;
+
+ return 0;
+}
diff --git a/pojcs101_M01321_chest_grid.cpp b/pojcs101_M01321_chest_grid.cpp
new file mode 100644
index 0000000..c010401
--- /dev/null
+++ b/pojcs101_M01321_chest_grid.cpp
@@ -0,0 +1,71 @@
+#include
+#include
+
+using namespace std;
+
+void log_array(const vector& arr, const string& name) {
+ clog << name << ": ";
+ for (const auto& cell : arr) {
+ clog << cell << " ";
+ }
+ clog << endl;
+}
+
+vector> get_grid(const int size) {
+ vector> grid;
+ for (int _ = 0; _ < size; _++) {
+ string pattern;
+ vector row;
+ cin >> pattern;
+ for (const char c : pattern) { row.push_back(c != '.'); }
+ grid.push_back(row);
+ }
+ return grid;
+}
+
+long long dfs(const vector>& grid,
+ const int remaining_chests,
+ const int curr_empty_row,
+ vector& col_occupation) {
+ const unsigned int size = grid[0].size();
+ if (remaining_chests == 0) { return 1; }
+ if (curr_empty_row >= size) { return 0; }
+ // clog << "curr_empty_row: " << curr_empty_row << endl;
+ // log_array(col_occupation, "col_occupation");
+ long long ans = 0;
+ for (int col = 0; col < size; col++) { // 这行放
+ if (!grid[curr_empty_row][col]) { continue; }
+ if (col_occupation[col]) { continue; }
+ // vector new_col_occupation(col_occupation);
+ col_occupation[col] = true;
+ ans += dfs(grid, remaining_chests - 1, curr_empty_row + 1, col_occupation);
+ col_occupation[col] = false;
+ }
+ ans += dfs(grid, remaining_chests, curr_empty_row + 1, col_occupation); // 不在这行放
+ return ans;
+}
+
+long long dfs(const vector>& grid,
+ const int remaining_chests,
+ const int curr_empty_row = 0) {
+ vector col_occupation(grid[0].size(), false);
+ return dfs(grid, remaining_chests, curr_empty_row, col_occupation);
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int size, chest_count;
+ while (cin >> size >> chest_count) {
+ if (size < 0) { break; }
+ vector> grid = get_grid(size);
+ cout << dfs(grid, chest_count) << endl;
+ }
+ // cin >> size;
+ // chest_count = size;
+ // const vector> grid = get_grid(size);
+ // cout << dfs(grid, chest_count) << endl;
+
+ return 0;
+}
diff --git a/pojcs101_M01328_radar_greedy.cpp b/pojcs101_M01328_radar_greedy.cpp
new file mode 100644
index 0000000..4c7595a
--- /dev/null
+++ b/pojcs101_M01328_radar_greedy.cpp
@@ -0,0 +1,59 @@
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+double other_leg(const double leg, const double diag) {
+ return sqrt(diag * diag - leg * leg);
+}
+
+double diag(const double leg1, const double leg2) {
+ return sqrt(leg1 * leg1 + leg2 * leg2);
+}
+
+vector> target_ranges(vector> islands, const int distance) {
+ vector> ranges;
+ for (auto& [island_x, island_y] : islands) {
+ if (island_y > distance) { throw std::runtime_error("Island is too far away from the radar."); }
+ const double offset = other_leg(island_y, distance);
+ ranges.emplace_back(island_x - offset, island_x + offset);
+ }
+ sort(ranges.begin(), ranges.end(),
+ [](const auto& a, const auto& b) { return a.second < b.second; });
+ return ranges;
+}
+
+int required_radars(const vector>& ranges) {
+ if (ranges.empty()) { return 0; }
+ int answer = 1;
+ double current_max_x = ranges[0].second;
+ for (const auto& [left,right] : ranges) {
+ if (left > current_max_x) { answer++, current_max_x = right; } // 放不下,加雷达
+ else if (right < current_max_x) { current_max_x = right; } // 放下,需要左移雷达
+ clog << "current_max_x: " << current_max_x << " answer: " << answer << endl;
+ }
+ return answer;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int n, distance, case_id = 0;
+ while (cin >> n >> distance) {
+ if (n == 0 && distance == 0) { break; }
+ vector> islands(n);
+ for (int i = 0; i < n; i++) {
+ cin >> islands[i].first >> islands[i].second;
+ }
+ // cout << "Case " << ++case_id << ": ";
+ try {
+ cout << required_radars(target_ranges(islands, distance)) << endl;
+ } catch (std::runtime_error&) {
+ cout << -1 << endl;
+ }
+ }
+}
+
diff --git a/pojcs101_M01328_radar_greedy_WRONG.cpp b/pojcs101_M01328_radar_greedy_WRONG.cpp
new file mode 100644
index 0000000..c090542
--- /dev/null
+++ b/pojcs101_M01328_radar_greedy_WRONG.cpp
@@ -0,0 +1,61 @@
+#include
+#include
+#include
+#include
+
+
+using namespace std;
+
+double other_leg(const double leg, const double diag) {
+ return sqrt(diag * diag - leg * leg);
+}
+
+double diag(const double leg1, const double leg2) {
+ return sqrt(leg1 * leg1 + leg2 * leg2);
+}
+
+int required_radars(vector> islands, int distance) {
+ for (auto& [island_x, island_y] : islands) {
+ if (island_y > distance) { return -1; }
+ }
+ sort(islands.begin(), islands.end());
+ int answer = 0;
+ while (!islands.empty()) {
+ answer++;
+ const auto current_island = islands.back();
+ islands.pop_back();
+ double min_x = current_island.first
+ - other_leg(current_island.second, distance)
+ - current_island.second;
+ vector> uncovered_islands;
+ while (!islands.empty() && islands.back().first >= min_x) {
+ // 右移雷达
+ const double offset = other_leg(islands.back().second, distance);
+ if (islands.back().first - offset >= min_x) {
+ min_x = islands.back().first - offset;
+ } else {
+ uncovered_islands.push_back(islands.back());
+ }
+ islands.pop_back();
+ }
+ for (int i = uncovered_islands.size() - 1; i >= 0; i--) { // NOLINT(*-narrowing-conversions)
+ islands.push_back(uncovered_islands[i]);
+ }
+ }
+ return answer;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int n, distance, case_id = 0;
+ while (cin >> n >> distance) {
+ if (n == 0 && distance == 0) { break; }
+ vector> islands(n);
+ for (int i = 0; i < n; i++) {
+ cin >> islands[i].first >> islands[i].second;
+ }
+ cout << "Case " << ++case_id << ": " << required_radars(islands, distance) << endl;
+ }
+}
diff --git a/pojcs101_M01852_ants_falling.cpp b/pojcs101_M01852_ants_falling.cpp
new file mode 100644
index 0000000..328dfea
--- /dev/null
+++ b/pojcs101_M01852_ants_falling.cpp
@@ -0,0 +1,40 @@
+#include
+#include
+#include
+
+using namespace std;
+
+int shortest_fall_time(const int length, vector ant_pos) {
+ sort(ant_pos.begin(), ant_pos.end(),
+ [length](const int pos1, const int pos2) {
+ return abs(pos1 - length / 2) < abs(pos2 - length / 2); // 到中点距离最短者
+ });
+ return min(ant_pos[0], length - ant_pos[0]);
+}
+
+int longest_fall_time(const int length, vector ant_pos) {
+ sort(ant_pos.begin(), ant_pos.end(),
+ [length](const int pos1, const int pos2) {
+ return abs(pos1 - length / 2) > abs(pos2 - length / 2); // 到中点距离最长者
+ });
+ return max(ant_pos[0], length - ant_pos[0]);
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int group_count;
+ cin >> group_count;
+ for (int _ = 0; _ < group_count; _++) {
+ int length, ant_count;
+ cin >> length >> ant_count;
+ vector ant_pos(ant_count);
+ for (int i = 0; i < ant_count; i++) {
+ cin >> ant_pos[i];
+ }
+ cout << shortest_fall_time(length, ant_pos) << " " << longest_fall_time(length, ant_pos) << endl;
+ }
+
+ return 0;
+}
diff --git a/pojcs101_M01922_ride_follower.cpp b/pojcs101_M01922_ride_follower.cpp
new file mode 100644
index 0000000..5dfbdd9
--- /dev/null
+++ b/pojcs101_M01922_ride_follower.cpp
@@ -0,0 +1,58 @@
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+typedef pair Rider;
+
+constexpr int FULL_DISTANCE = 4500 * 3.6;
+
+int ride_time(const Rider& rider, const int distance = FULL_DISTANCE) {
+ return static_cast(ceil(static_cast(distance) / rider.first)) + rider.second;
+}
+
+int start_pos(const Rider& rider) {
+ if (rider.second >= 0) { return 0; }
+ return rider.first * -rider.second;
+}
+
+int arrival_time(const vector& riders, const int distance = FULL_DISTANCE) {
+ vector early_riders, late_riders;
+ for (const auto& rider : riders) {
+ if (rider.second >= 0) {
+ late_riders.push_back(rider);
+ } else {
+ // early_riders.push_back(rider);
+ }
+ }
+ double min_time = ride_time(*min_element(
+ late_riders.begin(), late_riders.end(),
+ [distance](const Rider& a, const Rider& b) {
+ return ride_time(a, distance) < ride_time(b, distance);
+ }));
+ clog << "min_time: " << min_time << endl;
+ // for (const auto& rider : early_riders) {
+ // if (-rider.second > min_time) { continue; }
+ // min_time = min(min_time, ride_time(rider, distance));
+ // }
+ return static_cast(ceil(min_time));
+}
+
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+ int rider_count;
+ while (cin >> rider_count && rider_count) {
+ vector riders(rider_count);
+ for (int i = 0; i < rider_count; i++) {
+ cin >> riders[i].first >> riders[i].second;
+ // riders[i].first /= 3.6;
+ }
+ cout << arrival_time(riders) << endl;
+ }
+
+ return 0;
+}