aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/2020-algorithm-contest/code
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/life/2020-algorithm-contest/code')
-rw-r--r--store/works/life/2020-algorithm-contest/code/1.cpp22
-rw-r--r--store/works/life/2020-algorithm-contest/code/2.cpp54
-rw-r--r--store/works/life/2020-algorithm-contest/code/3.cpp38
-rw-r--r--store/works/life/2020-algorithm-contest/code/4.cpp71
-rw-r--r--store/works/life/2020-algorithm-contest/code/5.cpp133
5 files changed, 318 insertions, 0 deletions
diff --git a/store/works/life/2020-algorithm-contest/code/1.cpp b/store/works/life/2020-algorithm-contest/code/1.cpp
new file mode 100644
index 0000000..1ae1783
--- /dev/null
+++ b/store/works/life/2020-algorithm-contest/code/1.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+#include <set>
+
+int main()
+{
+ int size;
+ std::cin >> size;
+ std::set<int> s;
+ int v;
+ for (int i = 0; i< size; i++)
+ {
+ std::cin >> v;
+ s.insert(v);
+ }
+
+ for (auto value : s)
+ {
+ std::cout << value << ' ';
+ }
+
+ return 0;
+}
diff --git a/store/works/life/2020-algorithm-contest/code/2.cpp b/store/works/life/2020-algorithm-contest/code/2.cpp
new file mode 100644
index 0000000..2d5fded
--- /dev/null
+++ b/store/works/life/2020-algorithm-contest/code/2.cpp
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <map>
+#include <vector>
+#include <algorithm>
+
+int main()
+{
+ int size;
+ std::cin >> size;
+ int target;
+ std::cin >> target;
+
+ std::map<int, int> nums;
+
+ for (int i = 0; i < size; i++)
+ {
+ int v;
+ std::cin >> v;
+
+ nums[v]++;
+ }
+
+ std::vector<int> counts;
+ std::vector<std::vector<int>> sets;
+
+ for (const auto &pair : nums)
+ {
+ auto iter = std::lower_bound(counts.cbegin(), counts.cend(), pair.second);
+ if (iter != counts.cend() && *iter == pair.second)
+ {
+ sets[iter - counts.cbegin()].push_back(pair.first);
+ }
+ else
+ {
+ const auto offset = iter - counts.cbegin();
+ counts.insert(iter, pair.second);
+ sets.insert(sets.cbegin() + offset, std::vector<int>{pair.first});
+ }
+ }
+
+ if (target > counts.size())
+ {
+ std::cout << 0;
+ }
+ else
+ {
+ const auto &set = sets[counts.size() - target];
+ std::cout << set.size() << '\n';
+ for (auto i : set)
+ std::cout << i << ' ';
+ }
+
+ return 0;
+}
diff --git a/store/works/life/2020-algorithm-contest/code/3.cpp b/store/works/life/2020-algorithm-contest/code/3.cpp
new file mode 100644
index 0000000..088cc5e
--- /dev/null
+++ b/store/works/life/2020-algorithm-contest/code/3.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+
+int main()
+{
+ int length, numRows;
+ std::cin >> length >> numRows;
+
+ if (numRows == 1)
+ {
+ for (int i = 1; i <= length; i++)
+ std::cout << i << ' ';
+ return 0;
+ }
+
+ const int count_per_group = numRows * 2 - 2;
+ for (int row = 0; row < numRows; row++)
+ {
+ if (row == 0)
+ {
+ for (int p = 0; p < length; p += count_per_group)
+ std::cout << p + 1 << ' ';
+ }
+ else if (row == numRows - 1)
+ {
+ for (int p = row; p < length; p += count_per_group)
+ std::cout << p + 1 << ' ';
+ }
+ else
+ {
+ bool former = true;
+ const auto former_gap = count_per_group - row * 2;
+ const auto latter_gap = count_per_group - former_gap;
+ for (int p = row; p < length; p += (former ? former_gap : latter_gap), former = !former)
+ std::cout << p + 1 << ' ';
+ }
+ }
+ return 0;
+}
diff --git a/store/works/life/2020-algorithm-contest/code/4.cpp b/store/works/life/2020-algorithm-contest/code/4.cpp
new file mode 100644
index 0000000..4ee94cb
--- /dev/null
+++ b/store/works/life/2020-algorithm-contest/code/4.cpp
@@ -0,0 +1,71 @@
+#include <iostream>
+#include <vector>
+
+int calc(std::vector<std::vector<int>> &obstacleGrid)
+{
+
+ int R = obstacleGrid.size();
+ int C = obstacleGrid[0].size();
+
+ // If the starting cell has an obstacle, then simply return as there would be
+ // no paths to the destination.
+ if (obstacleGrid[0][0] == 1)
+ {
+ return 0;
+ }
+
+ // Number of ways of reaching the starting cell = 1.
+ obstacleGrid[0][0] = 1;
+
+ // Filling the values for the first column
+ for (int i = 1; i < R; i++)
+ {
+ obstacleGrid[i][0] = (obstacleGrid[i][0] == 0 && obstacleGrid[i - 1][0] == 1) ? 1 : 0;
+ }
+
+ // Filling the values for the first row
+ for (int i = 1; i < C; i++)
+ {
+ obstacleGrid[0][i] = (obstacleGrid[0][i] == 0 && obstacleGrid[0][i - 1] == 1) ? 1 : 0;
+ }
+
+ // Starting from cell(1,1) fill up the values
+ // No. of ways of reaching cell[i][j] = cell[i - 1][j] + cell[i][j - 1]
+ // i.e. From above and left.
+ for (int i = 1; i < R; i++)
+ {
+ for (int j = 1; j < C; j++)
+ {
+ if (obstacleGrid[i][j] == 0)
+ {
+ obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
+ }
+ else
+ {
+ obstacleGrid[i][j] = 0;
+ }
+ }
+ }
+
+ // Return value stored in rightmost bottommost cell. That is the destination.
+ return obstacleGrid[R - 1][C - 1];
+}
+
+int main()
+{
+ int row, column, bs;
+ std::cin >> row >> column >> bs;
+
+ std::vector<std::vector<int>> grid(row, std::vector<int>(column, 0));
+
+ for (int i = 0; i < bs; i++)
+ {
+ int r, c;
+ std::cin >> r >> c;
+ grid[r][c] = 1;
+ }
+
+ std::cout << calc(grid);
+
+ return 0;
+}
diff --git a/store/works/life/2020-algorithm-contest/code/5.cpp b/store/works/life/2020-algorithm-contest/code/5.cpp
new file mode 100644
index 0000000..24d3fd6
--- /dev/null
+++ b/store/works/life/2020-algorithm-contest/code/5.cpp
@@ -0,0 +1,133 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <cassert>
+
+enum class Op
+{
+ Add,
+ Sub,
+ Mul,
+ Div
+};
+
+struct OpInfo
+{
+ explicit OpInfo(Op op) : op(op), count(0) {}
+
+ Op op;
+ int count; // operand count
+};
+
+int main()
+{
+ std::string expr;
+ std::getline(std::cin, expr);
+
+ std::vector<int> operands;
+ std::vector<OpInfo> op_infos;
+
+ for (auto c : expr)
+ {
+ if (c == '+')
+ {
+ op_infos.emplace_back(Op::Add);
+ }
+ else if (c == '-')
+ {
+ op_infos.emplace_back(Op::Sub);
+ }
+ else if (c == '*')
+ {
+ op_infos.emplace_back(Op::Mul);
+ }
+ else if (c == '/')
+ {
+ op_infos.emplace_back(Op::Div);
+ }
+ else if (c == '(')
+ {
+ if (!op_infos.empty())
+ op_infos.back().count++;
+ }
+ else if (c >= '0' && c <= '9')
+ {
+ operands.push_back(c - '0');
+ op_infos.back().count++;
+ }
+ else if (c == ')')
+ {
+ const auto &info = op_infos.back();
+ if ((info.op == Op::Sub || info.op == Op::Div) && info.count != 2)
+ {
+ std::cerr << "Sub or Div Op has not 2 operands.";
+ return 1;
+ }
+
+ switch (info.op)
+ {
+ case Op::Add:
+ {
+ int r = 0;
+ for (int i = 0; i < info.count; i++)
+ {
+ r += operands.back();
+ operands.pop_back();
+ }
+ operands.push_back(r);
+ op_infos.pop_back();
+ break;
+ }
+ case Op::Sub:
+ {
+ int a = operands.back();
+ operands.pop_back();
+ int b = operands.back();
+ operands.pop_back();
+ operands.push_back(b - a);
+ op_infos.pop_back();
+ break;
+ }
+ case Op::Mul:
+ {
+ int r = 1;
+ for (int i = 0; i < info.count; i++)
+ {
+ r *= operands.back();
+ operands.pop_back();
+ }
+ operands.push_back(r);
+ op_infos.pop_back();
+ break;
+ }
+ case Op::Div:
+ {
+ int a = operands.back();
+ operands.pop_back();
+ int b = operands.back();
+ operands.pop_back();
+ operands.push_back(b / a);
+ op_infos.pop_back();
+ break;
+ }
+ default:
+ std::terminate();
+ }
+ }
+ }
+
+ if (operands.size() != 1)
+ {
+ std::cerr << "final operand count is not 1, but " << operands.size();
+ return 1;
+ }
+ if (!op_infos.empty())
+ {
+ std::cerr << "final op_info is not empty";
+ return 1;
+ }
+
+ std::cout << operands.front();
+
+ return 0;
+}