From e0a83335912fe56eb2905ddc0634973ad5552951 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 20 Apr 2025 12:15:26 -0400 Subject: [PATCH] feat(cses): more dp --- cses/dynamic-programming/.clang-format | 9 ++ cses/dynamic-programming/.clangd | 34 ++++++ cses/dynamic-programming/array-description.cc | 114 ++++++++++++++++++ cses/dynamic-programming/book-shop.cc | 93 ++++++++++++++ .../coin-combinations-i.cc | 96 +++++++++++++++ .../coin-combinations-ii.cc | 94 +++++++++++++++ cses/dynamic-programming/compile_flags.txt | 31 +++++ cses/dynamic-programming/debug_flags.txt | 12 ++ cses/dynamic-programming/dice-combinations.cc | 90 ++++++++++++++ cses/dynamic-programming/grid-paths.cc | 97 +++++++++++++++ .../io/array-description.in | 2 + .../io/array-description.out | 4 + cses/dynamic-programming/io/book-shop.in | 3 + cses/dynamic-programming/io/book-shop.out | 4 + .../io/coin-combinations-i.in | 2 + .../io/coin-combinations-i.out | 4 + .../io/coin-combinations-ii.in | 2 + .../io/coin-combinations-ii.out | 4 + .../io/dice-combinations.in | 1 + .../io/dice-combinations.out | 4 + cses/dynamic-programming/io/grid-paths.in | 5 + cses/dynamic-programming/io/grid-paths.out | 4 + .../io/minimizing-coins.in | 2 + .../io/minimizing-coins.out | 4 + .../dynamic-programming/io/removing-digits.in | 1 + .../io/removing-digits.out | 4 + cses/dynamic-programming/makefile | 28 +++++ cses/dynamic-programming/minimizing-coins.cc | 98 +++++++++++++++ cses/dynamic-programming/removing-digits.cc | 95 +++++++++++++++ cses/dynamic-programming/scripts/debug.sh | 29 +++++ cses/dynamic-programming/scripts/run.sh | 29 +++++ cses/dynamic-programming/scripts/utils.sh | 53 ++++++++ 32 files changed, 1052 insertions(+) create mode 100644 cses/dynamic-programming/.clang-format create mode 100644 cses/dynamic-programming/.clangd create mode 100644 cses/dynamic-programming/array-description.cc create mode 100644 cses/dynamic-programming/book-shop.cc create mode 100644 cses/dynamic-programming/coin-combinations-i.cc create mode 100644 cses/dynamic-programming/coin-combinations-ii.cc create mode 100644 cses/dynamic-programming/compile_flags.txt create mode 100644 cses/dynamic-programming/debug_flags.txt create mode 100644 cses/dynamic-programming/dice-combinations.cc create mode 100644 cses/dynamic-programming/grid-paths.cc create mode 100644 cses/dynamic-programming/io/array-description.in create mode 100644 cses/dynamic-programming/io/array-description.out create mode 100644 cses/dynamic-programming/io/book-shop.in create mode 100644 cses/dynamic-programming/io/book-shop.out create mode 100644 cses/dynamic-programming/io/coin-combinations-i.in create mode 100644 cses/dynamic-programming/io/coin-combinations-i.out create mode 100644 cses/dynamic-programming/io/coin-combinations-ii.in create mode 100644 cses/dynamic-programming/io/coin-combinations-ii.out create mode 100644 cses/dynamic-programming/io/dice-combinations.in create mode 100644 cses/dynamic-programming/io/dice-combinations.out create mode 100644 cses/dynamic-programming/io/grid-paths.in create mode 100644 cses/dynamic-programming/io/grid-paths.out create mode 100644 cses/dynamic-programming/io/minimizing-coins.in create mode 100644 cses/dynamic-programming/io/minimizing-coins.out create mode 100644 cses/dynamic-programming/io/removing-digits.in create mode 100644 cses/dynamic-programming/io/removing-digits.out create mode 100644 cses/dynamic-programming/makefile create mode 100644 cses/dynamic-programming/minimizing-coins.cc create mode 100644 cses/dynamic-programming/removing-digits.cc create mode 100644 cses/dynamic-programming/scripts/debug.sh create mode 100644 cses/dynamic-programming/scripts/run.sh create mode 100644 cses/dynamic-programming/scripts/utils.sh diff --git a/cses/dynamic-programming/.clang-format b/cses/dynamic-programming/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/cses/dynamic-programming/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/cses/dynamic-programming/.clangd b/cses/dynamic-programming/.clangd new file mode 100644 index 0000000..5b59a31 --- /dev/null +++ b/cses/dynamic-programming/.clangd @@ -0,0 +1,34 @@ +CompileFlags: + Add: + -O2 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wsign-conversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + -Wnull-dereference + -Wformat=2 + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef + -DLOCAL + -std=c++20 + -Wno-unknown-pragmas diff --git a/cses/dynamic-programming/array-description.cc b/cses/dynamic-programming/array-description.cc new file mode 100644 index 0000000..519ff02 --- /dev/null +++ b/cses/dynamic-programming/array-description.cc @@ -0,0 +1,114 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +static constexpr u64 MOD = 1e9 + 7; + +void solve() { + u32 n, m; + cin >> n >> m; + vec a(n); + for (auto& e : a) + cin >> e; + + vec prev(m + 1, 0); + if (a[0] != 0) { + prev[a[0]] = 1; + } else { + for (u32 i = 1; i <= m; ++i) + prev[i] = 1; + } + + for (u32 i = 2; i <= n; ++i) { + vec cur(m + 1, 0); + if (a[i - 1] == 0) { + for (u32 x = 1; x <= m; ++x) { + for (i64 delta = -1; delta <= 1; ++delta) { + if (1 <= x + delta && x + delta <= m) + cur[x] = (cur[x] + prev[x + delta]) % MOD; + } + } + } else { + for (i64 delta = -1; delta <= 1; ++delta) { + if (1 <= a[i - 1] + delta && a[i - 1] + delta <= m) + cur[a[i - 1]] = (cur[a[i - 1]] + prev[a[i - 1] + delta]) % MOD; + } + } + prev = cur; + } + + i64 ans = 0; + for (auto e : prev) { + ans = (ans + e) % MOD; + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/book-shop.cc b/cses/dynamic-programming/book-shop.cc new file mode 100644 index 0000000..4e5bd6f --- /dev/null +++ b/cses/dynamic-programming/book-shop.cc @@ -0,0 +1,93 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u64 n, x; + cin >> n >> x; + vec pages(n), prices(n); + for (auto& e : prices) + cin >> e; + for (auto& e : pages) + cin >> e; + + vec dp(x + 1, 0); + + for (u64 i = 1; i <= n; ++i) { + for (u64 j = x; j >= 1; --j) { + if (j >= prices[i - 1]) + dp[j] = max(dp[j], dp[j - prices[i - 1]] + pages[i - 1]); + } + } + + cout << dp[x] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/coin-combinations-i.cc b/cses/dynamic-programming/coin-combinations-i.cc new file mode 100644 index 0000000..9466927 --- /dev/null +++ b/cses/dynamic-programming/coin-combinations-i.cc @@ -0,0 +1,96 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +static constexpr u64 MOD = 1e9 + 7; + +void solve() { + u64 n, x; + cin >> n >> x; + vec c(n); + for (auto& e : c) + cin >> e; + sort(all(c)); + + vec distinct(x + 1, 0); + distinct[0] = 1; + + for (u64 i = 1; i <= x; ++i) { + for (u64 j = 0; j < n && c[j] <= i; ++j) { + if (distinct[i - c[j]] != 0) { + distinct[i] = (distinct[i] + distinct[i - c[j]]) % MOD; + } + } + } + + cout << distinct[x] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/coin-combinations-ii.cc b/cses/dynamic-programming/coin-combinations-ii.cc new file mode 100644 index 0000000..fc6995d --- /dev/null +++ b/cses/dynamic-programming/coin-combinations-ii.cc @@ -0,0 +1,94 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +static constexpr u64 MOD = 1e9 + 7; + +void solve() { + u64 n, x; + cin >> n >> x; + vec c(n); + for (auto& e : c) { + cin >> e; + } + sort(all(c)); + + vec ways(x + 1, 0); + ways[0] = 1; + for (u32 i = 0; i < n; ++i) { + for (u64 j = c[i]; j <= x; ++j) { + ways[j] = (ways[j] + ways[j - c[i]]) % MOD; + } + } + + cout << ways[x] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/compile_flags.txt b/cses/dynamic-programming/compile_flags.txt new file mode 100644 index 0000000..5725a8c --- /dev/null +++ b/cses/dynamic-programming/compile_flags.txt @@ -0,0 +1,31 @@ +-O2 +-Wall +-Wextra +-Wpedantic +-Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++23 diff --git a/cses/dynamic-programming/debug_flags.txt b/cses/dynamic-programming/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/cses/dynamic-programming/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/cses/dynamic-programming/dice-combinations.cc b/cses/dynamic-programming/dice-combinations.cc new file mode 100644 index 0000000..116f8e5 --- /dev/null +++ b/cses/dynamic-programming/dice-combinations.cc @@ -0,0 +1,90 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db +#define dbln +#endif +// }}} + +constexpr u64 MOD = 1e9 + 7; + +void solve() { + u64 n; + cin >> n; + + u64 six = 6; + vec toss(n + 1, 0); + toss[0] = 1; + for (u64 i = 1; i <= n; ++i) { + for (u64 j = 1; j <= min(six, i); ++j) { + toss[i] = (toss[i] + toss[i - j]) % MOD; + } + } + + cout << toss[n] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/grid-paths.cc b/cses/dynamic-programming/grid-paths.cc new file mode 100644 index 0000000..2fb8069 --- /dev/null +++ b/cses/dynamic-programming/grid-paths.cc @@ -0,0 +1,97 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +static constexpr u64 MOD = 1e9 + 7; + +void solve() { + u64 n; + cin >> n; + vec board(n); + for (auto& e : board) + cin >> e; + + vec moves(n + 1, 0); + moves[1] = board[0][0] == '.'; + + for (u64 r = 1; r <= n; ++r) { + for (u64 c = 1; c <= n; ++c) { + if (board[r - 1][c - 1] == '*') { + moves[c] = 0; + } else { + moves[c] = (moves[c - 1] + moves[c]) % MOD; + } + } + } + + cout << moves[n] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/io/array-description.in b/cses/dynamic-programming/io/array-description.in new file mode 100644 index 0000000..fb7a8f3 --- /dev/null +++ b/cses/dynamic-programming/io/array-description.in @@ -0,0 +1,2 @@ +10 3 +0 0 0 0 0 0 0 0 0 0 diff --git a/cses/dynamic-programming/io/array-description.out b/cses/dynamic-programming/io/array-description.out new file mode 100644 index 0000000..4d99245 --- /dev/null +++ b/cses/dynamic-programming/io/array-description.out @@ -0,0 +1,4 @@ +8119 + +[code]: 0 +[time]: 3.31473 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/book-shop.in b/cses/dynamic-programming/io/book-shop.in new file mode 100644 index 0000000..db9738d --- /dev/null +++ b/cses/dynamic-programming/io/book-shop.in @@ -0,0 +1,3 @@ +4 10 +4 8 5 3 +5 12 8 1 diff --git a/cses/dynamic-programming/io/book-shop.out b/cses/dynamic-programming/io/book-shop.out new file mode 100644 index 0000000..ab175b2 --- /dev/null +++ b/cses/dynamic-programming/io/book-shop.out @@ -0,0 +1,4 @@ +13 + +[code]: 0 +[time]: 4.72617 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/coin-combinations-i.in b/cses/dynamic-programming/io/coin-combinations-i.in new file mode 100644 index 0000000..2b19196 --- /dev/null +++ b/cses/dynamic-programming/io/coin-combinations-i.in @@ -0,0 +1,2 @@ +3 9 +2 3 5 diff --git a/cses/dynamic-programming/io/coin-combinations-i.out b/cses/dynamic-programming/io/coin-combinations-i.out new file mode 100644 index 0000000..6cf5a56 --- /dev/null +++ b/cses/dynamic-programming/io/coin-combinations-i.out @@ -0,0 +1,4 @@ +8 + +[code]: 0 +[time]: 4.17066 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/coin-combinations-ii.in b/cses/dynamic-programming/io/coin-combinations-ii.in new file mode 100644 index 0000000..2b19196 --- /dev/null +++ b/cses/dynamic-programming/io/coin-combinations-ii.in @@ -0,0 +1,2 @@ +3 9 +2 3 5 diff --git a/cses/dynamic-programming/io/coin-combinations-ii.out b/cses/dynamic-programming/io/coin-combinations-ii.out new file mode 100644 index 0000000..d33aa2c --- /dev/null +++ b/cses/dynamic-programming/io/coin-combinations-ii.out @@ -0,0 +1,4 @@ +3 + +[code]: 0 +[time]: 4.31561 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/dice-combinations.in b/cses/dynamic-programming/io/dice-combinations.in new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/cses/dynamic-programming/io/dice-combinations.in @@ -0,0 +1 @@ +3 diff --git a/cses/dynamic-programming/io/dice-combinations.out b/cses/dynamic-programming/io/dice-combinations.out new file mode 100644 index 0000000..0078d1f --- /dev/null +++ b/cses/dynamic-programming/io/dice-combinations.out @@ -0,0 +1,4 @@ +4 + +[code]: 0 +[time]: 4.10962 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/grid-paths.in b/cses/dynamic-programming/io/grid-paths.in new file mode 100644 index 0000000..eff7309 --- /dev/null +++ b/cses/dynamic-programming/io/grid-paths.in @@ -0,0 +1,5 @@ +4 +.... +.*.. +...* +*... diff --git a/cses/dynamic-programming/io/grid-paths.out b/cses/dynamic-programming/io/grid-paths.out new file mode 100644 index 0000000..83e1e38 --- /dev/null +++ b/cses/dynamic-programming/io/grid-paths.out @@ -0,0 +1,4 @@ +3 + +[code]: 0 +[time]: 4.26126 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/minimizing-coins.in b/cses/dynamic-programming/io/minimizing-coins.in new file mode 100644 index 0000000..e1c3cea --- /dev/null +++ b/cses/dynamic-programming/io/minimizing-coins.in @@ -0,0 +1,2 @@ +1 1000000 +1 diff --git a/cses/dynamic-programming/io/minimizing-coins.out b/cses/dynamic-programming/io/minimizing-coins.out new file mode 100644 index 0000000..6cd5fbc --- /dev/null +++ b/cses/dynamic-programming/io/minimizing-coins.out @@ -0,0 +1,4 @@ +1000000 + +[code]: 0 +[time]: 5.76615 ms \ No newline at end of file diff --git a/cses/dynamic-programming/io/removing-digits.in b/cses/dynamic-programming/io/removing-digits.in new file mode 100644 index 0000000..f64f5d8 --- /dev/null +++ b/cses/dynamic-programming/io/removing-digits.in @@ -0,0 +1 @@ +27 diff --git a/cses/dynamic-programming/io/removing-digits.out b/cses/dynamic-programming/io/removing-digits.out new file mode 100644 index 0000000..f24b8d3 --- /dev/null +++ b/cses/dynamic-programming/io/removing-digits.out @@ -0,0 +1,4 @@ +5 + +[code]: 0 +[time]: 4.68349 ms \ No newline at end of file diff --git a/cses/dynamic-programming/makefile b/cses/dynamic-programming/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/cses/dynamic-programming/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/cses/dynamic-programming/minimizing-coins.cc b/cses/dynamic-programming/minimizing-coins.cc new file mode 100644 index 0000000..7d74b31 --- /dev/null +++ b/cses/dynamic-programming/minimizing-coins.cc @@ -0,0 +1,98 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u64 n, x; + cin >> n >> x; + vec c(n); + + for (auto& e : c) + cin >> e; + sort(all(c)); + + vec make(x + 1, MAX); + make[0] = 0; + + for (u64 i = 1; i <= x; ++i) { + for (u32 j = 0; j < n && c[j] <= i; ++j) { + if (make[i - c[j]] != MAX) + make[i] = min(make[i], 1 + make[i - c[j]]); + } + } + + if (make[x] == MAX) { + cout << "-1\n"; + } else { + cout << make[x] << '\n'; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/removing-digits.cc b/cses/dynamic-programming/removing-digits.cc new file mode 100644 index 0000000..5c09358 --- /dev/null +++ b/cses/dynamic-programming/removing-digits.cc @@ -0,0 +1,95 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u64 n; + cin >> n; + + auto biggest = [](u64 x) -> u64 { + u64 ans = 0; + while (x) { + ans = max(ans, x % 10); + x /= 10; + } + return ans; + }; + + u64 ans = 0; + + while (n) { + ++ans; + n -= biggest(n); + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + // cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/dynamic-programming/scripts/debug.sh b/cses/dynamic-programming/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/cses/dynamic-programming/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/dynamic-programming/scripts/run.sh b/cses/dynamic-programming/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/cses/dynamic-programming/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/dynamic-programming/scripts/utils.sh b/cses/dynamic-programming/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/cses/dynamic-programming/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +}