aboutsummaryrefslogtreecommitdiff
path: root/01_introductory_problems
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /01_introductory_problems
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '01_introductory_problems')
-rwxr-xr-x01_introductory_problems/a.outbin0 -> 18512 bytes
-rw-r--r--01_introductory_problems/apple_division_1623.cpp21
-rw-r--r--01_introductory_problems/bit_strings_1617.cpp10
-rw-r--r--01_introductory_problems/chessboard_and_queens_1624.cpp42
-rw-r--r--01_introductory_problems/coin_piles_1754.cpp18
-rw-r--r--01_introductory_problems/creating_strings_1622.cpp30
-rw-r--r--01_introductory_problems/digit_queries_2431.cpp23
-rw-r--r--01_introductory_problems/gray_code_2205.cpp22
-rw-r--r--01_introductory_problems/grid_coloring_i_3311.cpp17
-rw-r--r--01_introductory_problems/grid_path_description_1625.cpp150
-rw-r--r--01_introductory_problems/increasing_array_1094.cpp12
-rw-r--r--01_introductory_problems/knight_moves_grid_3217.cpp40
-rw-r--r--01_introductory_problems/mex_grid_construction_3419.cpp30
-rw-r--r--01_introductory_problems/missing_number_1083.cpp9
-rw-r--r--01_introductory_problems/number_spiral_1071.cpp27
-rw-r--r--01_introductory_problems/palindrome_reorder_1755.cpp35
-rw-r--r--01_introductory_problems/permutations_1070.cpp15
-rw-r--r--01_introductory_problems/raab_game_i_3399.cpp67
-rw-r--r--01_introductory_problems/repetitions_1069.cpp20
-rw-r--r--01_introductory_problems/string_reorder_1743.cpp38
-rw-r--r--01_introductory_problems/tower_of_hanoi_2165.cpp17
-rw-r--r--01_introductory_problems/trailing_zeros_1618.cpp9
-rw-r--r--01_introductory_problems/two_knights_1072.cpp24
-rw-r--r--01_introductory_problems/two_sets_1092.cpp53
-rw-r--r--01_introductory_problems/weird_algorithm_1068.cpp12
25 files changed, 741 insertions, 0 deletions
diff --git a/01_introductory_problems/a.out b/01_introductory_problems/a.out
new file mode 100755
index 0000000..8c1bdbd
--- /dev/null
+++ b/01_introductory_problems/a.out
Binary files differ
diff --git a/01_introductory_problems/apple_division_1623.cpp b/01_introductory_problems/apple_division_1623.cpp
new file mode 100644
index 0000000..fb367b4
--- /dev/null
+++ b/01_introductory_problems/apple_division_1623.cpp
@@ -0,0 +1,21 @@
1#include <iostream>
2#include <numeric>
3#include <vector>
4
5long long md(std::vector<long long>& a, long long p, long long t, size_t i) {
6 if (2*p >= t)
7 return 2*p - t;
8 if (i == a.size())
9 return t;
10 return std::min(md(a, p, t, i+1), md(a, p+a[i], t, i+1));
11}
12
13int main() {
14 size_t n;
15 std::cin >> n;
16 std::vector<long long> a(n);
17 for (size_t i = 0; i < n; i++)
18 std::cin >> a[i];
19 auto s = md(a, 0, std::accumulate(a.begin(), a.end(), (long long)0), 0);
20 std::cout << s << "\n";
21}
diff --git a/01_introductory_problems/bit_strings_1617.cpp b/01_introductory_problems/bit_strings_1617.cpp
new file mode 100644
index 0000000..6be46ac
--- /dev/null
+++ b/01_introductory_problems/bit_strings_1617.cpp
@@ -0,0 +1,10 @@
1#include <iostream>
2
3int main() {
4 constexpr long long M = 1e9+7;
5 long long n, a{1};
6 std::cin >> n;
7 while (--n >= 0)
8 a = (a * 2) % M;
9 std::cout << a << std::endl;
10}
diff --git a/01_introductory_problems/chessboard_and_queens_1624.cpp b/01_introductory_problems/chessboard_and_queens_1624.cpp
new file mode 100644
index 0000000..d595232
--- /dev/null
+++ b/01_introductory_problems/chessboard_and_queens_1624.cpp
@@ -0,0 +1,42 @@
1#include <bitset>
2#include <iostream>
3#include <string>
4
5void set_bit(std::bitset<64>& r, int i, int j) {
6 if (i >= 0 && i < 8 && j >= 0 && j < 8)
7 r |= 1ULL << (unsigned long long)(8*i + j);
8}
9
10std::bitset<64> maskall(const std::bitset<64>& b, int i, int j) {
11 std::bitset<64> r{b};
12 r |= 0x0101010101010101ULL << (unsigned long long)j;
13 for (int k = 0; k < 8; k++) {
14 set_bit(r, k, k+j-i);
15 set_bit(r, k, -k+j+i);
16 }
17 return r;
18}
19
20long long f(std::bitset<64>& b, int i) {
21 if (i == 8)
22 return 1;
23 long long s{0};
24 for (int j = 0; j < 8; j++) {
25 if (!b[8*i+j]) {
26 auto rr = maskall(b, i, j);
27 s += f(rr, i+1);
28 }
29 }
30 return s;
31}
32
33int main() {
34 std::bitset<64> b{0};
35 std::string s;
36 for (int i = 0; i < 8; i++) {
37 std::cin >> s;
38 for (int j = 0; j < 8; j++)
39 b[8*i+j] = s[j] == '*';
40 }
41 std::cout << f(b, 0) << "\n";
42}
diff --git a/01_introductory_problems/coin_piles_1754.cpp b/01_introductory_problems/coin_piles_1754.cpp
new file mode 100644
index 0000000..c54cdf1
--- /dev/null
+++ b/01_introductory_problems/coin_piles_1754.cpp
@@ -0,0 +1,18 @@
1#include <algorithm>
2#include <iostream>
3
4bool f(int a, int b) {
5 auto max = std::max(a, b);
6 auto min = std::min(a, b);
7
8 return max <= 2*min && (2*min - max) % 3 == 0;
9}
10
11int main() {
12 int t, a, b;
13 std::cin >> t;
14 for (int i = 0; i < t; i++) {
15 std::cin >> a >> b;
16 std::cout << (f(a, b) ? "YES" : "NO") << std::endl;
17 }
18}
diff --git a/01_introductory_problems/creating_strings_1622.cpp b/01_introductory_problems/creating_strings_1622.cpp
new file mode 100644
index 0000000..689e083
--- /dev/null
+++ b/01_introductory_problems/creating_strings_1622.cpp
@@ -0,0 +1,30 @@
1#include <iostream>
2#include <string>
3#include <map>
4#include <vector>
5
6void gen(std::map<char, int>& a, int count,
7 std::string start, std::vector<std::string>& res) {
8 if (count == 0)
9 res.push_back(start);
10 for (auto [k, v] : a) {
11 if (v > 0) {
12 a[k]--;
13 gen(a, count - 1, start + k, res);
14 a[k]++;
15 }
16 }
17}
18
19int main() {
20 std::string s;
21 std::map<char, int> a;
22 std::vector<std::string> sol;
23 std::cin >> s;
24 for (auto c : s)
25 a[c]++;
26 gen(a, s.size(), "", sol);
27 std::cout << sol.size() << "\n";
28 for (auto x : sol)
29 std::cout << x << "\n";
30}
diff --git a/01_introductory_problems/digit_queries_2431.cpp b/01_introductory_problems/digit_queries_2431.cpp
new file mode 100644
index 0000000..28c4a2f
--- /dev/null
+++ b/01_introductory_problems/digit_queries_2431.cpp
@@ -0,0 +1,23 @@
1#include <iostream>
2
3int f(long long k) {
4 long long n{1}, p{1}, d{1}, q{9};
5 while (p + d*q <= k) {
6 n += q;
7 p += d * q;
8 d++;
9 q *= 10;
10 }
11 long long x{(k-p) / d + n};
12 long long m{(k-p) % d};
13 for (long long j = 0; j < d-m-1; j++)
14 x /= (long long)10;
15 return x % (long long)10;
16}
17
18int main() {
19 long long k;
20 std::cin >> k;
21 while (std::cin >> k)
22 std::cout << f(k) << "\n";
23}
diff --git a/01_introductory_problems/gray_code_2205.cpp b/01_introductory_problems/gray_code_2205.cpp
new file mode 100644
index 0000000..4fa8a2d
--- /dev/null
+++ b/01_introductory_problems/gray_code_2205.cpp
@@ -0,0 +1,22 @@
1#include <iostream>
2#include <format>
3
4void print(int number, int ndigits) {
5 std::cout << std::format("{0:0{1}b}", number, ndigits) << "\n";
6}
7
8void print_all(int& start, int digit, int ndigits) {
9 if (digit == 0) {
10 print(start, ndigits);
11 } else {
12 print_all(start, digit-1, ndigits);
13 start ^= 1 << (digit-1);
14 print_all(start, digit-1, ndigits);
15 }
16}
17
18int main() {
19 int n, start{0};
20 std::cin >> n;
21 print_all(start, n, n);
22}
diff --git a/01_introductory_problems/grid_coloring_i_3311.cpp b/01_introductory_problems/grid_coloring_i_3311.cpp
new file mode 100644
index 0000000..b6c59a4
--- /dev/null
+++ b/01_introductory_problems/grid_coloring_i_3311.cpp
@@ -0,0 +1,17 @@
1#include <iostream>
2#include <string>
3
4int main() {
5 int n, m;
6 std::cin >> n >> m;
7 for (int i = 0; i < n; i++) {
8 std::string s;
9 std::cin >> s;
10 for (int j = 0; j < m; j++) {
11 char c = 'A' + 2*((i+j)%2);
12 c += c == s[j];
13 std::cout << c;
14 }
15 std::cout << "\n";
16 }
17}
diff --git a/01_introductory_problems/grid_path_description_1625.cpp b/01_introductory_problems/grid_path_description_1625.cpp
new file mode 100644
index 0000000..b483168
--- /dev/null
+++ b/01_introductory_problems/grid_path_description_1625.cpp
@@ -0,0 +1,150 @@
1#include <algorithm>
2#include <bitset>
3#include <iostream>
4#include <queue>
5#include <string>
6#include <vector>
7
8/*
9The official solution uses the heuristic: if both adjacent squares
10in horizontal direction are visited or wall, and both in vertical direction
11are not visited (or the other way round), then we stop because we borked
12the square. I did not think of this criterion, so to check if the square
13is borked I do a full visit from the bottom-left corner. This is too slow,
14so I do this only at depths 10, 20, 30 and 40. This is good enough.
15*/
16
17class Tile {
18public:
19 int i;
20 int j;
21
22 bool valid() const { return i >= 0 && i < 7 && j >= 0 && j < 7; }
23 bool end() const { return i == 6 && j == 0; }
24 bool operator==(const Tile& t) const { return i == t.i && j == t.j; }
25 Tile u() const { return Tile{i-1, j}; }
26 Tile d() const { return Tile{i+1, j}; }
27 Tile l() const { return Tile{i, j-1}; }
28 Tile r() const { return Tile{i, j+1}; }
29 static Tile err() { return Tile{-1, -1}; }
30
31 Tile move(char c) const {
32 if (c == 'U') return u();
33 if (c == 'D') return d();
34 if (c == 'L') return l();
35 if (c == 'R') return r();
36 return err();
37 }
38
39 std::vector<Tile> neighbors() const {
40 return std::vector { u(), d(), l(), r() };
41 }
42};
43
44class Map {
45public:
46 Map() : b(), v(49, 4) {
47 for (int i = 0; i < 7; i++) {
48 v[index(Tile{i, 0})]--;
49 v[index(Tile{i, 6})]--;
50 v[index(Tile{0, i})]--;
51 v[index(Tile{6, i})]--;
52 }
53 }
54
55 bool visited(Tile t) const {
56 return !t.valid() || b.test(index(t));
57 }
58
59 void set(Tile t) {
60 if (!t.valid()) return;
61 b.set(index(t));
62 for (auto u : t.neighbors())
63 if (u.valid())
64 v[index(u)]--;
65 }
66
67 void reset(Tile t) {
68 if (!t.valid()) return;
69 b.reset(index(t));
70 for (auto u : t.neighbors())
71 if (u.valid())
72 v[index(u)]++;
73 }
74
75 std::vector<Tile> locked_neighbors(Tile t) const {
76 std::vector<Tile> r{};
77 for (auto u : t.neighbors())
78 if (locked(u))
79 r.push_back(u);
80 return r;
81 }
82
83 int count() const { return b.count(); }
84
85 bool borked() const {
86 std::bitset<49> vv{};
87 std::queue<Tile> q;
88 q.push(Tile{6, 0});
89 vv.set(index(Tile{6, 0}));
90 int c{1};
91 while (!q.empty()) {
92 Tile t = q.front();
93 q.pop();
94 for (auto u : t.neighbors()) {
95 if (!visited(u) && !vv.test(index(u))) {
96 vv.set(index(u));
97 c++;
98 q.push(u);
99 }
100 }
101 }
102
103 return c + count() < 49;
104 }
105
106private:
107 std::bitset<49> b;
108 std::vector<int> v;
109 static int index(Tile t) { return 7*t.i + t.j; }
110
111 bool locked(Tile t) const {
112 return t.valid() && !visited(t) && !t.end() && v[index(t)] < 2;
113 }
114};
115
116int f(Map& m, const std::string& s, size_t n, Tile t) {
117 if (n == 48) return t.end();
118 if (m.visited(t) || t.end()) return 0;
119
120 m.set(t);
121 if (n % 10 == 0 && m.borked()) {
122 m.reset(t);
123 return 0;
124 }
125
126 auto ln = m.locked_neighbors(t);
127
128 int r{0};
129 if (s[n] != '?') {
130 Tile nt = t.move(s[n]);
131 if (ln.size() == 0 || (ln.size() == 1 && ln[0] == nt))
132 r = f(m, s, n+1, nt);
133 } else {
134 if (ln.size() == 0)
135 r = f(m, s, n+1, t.u()) + f(m, s, n+1, t.d())
136 + f(m, s, n+1, t.l()) + f(m, s, n+1, t.r());
137 if (ln.size() == 1)
138 r = f(m, s, n+1, ln[0]);
139 }
140
141 m.reset(t);
142 return r;
143}
144
145int main() {
146 Map m;
147 std::string s;
148 std::cin >> s;
149 std::cout << f(m, s, 0, Tile{0, 0}) << "\n";
150}
diff --git a/01_introductory_problems/increasing_array_1094.cpp b/01_introductory_problems/increasing_array_1094.cpp
new file mode 100644
index 0000000..314c686
--- /dev/null
+++ b/01_introductory_problems/increasing_array_1094.cpp
@@ -0,0 +1,12 @@
1#include <algorithm>
2#include <iostream>
3
4int main() {
5 long long n, prev{0}, sum{0};
6 std::cin >> n;
7 while (std::cin >> n) {
8 sum += std::max<long long>(0, prev - n);
9 prev = std::max(prev, n);
10 }
11 std::cout << sum << std::endl;
12}
diff --git a/01_introductory_problems/knight_moves_grid_3217.cpp b/01_introductory_problems/knight_moves_grid_3217.cpp
new file mode 100644
index 0000000..212e82c
--- /dev/null
+++ b/01_introductory_problems/knight_moves_grid_3217.cpp
@@ -0,0 +1,40 @@
1#include <iostream>
2#include <queue>
3#include <tuple>
4#include <vector>
5
6struct Node {
7 int i;
8 int j;
9 int d;
10};
11
12int main() {
13 int n;
14 std::cin >> n;
15 std::vector<std::vector<int>> a(n, std::vector<int>(n, 1e7));
16 std::queue<Node> q;
17
18 q.push(Node{0, 0, 0});
19 while (!q.empty()) {
20 auto v = q.front();
21 q.pop();
22 if (v.i < 0 || v.j < 0 || v.i >= n || v.j >= n || a[v.i][v.j] <= v.d)
23 continue;
24 a[v.i][v.j] = v.d;
25 q.push(Node{v.i-2, v.j-1, v.d+1});
26 q.push(Node{v.i-2, v.j+1, v.d+1});
27 q.push(Node{v.i-1, v.j-2, v.d+1});
28 q.push(Node{v.i-1, v.j+2, v.d+1});
29 q.push(Node{v.i+2, v.j-1, v.d+1});
30 q.push(Node{v.i+2, v.j+1, v.d+1});
31 q.push(Node{v.i+1, v.j-2, v.d+1});
32 q.push(Node{v.i+1, v.j+2, v.d+1});
33 }
34
35 for (auto& v : a) {
36 for (auto& x : v)
37 std::cout << x << " ";
38 std::cout << "\n";
39 }
40}
diff --git a/01_introductory_problems/mex_grid_construction_3419.cpp b/01_introductory_problems/mex_grid_construction_3419.cpp
new file mode 100644
index 0000000..feb1171
--- /dev/null
+++ b/01_introductory_problems/mex_grid_construction_3419.cpp
@@ -0,0 +1,30 @@
1#include <algorithm>
2#include <bitset>
3#include <iostream>
4#include <vector>
5
6int firstzero(const std::bitset<200>& b) {
7 for (int i = 0; i < 200; i++)
8 if (!b.test(i))
9 return i;
10 return -1;
11}
12
13int next(int i, int j, std::vector<std::bitset<200>>& col,
14 std::vector<std::bitset<200>>& row) {
15 int r = firstzero(row[i] | col[j]);
16 row[i][r] = col[j][r] = 1;
17 return r;
18}
19
20int main() {
21 int n;
22 std::cin >> n;
23 std::vector<std::bitset<200>> col(n), row(n);
24
25 for (int i = 0; i < n; i++) {
26 for (int j = 0; j < n; j++)
27 std::cout << next(i, j, col, row) << " ";
28 std::cout << "\n";
29 }
30}
diff --git a/01_introductory_problems/missing_number_1083.cpp b/01_introductory_problems/missing_number_1083.cpp
new file mode 100644
index 0000000..527abb2
--- /dev/null
+++ b/01_introductory_problems/missing_number_1083.cpp
@@ -0,0 +1,9 @@
1#include <iostream>
2
3int main() {
4 long long n, m, sum{0};
5 std::cin >> n;
6 while (std::cin >> m)
7 sum += m;
8 std::cout << n*(n+1)/2 - sum << std::endl;
9}
diff --git a/01_introductory_problems/number_spiral_1071.cpp b/01_introductory_problems/number_spiral_1071.cpp
new file mode 100644
index 0000000..d4df631
--- /dev/null
+++ b/01_introductory_problems/number_spiral_1071.cpp
@@ -0,0 +1,27 @@
1#include <algorithm>
2#include <iostream>
3
4long long f(long long x, long long y) {
5 long long c = std::max(x, y);
6 if (c % 2) {
7 if (y >= x)
8 return (c-1)*(c-1)+x;
9 else
10 return c*c-y+1;
11 } else {
12 if (y >= x)
13 return c*c-x+1;
14 else
15 return (c-1)*(c-1)+y;
16 }
17}
18
19int main() {
20 int t;
21 std::cin >> t;
22 for (int i = 0; i < t; i++) {
23 long long x, y;
24 std::cin >> y >> x;
25 std:: cout << f(x, y) << std::endl;
26 }
27}
diff --git a/01_introductory_problems/palindrome_reorder_1755.cpp b/01_introductory_problems/palindrome_reorder_1755.cpp
new file mode 100644
index 0000000..8ebf4f4
--- /dev/null
+++ b/01_introductory_problems/palindrome_reorder_1755.cpp
@@ -0,0 +1,35 @@
1#include <iostream>
2#include <string>
3
4void printn(char c, int n) {
5 for (int i = 0; i < n; i++)
6 std::cout << c;
7}
8
9int main() {
10 std::string s;
11 int odd{0}, a['Z'+1] = {0};
12 char oddc{0};
13 std::cin >> s;
14 for (auto c : s)
15 a[(size_t)c]++;
16 for (char c = 'A'; c <= 'Z'; c++) {
17 if (a[(size_t)c] % 2) {
18 odd++;
19 oddc = c;
20 }
21 }
22 if (odd > 1) {
23 std::cout << "NO SOLUTION\n";
24 } else {
25 for (char c = 'A'; c <= 'Z'; c++)
26 if (c != oddc)
27 printn(c, a[(size_t)c]/2);
28 if (odd)
29 printn(oddc, a[(size_t)oddc]);
30 for (char c = 'Z'; c >= 'A'; c--)
31 if (c != oddc)
32 printn(c, a[(size_t)c]/2);
33 std::cout << std::endl;
34 }
35}
diff --git a/01_introductory_problems/permutations_1070.cpp b/01_introductory_problems/permutations_1070.cpp
new file mode 100644
index 0000000..4438795
--- /dev/null
+++ b/01_introductory_problems/permutations_1070.cpp
@@ -0,0 +1,15 @@
1#include <iostream>
2
3int main() {
4 int n;
5 std::cin >> n;
6 if (n == 2 || n == 3) {
7 std::cout << "NO SOLUTION" << std::endl;
8 } else {
9 for (int i = 2; i <= n; i += 2)
10 std::cout << i << " ";
11 for (int i = 1; i <= n; i += 2)
12 std::cout << i << " ";
13 std::cout << std::endl;
14 }
15}
diff --git a/01_introductory_problems/raab_game_i_3399.cpp b/01_introductory_problems/raab_game_i_3399.cpp
new file mode 100644
index 0000000..bfa5193
--- /dev/null
+++ b/01_introductory_problems/raab_game_i_3399.cpp
@@ -0,0 +1,67 @@
1#include <iostream>
2#include <utility>
3#include <vector>
4
5class Game {
6public:
7 bool y;
8 std::vector<int> a;
9 std::vector<int> b;
10 int apts = 0;
11 int bpts = 0;
12
13 Game(int n, bool w)
14 : y{w}, a{std::vector<int>(n)}, b{std::vector<int>(n)} {}
15
16 void play(int i, int j) {
17 this->a[this->next] = i;
18 this->b[this->next] = j;
19 this->apts += i > j;
20 this->bpts += j > i;
21 this->next++;
22 }
23
24 friend std::ostream& operator<<(std::ostream& os, const Game g) {
25 if (!g.y) {
26 os << "NO\n";
27 } else {
28 os << "YES\n";
29 for (auto x : g.a)
30 os << x << " ";
31 os << "\n";
32 for (auto x : g.b)
33 os << x << " ";
34 os << "\n";
35 }
36 return os;
37 }
38private:
39 int next = 0;
40};
41
42Game play(int n, int a, int b) {
43 if (a + b > n)
44 return Game(1, false);
45
46 Game g(n, true);
47 for (int i = 0; i < n - (a+b); i++)
48 g.play(i+1, i+1);
49 for (int i = 0; i < a; i++)
50 g.play(n-a+i+1, n-(a+b)+i+1);
51 for (int i = 0; i < b; i++)
52 g.play(n-(a+b)+i+1, n-b+i+1);
53
54 if (g.apts != a || g.bpts != b)
55 g.y = false;
56
57 return g;
58}
59
60int main() {
61 int n, a, b, t;
62 std::cin >> t;
63 for (int i = 0; i < t; i++) {
64 std::cin >> n >> a >> b;
65 std::cout << play(n, a, b);
66 }
67}
diff --git a/01_introductory_problems/repetitions_1069.cpp b/01_introductory_problems/repetitions_1069.cpp
new file mode 100644
index 0000000..790f315
--- /dev/null
+++ b/01_introductory_problems/repetitions_1069.cpp
@@ -0,0 +1,20 @@
1#include <algorithm>
2#include <iostream>
3#include <string>
4
5int main() {
6 char cur{'x'};
7 int n{0}, m{0};
8 std::string str;
9 std::cin >> str;
10 for (auto c : str) {
11 if (c == cur) {
12 n++;
13 } else {
14 m = std::max(m, n);
15 n = 1;
16 cur = c;
17 }
18 }
19 std::cout << std::max(m, n) << std::endl;
20}
diff --git a/01_introductory_problems/string_reorder_1743.cpp b/01_introductory_problems/string_reorder_1743.cpp
new file mode 100644
index 0000000..d22041d
--- /dev/null
+++ b/01_introductory_problems/string_reorder_1743.cpp
@@ -0,0 +1,38 @@
1#include <algorithm>
2#include <array>
3#include <iostream>
4#include <iterator>
5#include <sstream>
6#include <string>
7
8int main() {
9 std::array<size_t, 26> a{};
10 std::string s;
11 std::cin >> s;
12 for (auto c : s) a[c-'A']++;
13
14 size_t i{0}, j{1}, tot{s.size()}, c{99};
15 std::stringstream ss{};
16 while (*std::max_element(a.begin(), a.end()) <= tot / 2) {
17 while (a[i] == 0) i++;
18 while (a[j] == 0 || j <= i) j++;
19 c = i == c ? j : i;
20 ss << (char)('A' + c);
21 a[c]--;
22 tot--;
23 }
24
25 j = std::distance(a.begin(), std::max_element(a.begin(), a.end()));
26 while (a[j] > 1) {
27 if (tot == a[j]) {
28 std::cout << "-1\n";
29 return 0;
30 }
31 while (a[i] == 0 || i == j) i++;
32 ss << (char)('A' + j) << (char)('A' + i);
33 a[i]--;
34 a[j]--;
35 tot -= 2;
36 }
37 std::cout << ss.str() << (char)('A' + j) << "\n";
38}
diff --git a/01_introductory_problems/tower_of_hanoi_2165.cpp b/01_introductory_problems/tower_of_hanoi_2165.cpp
new file mode 100644
index 0000000..2b83788
--- /dev/null
+++ b/01_introductory_problems/tower_of_hanoi_2165.cpp
@@ -0,0 +1,17 @@
1#include <iostream>
2
3void do_hanoi(int n, int l, int m, int r) {
4 if (n != 0) {
5 do_hanoi(n-1, l, r, m);
6 std::cout << l << " " << r << "\n";
7 do_hanoi(n-1, m, l, r);
8 }
9}
10
11int main() {
12 int n;
13 std::cin >> n;
14
15 std::cout << (1 << n) - 1 << "\n";
16 do_hanoi(n, 1, 2, 3);
17}
diff --git a/01_introductory_problems/trailing_zeros_1618.cpp b/01_introductory_problems/trailing_zeros_1618.cpp
new file mode 100644
index 0000000..04cf85b
--- /dev/null
+++ b/01_introductory_problems/trailing_zeros_1618.cpp
@@ -0,0 +1,9 @@
1#include <iostream>
2
3int main() {
4 int n, c{0};
5 std::cin >> n;
6 while (n > 0)
7 c += (n /= 5);
8 std::cout << c << std::endl;
9}
diff --git a/01_introductory_problems/two_knights_1072.cpp b/01_introductory_problems/two_knights_1072.cpp
new file mode 100644
index 0000000..15d5f55
--- /dev/null
+++ b/01_introductory_problems/two_knights_1072.cpp
@@ -0,0 +1,24 @@
1#include <iostream>
2#include <vector>
3
4int main() {
5 long long n, a;
6 std::vector<long long> b = {0, 0, 6, 28, 96};
7 std::cin >> n;
8
9 for (long long k = 1; k <= n; k++) {
10 if (k <= 4) {
11 a = b[k];
12 } else {
13 // Both knights in new strip
14 a += (2*k - 1)*(k - 1) - 2;
15
16 // One knight in new strip, one in previous square
17 const long long s = (k-1)*(k-1);
18 a += 5*(s - 2);
19 a += 4*(s - 3);
20 a += (2*k - 10)*(s - 4);
21 }
22 std::cout << a << std::endl;
23 }
24}
diff --git a/01_introductory_problems/two_sets_1092.cpp b/01_introductory_problems/two_sets_1092.cpp
new file mode 100644
index 0000000..4314654
--- /dev/null
+++ b/01_introductory_problems/two_sets_1092.cpp
@@ -0,0 +1,53 @@
1#include <iostream>
2#include <vector>
3
4void printarr(const std::vector<int>& a) {
5 std::cout << a.size() << std::endl;
6 for (auto x : a)
7 std::cout << x << " ";
8 std::cout << std::endl;
9}
10
11int main() {
12 int n;
13 std::cin >> n;
14
15 switch (n % 4) {
16 case 1:
17 case 2:
18 std::cout << "NO\n";
19 break;
20 case 0:
21 std::cout << "YES\n";
22 {
23 std::vector<int> a(n/2), b(n/2);
24 for (int i = 0; i < n/4; i++) {
25 a[2*i] = 4*i+1;
26 a[2*i+1] = 4*i+4;
27 b[2*i] = 4*i+2;
28 b[2*i+1] = 4*i+3;
29 }
30 printarr(a);
31 printarr(b);
32 }
33 break;
34 case 3:
35 std::cout << "YES\n";
36 if (n == 3) {
37 std::cout << "2\n1 2\n1\n3\n";
38 } else {
39 std::vector<int> a(n/2+1), b(n/2);
40 a[0] = 1; a[1] = 2; a[2] = 4; a[3] = 7;
41 b[0] = 3; b[1] = 5; b[2] = 6;
42 for (int i = 1; i < n/4; i++) {
43 a[2*i+2] = 4*i+4;
44 a[2*i+3] = 4*i+7;
45 b[2*i+1] = 4*i+5;
46 b[2*i+2] = 4*i+6;
47 }
48 printarr(a);
49 printarr(b);
50 }
51 break;
52 }
53}
diff --git a/01_introductory_problems/weird_algorithm_1068.cpp b/01_introductory_problems/weird_algorithm_1068.cpp
new file mode 100644
index 0000000..35cbdfa
--- /dev/null
+++ b/01_introductory_problems/weird_algorithm_1068.cpp
@@ -0,0 +1,12 @@
1#include <iostream>
2
3int main() {
4 long long n;
5 std::cin >> n;
6
7 while (n != 1) {
8 std::cout << n << " ";
9 n = (n % 2) ? n * 3 + 1 : n / 2;
10 }
11 std::cout << 1 << std::endl;
12}

Generated with cgit - Back to sebastiano.tronto.net