From a82ebbc4dabb92b703186adb46096939ef1af867 Mon Sep 17 00:00:00 2001 From: Segcolt <9hmbzr275@mozmail.com> Date: Fri, 4 Oct 2024 17:45:55 -0300 Subject: [PATCH] Add a lot of cses dp problems I had some free time to just do them, they were pretty easy though. --- CSES Problem Set/Array Description.cpp | 119 ++++++++++++++++++++++ CSES Problem Set/Book Shop.cpp | 99 ++++++++++++++++++ CSES Problem Set/Coin Combinations I.cpp | 97 ++++++++++++++++++ CSES Problem Set/Coin Combinations II.cpp | 97 ++++++++++++++++++ CSES Problem Set/Dice Combinations.cpp | 94 +++++++++++++++++ CSES Problem Set/Grid Paths.cpp | 115 +++++++++++++++++++++ CSES Problem Set/Minimizing Coins.cpp | 96 +++++++++++++++++ CSES Problem Set/Removing Digits.cpp | 93 +++++++++++++++++ 8 files changed, 810 insertions(+) create mode 100644 CSES Problem Set/Array Description.cpp create mode 100644 CSES Problem Set/Book Shop.cpp create mode 100644 CSES Problem Set/Coin Combinations I.cpp create mode 100644 CSES Problem Set/Coin Combinations II.cpp create mode 100644 CSES Problem Set/Dice Combinations.cpp create mode 100644 CSES Problem Set/Grid Paths.cpp create mode 100644 CSES Problem Set/Minimizing Coins.cpp create mode 100644 CSES Problem Set/Removing Digits.cpp diff --git a/CSES Problem Set/Array Description.cpp b/CSES Problem Set/Array Description.cpp new file mode 100644 index 0000000..96ba617 --- /dev/null +++ b/CSES Problem Set/Array Description.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://cses.fi/problemset/task/1746 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi nums(n); + cin >> nums; + + vvl dp(n, vl(m + 1, 0)); + if (nums[0] > 0) { + dp[0][nums[0]] = 1; + } else { + fill(dp[0].begin(), dp[0].end(), 1); + dp[0][0] = 0; + } + + nrep(i, 1, n) { + if (nums[i] > 0) { + ll ans = dp[i - 1][nums[i]]; + if (nums[i] < m) { + ans += dp[i - 1][nums[i] + 1]; + } + if (nums[i] > 1) { + ans += dp[i - 1][nums[i] - 1]; + } + dp[i][nums[i]] = ans % mod; + continue; + } + + for (size_t j = 1; j <= m; j++) { + dp[i][j] += dp[i - 1][j]; + if (j > 1) { + dp[i][j] += dp[i - 1][j - 1]; + } + if (j < m) { + dp[i][j] += dp[i - 1][j + 1]; + } + dp[i][j] %= mod; + } + } + + cout << accumulate(dp[n - 1].begin(), dp[n - 1].end(), 0LL) % mod << '\n'; +} diff --git a/CSES Problem Set/Book Shop.cpp b/CSES Problem Set/Book Shop.cpp new file mode 100644 index 0000000..76b02ba --- /dev/null +++ b/CSES Problem Set/Book Shop.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://cses.fi/problemset/task/1158 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int inf = INT32_MAX >> 1; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, x; + cin >> n >> x; + + vi cost(n); + vi pages(n); + cin >> cost >> pages; + + vvi dp(n + 1, vi(x + 1, 0)); + + for (size_t i = 1; i <= n; i++) { + for (size_t j = 0; j <= x; j++) { + if (cost[i - 1] > j) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cost[i - 1]] + pages[i - 1]); + } + } + } + + cout << dp[n][x] << '\n'; +} diff --git a/CSES Problem Set/Coin Combinations I.cpp b/CSES Problem Set/Coin Combinations I.cpp new file mode 100644 index 0000000..7dd4e81 --- /dev/null +++ b/CSES Problem Set/Coin Combinations I.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://cses.fi/problemset/task/1635 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, x; + cin >> n >> x; + vi coins(n); + cin >> coins; + + vl dp(x + 1, 0); + dp[0] = 1; + + for (size_t i = 1; i <= x; i++) { + for (auto j : coins) { + if (j <= i) { + dp[i] += dp[i - j]; + } + } + dp[i] %= mod; + } + + cout << dp[x] << '\n'; +} diff --git a/CSES Problem Set/Coin Combinations II.cpp b/CSES Problem Set/Coin Combinations II.cpp new file mode 100644 index 0000000..16e183f --- /dev/null +++ b/CSES Problem Set/Coin Combinations II.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://cses.fi/problemset/task/1636 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, x; + cin >> n >> x; + vi coins(n); + cin >> coins; + + vl dp(x + 1, 0); + dp[0] = 1; + + for (auto j : coins) { + for (size_t i = 1; i <= x; i++) { + if (j <= i) { + dp[i] += dp[i - j]; + } + dp[i] %= mod; + } + } + + cout << dp[x] << '\n'; +} diff --git a/CSES Problem Set/Dice Combinations.cpp b/CSES Problem Set/Dice Combinations.cpp new file mode 100644 index 0000000..042de40 --- /dev/null +++ b/CSES Problem Set/Dice Combinations.cpp @@ -0,0 +1,94 @@ +/* Problem URL: https://cses.fi/problemset/task/1633 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl dp(n + 1, 0); + dp[0] = 1; + for (size_t i = 1; i <= n; i++) { + for (size_t j = 1; j <= 6; j++) { + if (i >= j) { + dp[i] += dp[i - j]; + } + } + dp[i] %= mod; + } + + cout << dp[n] << '\n'; +} diff --git a/CSES Problem Set/Grid Paths.cpp b/CSES Problem Set/Grid Paths.cpp new file mode 100644 index 0000000..975db98 --- /dev/null +++ b/CSES Problem Set/Grid Paths.cpp @@ -0,0 +1,115 @@ +/* Problem URL: https://cses.fi/problemset/task/1638 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + vvl dp(n, vl(n, 0)); + + rep(i, n) { + rep(j, n) { + char c; + cin >> c; + if (c == '*') { + dp[i][j] = -1; + } + } + } + + if (dp[0][0] == -1) { + cout << "0\n"; + return 0; + } + dp[0][0] = 1; + + rep(i, n) { + rep(j, n) { + if (dp[i][j] == -1) { + continue; + } + if (i > 0 && dp[i - 1][j] != -1) { + dp[i][j] += dp[i - 1][j]; + } + if (j > 0 && dp[i][j - 1] != -1) { + dp[i][j] += dp[i][j - 1]; + } + dp[i][j] %= mod; + } + } + + cout << (dp[n - 1][n - 1] == -1 ? 0 : dp[n - 1][n - 1]) << '\n'; +} diff --git a/CSES Problem Set/Minimizing Coins.cpp b/CSES Problem Set/Minimizing Coins.cpp new file mode 100644 index 0000000..037fe11 --- /dev/null +++ b/CSES Problem Set/Minimizing Coins.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://cses.fi/problemset/task/1634 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int inf = INT32_MAX >> 1; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, x; + cin >> n >> x; + + vi coins(n); + cin >> coins; + vi dp(x + 1, inf); + dp[0] = 0; + + for (size_t i = 1; i <= x; i++) { + for (auto j : coins) { + if (i >= j) { + rmin(dp[i], dp[i - j] + 1); + } + } + } + + cout << (dp[x] != inf ? dp[x] : -1) << '\n'; +} diff --git a/CSES Problem Set/Removing Digits.cpp b/CSES Problem Set/Removing Digits.cpp new file mode 100644 index 0000000..f01d33a --- /dev/null +++ b/CSES Problem Set/Removing Digits.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://cses.fi/problemset/task/1637 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int inf = INT32_MAX >> 1; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi dp(n + 1, inf); + dp[0] = 0; + for (size_t i = 1; i <= n; i++) { + int res = i; + while (res > 0) { + rmin(dp[i], dp[i - (res % 10)] + 1); + res /= 10; + } + } + + cout << dp[n] << '\n'; +}