diff --git a/cses/introductory-problems/.clangd b/cses/introductory-problems/.clangd index 4f8ead9..610a3b1 100644 --- a/cses/introductory-problems/.clangd +++ b/cses/introductory-problems/.clangd @@ -5,4 +5,4 @@ CompileFlags: - -Wpedantic - -Wshadow - -DLOCAL - - -Wno-unknown-pragmas \ No newline at end of file + - -Wno-unknown-pragmas -std=c++23 diff --git a/cses/introductory-problems/apple-division.cc b/cses/introductory-problems/apple-division.cc index dda14ae..d9feaa0 100644 --- a/cses/introductory-problems/apple-division.cc +++ b/cses/introductory-problems/apple-division.cc @@ -7,60 +7,69 @@ using namespace std; -template -constexpr T MIN = std::numeric_limits::min(); +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; -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()); -} - -using ll = long long; -using ld = long double; -template -using vec = std::vector; -template -using arr = std::array; - -#define ff first -#define ss second -#define eb emplace_back -#define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif // }}} void solve() { - int n; + u32 n; cin >> n; - vec a(n); - for (auto& e : a) { + vector a(n); + for (auto& e : a) cin >> e; - } - auto rec = [&](auto&& self, int i, ll l, ll r) { - if (i == n) { - return abs(r - l); + // auto dfs = [&](u32 i, u64 s1, auto&& self) -> u64 { + // if (i == n) { + // return max(s1, s2) - min(s1, s2); + // } + // + // return min(self(i + 1, s1 + a[i], self), + // self(i + 1, s1,self)); + // }; + // + // cout << dfs(0, 0, dfs) << '\n'; + + u64 total = accumulate(all(a), 0LL); + u64 ans = total; + for (u64 mask = 0; mask < (1LL << n); ++mask) { + u64 sum = 0; + for (u64 bit = 0; bit < n; ++bit) { + if (mask & (1LL << bit)) { + sum += a[bit]; + } } - - return min(self(self, i + 1, l + a[i], r), self(self, i + 1, l, r + a[i])); - }; - - cout << rec(rec, 0, 0, 0); + ans = min(ans, max(total - sum, sum) - min(total - sum, sum)); + } + cout << ans << '\n'; } int main() { // {{{ cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); - solve(); + u32 tc = 1; + // cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } return 0; } diff --git a/cses/introductory-problems/compile_flags.txt b/cses/introductory-problems/compile_flags.txt index af7fb72..5373b01 100644 --- a/cses/introductory-problems/compile_flags.txt +++ b/cses/introductory-problems/compile_flags.txt @@ -16,7 +16,6 @@ -Wunused -Woverloaded-virtual -Wconversion --Wsign-conversion -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches @@ -28,4 +27,4 @@ -Wdouble-promotion -Wundef -DLOCAL --std=c++20 +-std=c++23 diff --git a/cses/introductory-problems/io/apple-division.in b/cses/introductory-problems/io/apple-division.in index 4f8bb59..b9679f8 100644 --- a/cses/introductory-problems/io/apple-division.in +++ b/cses/introductory-problems/io/apple-division.in @@ -1,2 +1,2 @@ -3 -7 7 100 +5 +3 2 7 4 1 diff --git a/cses/introductory-problems/io/apple-division.out b/cses/introductory-problems/io/apple-division.out index c503bcc..28064e2 100644 --- a/cses/introductory-problems/io/apple-division.out +++ b/cses/introductory-problems/io/apple-division.out @@ -1,3 +1,4 @@ -86 +1 + [code]: 0 -[time]: 3.65114 ms \ No newline at end of file +[time]: 4.19974 ms \ No newline at end of file diff --git a/cses/introductory-problems/makefile b/cses/introductory-problems/makefile index 9c5450b..78778b6 100644 --- a/cses/introductory-problems/makefile +++ b/cses/introductory-problems/makefile @@ -1,5 +1,7 @@ .PHONY: run debug clean setup init +VERSION ?= 20 + SRC = $(word 2,$(MAKECMDGOALS)) .SILENT: @@ -17,9 +19,8 @@ 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 . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd init: make setup diff --git a/usaco/bronze/complete-search/.clangd b/usaco/bronze/complete-search/.clangd new file mode 100644 index 0000000..0f141ad --- /dev/null +++ b/usaco/bronze/complete-search/.clangd @@ -0,0 +1,37 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++17 + -std=c++17 + -std=c++17 diff --git a/usaco/bronze/complete-search/air-cownditioning-ii.cc b/usaco/bronze/complete-search/air-cownditioning-ii.cc new file mode 100644 index 0000000..9784086 --- /dev/null +++ b/usaco/bronze/complete-search/air-cownditioning-ii.cc @@ -0,0 +1,126 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 201703L +template constexpr T MIN = std::numeric_limits::min(); + +template constexpr T MAX = std::numeric_limits::max(); + +template [[nodiscard]] T sc(U &&u) { + return static_cast(std::forward(u)); +} + +template [[nodiscard]] T sz(C const &c) { + return static_cast(c.size()); +} +#endif + +#if defined(LOCAL) && __cplusplus >= 201703L +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif + +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() +#define ff first +#define ss second +// }}} + +struct ac { + u32 l, r; + i32 p, m; + // NOTE: copied from AI - order doesn't matter, just need to consider all + // permutations + bool operator<(ac const &o) const { + return tie(l, r, p, m) < tie(o.l, o.r, o.p, m); + } +}; + +void solve() { + u32 N, M; + cin >> N >> M; + + vector to_cool(101, 0); + + u32 l, r, c, m; + for (u32 i = 0; i < N; ++i) { + cin >> l >> r >> c; + + for (u32 j = l; j <= r; ++j) { + to_cool[j] = c; + } + } + + vector acs; + for (u32 i = 0; i < M; ++i) { + cin >> l >> r >> c >> m; + + acs.emplace_back(ac{l, r, c, m}); + } + + sort(all(acs)); + + u32 ans = MAX; + for (u32 mask = 0; mask < 1LL << M; ++mask) { + u32 tc = MAX; + auto temps = to_cool; + + u32 cost = 0; + // subsets != permutation/order + // want to track use/lack of use + for (u32 bit = 0; bit < M; ++bit) { + if (!(mask & (1 << bit))) + continue; + auto &a = acs[bit]; + cost += a.m; + for (u32 j = a.l; j <= min((u32)100, a.r); ++j) { + temps[j] -= a.p; + } + if (all_of(all(temps), [](i32 e) { return e <= 0; })) { + ans = min(ans, cost); + break; + } + } + + ans = min(ans, tc); + } + + cout << ans << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "air-cownditioning-ii" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/balancing.cc b/usaco/bronze/complete-search/balancing.cc new file mode 100644 index 0000000..3a54318 --- /dev/null +++ b/usaco/bronze/complete-search/balancing.cc @@ -0,0 +1,100 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, B; + cin >> n >> B; + + vector> pts(n); + vector horiz_placements, vert_placements; + + for (u32 i = 0; i < n; ++i) { + cin >> pts[i].ff >> pts[i].ss; + vert_placements.emplace_back(pts[i].ff + 1); + horiz_placements.emplace_back(pts[i].ss + 1); + } + + u64 ans = numeric_limits::max(); + for (auto vp : vert_placements) { + for (auto hp : horiz_placements) { + u64 tl = 0, tr = 0, bl = 0, br = 0; + for (auto &[x, y] : pts) { + if (x < vp && y > hp) + ++tl; + if (x > vp && y > hp) + ++tr; + if (x < vp && y < hp) + ++bl; + if (x > vp && y < hp) + ++br; + } + ans = min(ans, max({tl, tr, bl, br})); + } + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "balancing" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/circlecross.cc b/usaco/bronze/complete-search/circlecross.cc new file mode 100644 index 0000000..1e62df2 --- /dev/null +++ b/usaco/bronze/complete-search/circlecross.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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + string s; + cin >> s; + array, 26> enter_exit{}; + for (u32 i = 0; i < s.size(); ++i) { + u32 j = s[i] - 'A'; + if (enter_exit[j].ff == 0) { + enter_exit[j].ff = i + 1; + } else { + enter_exit[j].ss = i + 1; + } + } + + u32 ans = 0; + for (u32 r = 0; r < enter_exit.size(); ++r) { + for (u32 l = 0; l < r; ++l) { + if (enter_exit[l].ff < enter_exit[r].ff && + enter_exit[r].ff < enter_exit[l].ss && + enter_exit[l].ss < enter_exit[r].ss || + enter_exit[r].ff < enter_exit[l].ff && + enter_exit[l].ff < enter_exit[r].ss && + enter_exit[r].ss < enter_exit[l].ss) { + ++ans; + } + } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "circlecross" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/compile_flags.txt b/usaco/bronze/complete-search/compile_flags.txt similarity index 95% rename from usaco/bronze/compile_flags.txt rename to usaco/bronze/complete-search/compile_flags.txt index c6f2a50..785091b 100644 --- a/usaco/bronze/compile_flags.txt +++ b/usaco/bronze/complete-search/compile_flags.txt @@ -16,7 +16,6 @@ -Wunused -Woverloaded-virtual -Wconversion --Wsign-conversion -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches @@ -28,3 +27,4 @@ -Wdouble-promotion -Wundef -DLOCAL +-std=c++17 diff --git a/usaco/bronze/complete-search/cownomics.cc b/usaco/bronze/complete-search/cownomics.cc new file mode 100644 index 0000000..0df1d8c --- /dev/null +++ b/usaco/bronze/complete-search/cownomics.cc @@ -0,0 +1,91 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + array, 2> cows{vector(n), vector(n)}; + for (auto &e : cows) + for (auto &g : e) + cin >> g; + + u32 ans = 0; + for (u32 col = 0; col < m; ++col) { + unordered_set plain; + for (u32 i = 0; i < n; ++i) + plain.insert(cows[0][i][col]); + u32 i; + for (i = 0; i < n; ++i) + if (plain.find(cows[1][i][col]) != plain.end()) { + break; + } + ans += i == n; + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "cownomics" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/debug_flags.txt b/usaco/bronze/complete-search/debug_flags.txt new file mode 100644 index 0000000..af1b521 --- /dev/null +++ b/usaco/bronze/complete-search/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++20 diff --git a/usaco/bronze/complete-search/diamond.cc b/usaco/bronze/complete-search/diamond.cc new file mode 100644 index 0000000..186effc --- /dev/null +++ b/usaco/bronze/complete-search/diamond.cc @@ -0,0 +1,87 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, k; + cin >> n >> k; + + vector diamonds(n); + for (auto &e : diamonds) + cin >> e; + sort(all(diamonds)); + + u32 r = 0; + u32 ans = 0; + for (u32 l = 0; l < n; ++l) { + while (r < n && diamonds[r] - diamonds[l] <= k) { + ++r; + } + ans = max(ans, r - l); + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "diamond" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/gymnastics.cc b/usaco/bronze/complete-search/gymnastics.cc new file mode 100644 index 0000000..90704bb --- /dev/null +++ b/usaco/bronze/complete-search/gymnastics.cc @@ -0,0 +1,102 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 k, n; + cin >> k >> n; + + vector> rounds(k, vector(n)); + + for (auto &e : rounds) + for (auto &g : e) + cin >> g; + + u32 ans = 0; + + auto consistent = [&](u32 c1, u32 c2) { + for (u32 i = 0; i < k; ++i) { + bool found_c2 = false; + for (u32 j = 0; j < n; ++j) { + if (rounds[i][j] == c1 && found_c2) { + return false; + } + found_c2 |= rounds[i][j] == c2; + } + } + return true; + }; + + for (u32 i = 1; i <= n; ++i) { + for (u32 j = 1; j < i; ++j) { + if (consistent(i, j) || consistent(j, i)) + ++ans; + } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "gymnastics" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/io/air-cownditioning-ii.in b/usaco/bronze/complete-search/io/air-cownditioning-ii.in new file mode 100644 index 0000000..fe32e19 --- /dev/null +++ b/usaco/bronze/complete-search/io/air-cownditioning-ii.in @@ -0,0 +1,7 @@ +2 4 +1 5 2 +7 9 3 +2 9 2 3 +1 6 2 8 +1 2 4 2 +6 9 1 5 diff --git a/usaco/bronze/complete-search/io/air-cownditioning-ii.out b/usaco/bronze/complete-search/io/air-cownditioning-ii.out new file mode 100644 index 0000000..fc1c91e --- /dev/null +++ b/usaco/bronze/complete-search/io/air-cownditioning-ii.out @@ -0,0 +1,4 @@ +10 + +[code]: 0 +[time]: 4.50897 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/balancing.in b/usaco/bronze/complete-search/io/balancing.in new file mode 100644 index 0000000..904c20e --- /dev/null +++ b/usaco/bronze/complete-search/io/balancing.in @@ -0,0 +1,8 @@ +7 10 +7 3 +5 5 +9 7 +3 1 +7 7 +5 3 +9 1 diff --git a/usaco/bronze/complete-search/io/balancing.out b/usaco/bronze/complete-search/io/balancing.out new file mode 100644 index 0000000..ed5725f --- /dev/null +++ b/usaco/bronze/complete-search/io/balancing.out @@ -0,0 +1,4 @@ +2 + +[code]: 0 +[time]: 5.50079 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/circlecross.in b/usaco/bronze/complete-search/io/circlecross.in new file mode 100644 index 0000000..28dbab8 --- /dev/null +++ b/usaco/bronze/complete-search/io/circlecross.in @@ -0,0 +1 @@ +ABCCABDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ diff --git a/usaco/bronze/complete-search/io/circlecross.out b/usaco/bronze/complete-search/io/circlecross.out new file mode 100644 index 0000000..7e5a07d --- /dev/null +++ b/usaco/bronze/complete-search/io/circlecross.out @@ -0,0 +1,4 @@ +1 + +[code]: 0 +[time]: 4.23932 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/cownomics.in b/usaco/bronze/complete-search/io/cownomics.in new file mode 100644 index 0000000..1192750 --- /dev/null +++ b/usaco/bronze/complete-search/io/cownomics.in @@ -0,0 +1,7 @@ +3 8 +AATCCCAT +GATTGCAA +GGTCGCAA +ACTCCCAG +ACTCGCAT +ACTTCCAT diff --git a/usaco/bronze/complete-search/io/cownomics.out b/usaco/bronze/complete-search/io/cownomics.out new file mode 100644 index 0000000..f049452 --- /dev/null +++ b/usaco/bronze/complete-search/io/cownomics.out @@ -0,0 +1,4 @@ +1 + +[code]: 0 +[time]: 4.05145 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/diamond.in b/usaco/bronze/complete-search/io/diamond.in new file mode 100644 index 0000000..0480c3b --- /dev/null +++ b/usaco/bronze/complete-search/io/diamond.in @@ -0,0 +1,6 @@ +5 3 +1 +6 +4 +3 +1 diff --git a/usaco/bronze/complete-search/io/diamond.out b/usaco/bronze/complete-search/io/diamond.out new file mode 100644 index 0000000..107bec3 --- /dev/null +++ b/usaco/bronze/complete-search/io/diamond.out @@ -0,0 +1,4 @@ +4 + +[code]: 0 +[time]: 4.07553 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/gymnastics.in b/usaco/bronze/complete-search/io/gymnastics.in new file mode 100644 index 0000000..badd777 --- /dev/null +++ b/usaco/bronze/complete-search/io/gymnastics.in @@ -0,0 +1,4 @@ +3 4 +4 1 2 3 +4 1 3 2 +4 2 1 3 diff --git a/usaco/bronze/complete-search/io/gymnastics.out b/usaco/bronze/complete-search/io/gymnastics.out new file mode 100644 index 0000000..ed6fec9 --- /dev/null +++ b/usaco/bronze/complete-search/io/gymnastics.out @@ -0,0 +1,4 @@ +4 + +[code]: 0 +[time]: 4.5898 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/lineup.in b/usaco/bronze/complete-search/io/lineup.in new file mode 100644 index 0000000..2a48b46 --- /dev/null +++ b/usaco/bronze/complete-search/io/lineup.in @@ -0,0 +1,4 @@ +3 +Buttercup must be milked beside Bella +Blue must be milked beside Bella +Sue must be milked beside Beatrice diff --git a/usaco/bronze/complete-search/io/lineup.out b/usaco/bronze/complete-search/io/lineup.out new file mode 100644 index 0000000..603c442 --- /dev/null +++ b/usaco/bronze/complete-search/io/lineup.out @@ -0,0 +1,11 @@ +Beatrice +Sue +Belinda +Bessie +Betsy +Blue +Bella +Buttercup + +[code]: 0 +[time]: 5.91278 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/maximum-distance.in b/usaco/bronze/complete-search/io/maximum-distance.in new file mode 100644 index 0000000..4fcf889 --- /dev/null +++ b/usaco/bronze/complete-search/io/maximum-distance.in @@ -0,0 +1,3 @@ +3 +321 -15 -525 +404 373 990 diff --git a/usaco/bronze/complete-search/io/maximum-distance.out b/usaco/bronze/complete-search/io/maximum-distance.out new file mode 100644 index 0000000..b6d5a71 --- /dev/null +++ b/usaco/bronze/complete-search/io/maximum-distance.out @@ -0,0 +1,4 @@ +1059112 + +[code]: 0 +[time]: 4.35638 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/pails.in b/usaco/bronze/complete-search/io/pails.in new file mode 100644 index 0000000..ccc3f52 --- /dev/null +++ b/usaco/bronze/complete-search/io/pails.in @@ -0,0 +1 @@ +17 25 77 diff --git a/usaco/bronze/complete-search/io/pails.out b/usaco/bronze/complete-search/io/pails.out new file mode 100644 index 0000000..3d566c4 --- /dev/null +++ b/usaco/bronze/complete-search/io/pails.out @@ -0,0 +1,4 @@ +76 + +[code]: 0 +[time]: 3.78871 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/sleeping-in-class.in b/usaco/bronze/complete-search/io/sleeping-in-class.in new file mode 100644 index 0000000..b4b91d1 --- /dev/null +++ b/usaco/bronze/complete-search/io/sleeping-in-class.in @@ -0,0 +1,7 @@ +3 +6 +1 2 3 1 1 1 +3 +2 2 3 +5 +0 0 0 0 0 diff --git a/usaco/bronze/complete-search/io/sleeping-in-class.out b/usaco/bronze/complete-search/io/sleeping-in-class.out new file mode 100644 index 0000000..accda51 --- /dev/null +++ b/usaco/bronze/complete-search/io/sleeping-in-class.out @@ -0,0 +1,6 @@ +3 +2 +0 + +[code]: 0 +[time]: 3.57866 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/three-logos.in b/usaco/bronze/complete-search/io/three-logos.in new file mode 100644 index 0000000..23dfe07 --- /dev/null +++ b/usaco/bronze/complete-search/io/three-logos.in @@ -0,0 +1,3 @@ +1 1 +1 3 +4 3 diff --git a/usaco/bronze/complete-search/io/three-logos.out b/usaco/bronze/complete-search/io/three-logos.out new file mode 100644 index 0000000..ff2c0ab --- /dev/null +++ b/usaco/bronze/complete-search/io/three-logos.out @@ -0,0 +1,8 @@ +4 +ABBB +CCCC +CCCC +CCCC + +[code]: 0 +[time]: 11.8504 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/io/tracing.in b/usaco/bronze/complete-search/io/tracing.in new file mode 100644 index 0000000..86ca85a --- /dev/null +++ b/usaco/bronze/complete-search/io/tracing.in @@ -0,0 +1,5 @@ +4 3 +1100 +7 1 2 +5 2 3 +6 2 4 diff --git a/usaco/bronze/complete-search/io/tracing.out b/usaco/bronze/complete-search/io/tracing.out new file mode 100644 index 0000000..51f1e1d --- /dev/null +++ b/usaco/bronze/complete-search/io/tracing.out @@ -0,0 +1,4 @@ +1 1 Infinity + +[code]: 0 +[time]: 4.76265 ms \ No newline at end of file diff --git a/usaco/bronze/complete-search/lineup.cc b/usaco/bronze/complete-search/lineup.cc new file mode 100644 index 0000000..2b3c93a --- /dev/null +++ b/usaco/bronze/complete-search/lineup.cc @@ -0,0 +1,116 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +vector names; + +void solve() { + u32 n; + cin >> n; + + vector> constraints(n); + string word, first, last; + for (u32 i = 0; i < n; ++i) { + cin >> constraints[i].ff; + for (u32 j = 0; j < 4; ++j) + cin >> word; + cin >> constraints[i].ss; + } + + vector ans(8, "Z"); + + do { + bool good = true; + + for (auto &[a, b] : constraints) { + bool ok = false; + for (i32 j = 0; j < 8; ++j) { + if (names[j] == a && (j + 1 < 8 && names[j + 1] == b || + j - 1 >= 0 && names[j - 1] == b)) { + ok = true; + break; + } + } + if (!ok) { + good = false; + break; + } + } + + if (good && names < ans) { + ans = names; + } + + } while (next_permutation(names.begin(), names.end())); + + for (auto &e : ans) + cout << e << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + names = {"Bessie", "Buttercup", "Belinda", "Beatrice", + "Bella", "Blue", "Betsy", "Sue"}; + sort(all(names)); + +#define PROBLEM_NAME "lineup" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/makefile b/usaco/bronze/complete-search/makefile similarity index 66% rename from usaco/bronze/makefile rename to usaco/bronze/complete-search/makefile index ef28b72..78778b6 100644 --- a/usaco/bronze/makefile +++ b/usaco/bronze/complete-search/makefile @@ -1,5 +1,7 @@ .PHONY: run debug clean setup init +VERSION ?= 20 + SRC = $(word 2,$(MAKECMDGOALS)) .SILENT: @@ -17,8 +19,8 @@ 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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd init: make setup diff --git a/usaco/bronze/complete-search/maximum-distance.cc b/usaco/bronze/complete-search/maximum-distance.cc new file mode 100644 index 0000000..06911e5 --- /dev/null +++ b/usaco/bronze/complete-search/maximum-distance.cc @@ -0,0 +1,92 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + + vector> pts(n); + for (i32 i = 0; i < n; ++i) + cin >> pts[i].ff; + for (i32 i = 0; i < n; ++i) + cin >> pts[i].ss; + + i64 ans = MIN; + + for (u32 i = 0; i < n; ++i) { + for (u32 j = 0; j < i; ++j) { + ans = max(ans, (pts[i].ff - pts[j].ff) * (pts[i].ff - pts[j].ff) + + (pts[i].ss - pts[j].ss) * (pts[i].ss - pts[j].ss)); + } + } + + 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/usaco/bronze/complete-search/pails.cc b/usaco/bronze/complete-search/pails.cc new file mode 100644 index 0000000..40930d5 --- /dev/null +++ b/usaco/bronze/complete-search/pails.cc @@ -0,0 +1,82 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u64 x, y, m; + cin >> x >> y >> m; + + u64 ans = 0; + for (u32 x_uses = 0; x_uses <= m / x; ++x_uses) { + for (u32 y_uses = 0; y_uses <= (m - x_uses * x) / y; ++y_uses) { + u64 used = x_uses * x + y_uses * y; + ans = max(ans, used); + } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "pails" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/scripts/debug.sh b/usaco/bronze/complete-search/scripts/debug.sh similarity index 100% rename from usaco/bronze/scripts/debug.sh rename to usaco/bronze/complete-search/scripts/debug.sh diff --git a/usaco/bronze/complete-search/scripts/memdebug.sh b/usaco/bronze/complete-search/scripts/memdebug.sh new file mode 100644 index 0000000..0862869 --- /dev/null +++ b/usaco/bronze/complete-search/scripts/memdebug.sh @@ -0,0 +1,31 @@ +#!/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" @mem_debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +# Use ulimit to restrict memory to 256MB (262144 KB) +# This will run the binary with memory restrictions +execute_binary_with_memory_limit "$DBG_BIN" "$INPUT" "$OUTPUT" 262144 +exit $? \ No newline at end of file diff --git a/usaco/bronze/scripts/run.sh b/usaco/bronze/complete-search/scripts/run.sh similarity index 100% rename from usaco/bronze/scripts/run.sh rename to usaco/bronze/complete-search/scripts/run.sh diff --git a/usaco/bronze/scripts/utils.sh b/usaco/bronze/complete-search/scripts/utils.sh similarity index 100% rename from usaco/bronze/scripts/utils.sh rename to usaco/bronze/complete-search/scripts/utils.sh diff --git a/usaco/bronze/complete-search/sleeping-in-class.cc b/usaco/bronze/complete-search/sleeping-in-class.cc new file mode 100644 index 0000000..40478bc --- /dev/null +++ b/usaco/bronze/complete-search/sleeping-in-class.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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (auto &e : a) + cin >> e; + + u64 big = *max_element(all(a)); + u64 ub = accumulate(all(a), 0LL); + + for (u64 target = big; target <= ub; ++target) { + u32 last = 0, moves = 0; + u64 total = 0; + bool bad = false; + for (u32 i = 0; i < n && !bad; ++i) { + total += a[i]; + if (total == target) { + moves += i - last; + last = i + 1; + total = 0; + } else if (i == n - 1 && total != target || total > target) { + bad = true; + } + } + if (!bad) { + cout << moves + n - last << endl; + return; + } + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "sleeping-in-class" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#endif + + u32 t; + cin >> t; + while (t--) + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/three-logos.cc b/usaco/bronze/complete-search/three-logos.cc new file mode 100644 index 0000000..7ee3ae0 --- /dev/null +++ b/usaco/bronze/complete-search/three-logos.cc @@ -0,0 +1,187 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void print(u32 n, u32 x1, u32 y1, u32 x2, u32 y2, u32 x3, u32 y3) { + vector> board(n, vector(n, '.')); + + for (u32 i = 0; i < y1; ++i) { + for (u32 j = 0; j < x1; ++j) { + board[i][j] = 'A'; + } + } + + u32 sr = 0, sc = x1; + if (x1 == n) { + sr = y1; + sc = 0; + } + + for (u32 i = 0; i < y2; ++i) { + for (u32 j = 0; j < x2; ++j) { + board[sr + i][sc + j] = 'B'; + } + } + + bool done = false; + for (u32 i = 0; !done && i < n; ++i) { + for (u32 j = 0; j < n; ++j) { + if (board[i][j] == '.') { + sr = i; + sc = j; + done = true; + break; + } + } + } + + for (u32 i2 = 0; i2 < y3; ++i2) { + for (u32 j2 = 0; j2 < x3; ++j2) { + board[sr + i2][sc + j2] = 'C'; + } + } + + cout << n << endl; + for (const auto &row : board) { + for (char c : row) { + cout << c; + } + cout << '\n'; + } +} + +void solve() { + u32 x1, y1, x2, y2, x3, y3; + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; + + u32 area = x1 * y1 + x2 * y2 + x3 * y3; + + if (ceil(sqrt(area)) != floor(sqrt(area))) { + cout << "-1\n"; + return; + } + + u32 n = sqrt(area); + + if (x1 != n) { + swap(x1, y1); + } + + if (x1 == n) { + if (y2 + y3 == n) { + swap(x2, y2); + swap(x3, y3); + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (x2 + y3 == n) { + swap(x3, y3); + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (y2 + x3 == n) { + swap(x2, y2); + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (x2 + x3 == n) { + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (x2 == n && y2 + y3 == n - y1 || x3 == n && y2 + y3 == n - y1) { + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (y2 == n && x2 + y3 == n - y1) { + print(n, x1, y1, y2, x2, x3, y3); + return; + } else if (y2 == n && x2 + x3 == n - y1) { + swap(x2, y2); + swap(x3, y3); + print(n, x1, y1, x2, y2, x3, y3); + return; + } else if (y3 == n && y2 + x3 == n - y1) { + print(n, x1, y1, y2, x2, y3, x3); + return; + } + } + if (x1 + x2 != n) { + swap(x2, y2); + } + if (x1 + x2 == n) { + if (y1 + y3 != n) { + swap(x3, y3); + } + if (y1 + y3 == n) { + print(n, x1, y1, x2, y2, x3, y3); + return; + } + } + if (x1 + x3 != n) + swap(x3, y3); + + if (x1 + x3 == n) { + if (y1 + y2 != n) { + swap(x2, y2); + } + if (y1 + y2 == n) { + print(n, x1, y1, x2, y2, x3, y3); + return; + } + } + cout << "-1\n"; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + // cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/tracing.cc b/usaco/bronze/complete-search/tracing.cc new file mode 100644 index 0000000..b399f6e --- /dev/null +++ b/usaco/bronze/complete-search/tracing.cc @@ -0,0 +1,168 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +vector> trace(251); + +void solve() { + /* + - # cows patient 0 + - k bounds + + 1. sort by time to "sim" interactions + + - say x and y shake; unless wek one is sick, wedk consequences + (i.e. who gets sick) + - only ONE starts out + + - guess ONE cow is patient zero, run sim + - guess all k from 0 to 251?, taking min of low bound, max of high bound + + k = 250 -> infinity + bsearch <- no (if can achieve state w/ k=i, doesn't mean k=i-1, nor k=i+1) + +NOTE: only candidates for patient zero are initial set 0s, since cows stay sick + */ + + u32 n, t; + cin >> n >> t; + string state; + cin >> state; + state = '0' + state; + + u32 t_; + for (u32 i = 0; i < t; ++i) { + cin >> t_; + cin >> trace[t_].ff >> trace[t_].ss; + // NOTE: forgot the one-indexin + } + + u32 lb = numeric_limits::max(), ub = 0; + u32 count = 0; + + /* + + + a bunch of cows shake hooves because the gay USACO (usa computing olympiad) + just decided to theme everything cows + + and for some K, a cow can only transmit what you have (STDs) + after at most K hooveshakes + + but i misread it as saying that if they contract what you have (an STD) + they can only transmit the STD up to K days later + not after they have sex k times (which is what you do, let k approach + infinity) + + ok. then, after that, i missed k=0 basecase + then, after that + basically, i only counted when someone transmitted an std & infecting someone + instead, i should've counted the cow having sex with ANYONE, even if they already + have an std, it still counts as a handshake + + core note: once realize wrong, reexamine entire problem solution (has rpercussions) rather than what u see is wrong + */ + + for (u32 i = 1; i <= n; ++i) { + bool zero = false; + for (u32 k = 0; k <= 250; ++k) { + vector shakes(n + 1, 0); + string cur_state(n + 1, '0'); + cur_state[i] = '1'; + + for (t_ = 1; t_ <= 250; ++t_) { + auto [a, b] = trace[t_]; + if (a == 0) { + continue; + } + + if (cur_state[a] == '1') { + ++shakes[a]; + } + if (cur_state[b] == '1') { + ++shakes[b]; + } + if (cur_state[a] == '0' && cur_state[b] == '1' && shakes[b] <= k) { + cur_state[a] = '1'; + } else if (cur_state[b] == '0' && cur_state[a] == '1' && + shakes[a] <= k) { + cur_state[b] = '1'; + } + } + + if (cur_state == state) { + zero = true; + lb = min(lb, k); + ub = max(ub, k); + } + } + count += zero; + } + + cout << count << ' ' << lb << ' ' << (ub == 250 ? "Infinity" : to_string(ub)) + << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "tracing" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/.clang-format b/usaco/bronze/simulation/.clang-format similarity index 100% rename from usaco/bronze/.clang-format rename to usaco/bronze/simulation/.clang-format diff --git a/usaco/bronze/simulation/.clangd b/usaco/bronze/simulation/.clangd new file mode 100644 index 0000000..29cb00c --- /dev/null +++ b/usaco/bronze/simulation/.clangd @@ -0,0 +1,35 @@ +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 + -Wno-unknown-pragmas + -std=c++17 + -std=c++17 diff --git a/usaco/bronze/cbarn.cc b/usaco/bronze/simulation/cbarn.cc similarity index 100% rename from usaco/bronze/cbarn.cc rename to usaco/bronze/simulation/cbarn.cc diff --git a/usaco/bronze/simulation/compile_flags.txt b/usaco/bronze/simulation/compile_flags.txt new file mode 100644 index 0000000..785091b --- /dev/null +++ b/usaco/bronze/simulation/compile_flags.txt @@ -0,0 +1,30 @@ +-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 +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++17 diff --git a/usaco/bronze/debug_flags.txt b/usaco/bronze/simulation/debug_flags.txt similarity index 100% rename from usaco/bronze/debug_flags.txt rename to usaco/bronze/simulation/debug_flags.txt diff --git a/usaco/bronze/io/cbarn.in b/usaco/bronze/simulation/io/cbarn.in similarity index 100% rename from usaco/bronze/io/cbarn.in rename to usaco/bronze/simulation/io/cbarn.in diff --git a/usaco/bronze/io/cbarn.out b/usaco/bronze/simulation/io/cbarn.out similarity index 100% rename from usaco/bronze/io/cbarn.out rename to usaco/bronze/simulation/io/cbarn.out diff --git a/usaco/bronze/io/measurement.in b/usaco/bronze/simulation/io/measurement.in similarity index 100% rename from usaco/bronze/io/measurement.in rename to usaco/bronze/simulation/io/measurement.in diff --git a/usaco/bronze/io/measurement.out b/usaco/bronze/simulation/io/measurement.out similarity index 100% rename from usaco/bronze/io/measurement.out rename to usaco/bronze/simulation/io/measurement.out diff --git a/usaco/bronze/io/mixmilk.in b/usaco/bronze/simulation/io/mixmilk.in similarity index 100% rename from usaco/bronze/io/mixmilk.in rename to usaco/bronze/simulation/io/mixmilk.in diff --git a/usaco/bronze/io/mixmilk.out b/usaco/bronze/simulation/io/mixmilk.out similarity index 100% rename from usaco/bronze/io/mixmilk.out rename to usaco/bronze/simulation/io/mixmilk.out diff --git a/usaco/bronze/io/mowing.in b/usaco/bronze/simulation/io/mowing.in similarity index 100% rename from usaco/bronze/io/mowing.in rename to usaco/bronze/simulation/io/mowing.in diff --git a/usaco/bronze/io/mowing.out b/usaco/bronze/simulation/io/mowing.out similarity index 100% rename from usaco/bronze/io/mowing.out rename to usaco/bronze/simulation/io/mowing.out diff --git a/usaco/bronze/io/rut.in b/usaco/bronze/simulation/io/rut.in similarity index 100% rename from usaco/bronze/io/rut.in rename to usaco/bronze/simulation/io/rut.in diff --git a/usaco/bronze/io/rut.out b/usaco/bronze/simulation/io/rut.out similarity index 100% rename from usaco/bronze/io/rut.out rename to usaco/bronze/simulation/io/rut.out diff --git a/usaco/bronze/io/shell-game.in b/usaco/bronze/simulation/io/shell-game.in similarity index 100% rename from usaco/bronze/io/shell-game.in rename to usaco/bronze/simulation/io/shell-game.in diff --git a/usaco/bronze/io/shell-game.out b/usaco/bronze/simulation/io/shell-game.out similarity index 100% rename from usaco/bronze/io/shell-game.out rename to usaco/bronze/simulation/io/shell-game.out diff --git a/usaco/bronze/io/speeding.in b/usaco/bronze/simulation/io/speeding.in similarity index 100% rename from usaco/bronze/io/speeding.in rename to usaco/bronze/simulation/io/speeding.in diff --git a/usaco/bronze/io/speeding.out b/usaco/bronze/simulation/io/speeding.out similarity index 100% rename from usaco/bronze/io/speeding.out rename to usaco/bronze/simulation/io/speeding.out diff --git a/usaco/bronze/simulation/io/traffic.in b/usaco/bronze/simulation/io/traffic.in new file mode 100644 index 0000000..f3a3c1f --- /dev/null +++ b/usaco/bronze/simulation/io/traffic.in @@ -0,0 +1,5 @@ +4 +on 1 1 +none 10 14 +none 11 15 +off 2 3 diff --git a/usaco/bronze/simulation/io/traffic.out b/usaco/bronze/simulation/io/traffic.out new file mode 100644 index 0000000..7e861c1 --- /dev/null +++ b/usaco/bronze/simulation/io/traffic.out @@ -0,0 +1,5 @@ +10 13 +8 12 + +[code]: 0 +[time]: 3.91054 ms \ No newline at end of file diff --git a/usaco/bronze/io/ttt.in b/usaco/bronze/simulation/io/ttt.in similarity index 100% rename from usaco/bronze/io/ttt.in rename to usaco/bronze/simulation/io/ttt.in diff --git a/usaco/bronze/io/ttt.out b/usaco/bronze/simulation/io/ttt.out similarity index 100% rename from usaco/bronze/io/ttt.out rename to usaco/bronze/simulation/io/ttt.out diff --git a/usaco/bronze/io/tttt.in b/usaco/bronze/simulation/io/tttt.in similarity index 100% rename from usaco/bronze/io/tttt.in rename to usaco/bronze/simulation/io/tttt.in diff --git a/usaco/bronze/io/tttt.out b/usaco/bronze/simulation/io/tttt.out similarity index 100% rename from usaco/bronze/io/tttt.out rename to usaco/bronze/simulation/io/tttt.out diff --git a/usaco/bronze/simulation/makefile b/usaco/bronze/simulation/makefile new file mode 100644 index 0000000..78778b6 --- /dev/null +++ b/usaco/bronze/simulation/makefile @@ -0,0 +1,29 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/usaco/bronze/measurement.cc b/usaco/bronze/simulation/measurement.cc similarity index 100% rename from usaco/bronze/measurement.cc rename to usaco/bronze/simulation/measurement.cc diff --git a/usaco/bronze/mixmilk.cc b/usaco/bronze/simulation/mixmilk.cc similarity index 100% rename from usaco/bronze/mixmilk.cc rename to usaco/bronze/simulation/mixmilk.cc diff --git a/usaco/bronze/mowing.cc b/usaco/bronze/simulation/mowing.cc similarity index 100% rename from usaco/bronze/mowing.cc rename to usaco/bronze/simulation/mowing.cc diff --git a/usaco/bronze/rut.cc b/usaco/bronze/simulation/rut.cc similarity index 100% rename from usaco/bronze/rut.cc rename to usaco/bronze/simulation/rut.cc diff --git a/usaco/bronze/simulation/scripts/debug.sh b/usaco/bronze/simulation/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/usaco/bronze/simulation/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/usaco/bronze/simulation/scripts/run.sh b/usaco/bronze/simulation/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/usaco/bronze/simulation/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/usaco/bronze/simulation/scripts/utils.sh b/usaco/bronze/simulation/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/usaco/bronze/simulation/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 +} diff --git a/usaco/bronze/shell.cc b/usaco/bronze/simulation/shell.cc similarity index 100% rename from usaco/bronze/shell.cc rename to usaco/bronze/simulation/shell.cc diff --git a/usaco/bronze/speeding.cc b/usaco/bronze/simulation/speeding.cc similarity index 100% rename from usaco/bronze/speeding.cc rename to usaco/bronze/simulation/speeding.cc diff --git a/usaco/bronze/simulation/traffic.cc b/usaco/bronze/simulation/traffic.cc new file mode 100644 index 0000000..1839d3a --- /dev/null +++ b/usaco/bronze/simulation/traffic.cc @@ -0,0 +1,122 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + + string type; + i64 l, r; + vector> segments; + for (u32 i = 0; i < n; ++i) { + cin >> type >> l >> r; + segments.emplace_back(type, l, r); + } + + i64 lb = 0, ub = 1e9; + for (auto it = segments.rbegin(); it != segments.rend(); ++it) { + tie(type, l, r) = *it; + if (type == "none") { + lb = max(lb, l); + ub = min(ub, r); + } else if (type == "off") { + lb += l; + ub += r; + } else if (type == "on") { + lb = max(lb - r, (i64)0); + ub = max(ub - l, (i64)0); + } + } + cout << lb << ' ' << ub << '\n'; + + lb = 0, ub = 1e9; + for (auto& segment : segments) { + // NOTE: how does this work? + tie(type, l, r) = segment; + if (type == "none") { + lb = max(lb, l); + ub = min(ub, r); + } else if (type == "off") { + lb = max(lb - r, (i64)0); + ub = max(ub - l, (i64)0); + } else if (type == "on") { + lb += l; + ub += r; + } + } + + cout << lb << ' ' << ub << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "traffic" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/tttt.cc b/usaco/bronze/simulation/tttt.cc similarity index 100% rename from usaco/bronze/tttt.cc rename to usaco/bronze/simulation/tttt.cc diff --git a/usaco/bronze/sorting/.clangd b/usaco/bronze/sorting/.clangd new file mode 100644 index 0000000..c75dc4c --- /dev/null +++ b/usaco/bronze/sorting/.clangd @@ -0,0 +1,37 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++23 + -std=c++17 + -std=c++17 diff --git a/usaco/bronze/sorting/compile_flags.txt b/usaco/bronze/sorting/compile_flags.txt new file mode 100644 index 0000000..785091b --- /dev/null +++ b/usaco/bronze/sorting/compile_flags.txt @@ -0,0 +1,30 @@ +-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 +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++17 diff --git a/usaco/bronze/sorting/cow-college.cc b/usaco/bronze/sorting/cow-college.cc new file mode 100644 index 0000000..78c3071 --- /dev/null +++ b/usaco/bronze/sorting/cow-college.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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (auto &e : a) + cin >> e; + + sort(all(a)); + u64 ans = 0, charge = 1e9; + + // for (u32 i = 0; i < n; ++i) { + // auto it = lower_bound(all(a), a[i]); + // + // u32 cnt = distance(it, a.end()); + // if (cnt * a[i] > ans) { + // ans = cnt * a[i]; + // charge = a[i]; + // } else if (cnt * a[i] == ans) { + // charge = min(charge, a[i]); + // } + // } + + for (u32 i = 0; i < n; ++i) { + if (ans < (n - i) * a[i]) { + ans = (n - i) * a[i]; + charge = a[i]; + } else if (ans == (n - i) * a[i]) { + charge = min(charge, a[i]); + } + } + + cout << ans << ' ' << charge; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "cow-college" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/sorting/debug_flags.txt b/usaco/bronze/sorting/debug_flags.txt new file mode 100644 index 0000000..af1b521 --- /dev/null +++ b/usaco/bronze/sorting/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++20 diff --git a/usaco/bronze/sorting/distinct-numbers.cc b/usaco/bronze/sorting/distinct-numbers.cc new file mode 100644 index 0000000..8fee41e --- /dev/null +++ b/usaco/bronze/sorting/distinct-numbers.cc @@ -0,0 +1,80 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (auto &e : a) + cin >> e; + sort(all(a)); + + u32 ans = 0; + for (u32 i = 0; i < n;) { + ++ans; + while (i + 1 < n && a[i + 1] == a[i]) + ++i; + ++i; + } + cout << ans << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + // cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/usaco/bronze/sorting/io/cow-college.in b/usaco/bronze/sorting/io/cow-college.in new file mode 100644 index 0000000..5886163 --- /dev/null +++ b/usaco/bronze/sorting/io/cow-college.in @@ -0,0 +1,2 @@ +4 +1 6 4 6 diff --git a/usaco/bronze/sorting/io/cow-college.out b/usaco/bronze/sorting/io/cow-college.out new file mode 100644 index 0000000..129149a --- /dev/null +++ b/usaco/bronze/sorting/io/cow-college.out @@ -0,0 +1,3 @@ +12 4 +[code]: 0 +[time]: 4.01235 ms \ No newline at end of file diff --git a/usaco/bronze/sorting/io/distinct-numbers.in b/usaco/bronze/sorting/io/distinct-numbers.in new file mode 100644 index 0000000..aba90be --- /dev/null +++ b/usaco/bronze/sorting/io/distinct-numbers.in @@ -0,0 +1,2 @@ +10 +5 9 5 5 10 9 3 1 8 8 diff --git a/usaco/bronze/sorting/io/distinct-numbers.out b/usaco/bronze/sorting/io/distinct-numbers.out new file mode 100644 index 0000000..348bd3b --- /dev/null +++ b/usaco/bronze/sorting/io/distinct-numbers.out @@ -0,0 +1,4 @@ +6 + +[code]: 0 +[time]: 4.40502 ms \ No newline at end of file diff --git a/usaco/bronze/sorting/io/kayaking.in b/usaco/bronze/sorting/io/kayaking.in new file mode 100644 index 0000000..638d970 --- /dev/null +++ b/usaco/bronze/sorting/io/kayaking.in @@ -0,0 +1,2 @@ +4 +1 2 3 4 5 6 100 1000 diff --git a/usaco/bronze/sorting/io/kayaking.out b/usaco/bronze/sorting/io/kayaking.out new file mode 100644 index 0000000..554bf8a --- /dev/null +++ b/usaco/bronze/sorting/io/kayaking.out @@ -0,0 +1,3 @@ +3 +[code]: 0 +[time]: 3.99446 ms \ No newline at end of file diff --git a/usaco/bronze/sorting/io/notlast.in b/usaco/bronze/sorting/io/notlast.in new file mode 100644 index 0000000..8083f63 --- /dev/null +++ b/usaco/bronze/sorting/io/notlast.in @@ -0,0 +1,8 @@ +7 +Bessie 1 +Elsie 1 +Daisy 2 +Gertie 2 +Annabelle 3 +Maggie 4 +Henrietta 4 diff --git a/usaco/bronze/sorting/io/notlast.out b/usaco/bronze/sorting/io/notlast.out new file mode 100644 index 0000000..59013f6 --- /dev/null +++ b/usaco/bronze/sorting/io/notlast.out @@ -0,0 +1,3 @@ +Tie +[code]: 0 +[time]: 3.75843 ms \ No newline at end of file diff --git a/usaco/bronze/sorting/kayaking.cc b/usaco/bronze/sorting/kayaking.cc new file mode 100644 index 0000000..7227f4c --- /dev/null +++ b/usaco/bronze/sorting/kayaking.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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +long long findMinScore(std::vector &a) { + int n = a.size() / 2; + long long S = 0; + for (int i = 0; i < 2 * n; i += 2) { + S += a[i + 1] - a[i]; + } + long long min_score = S; + for (int i = 0; i < 2 * n - 1; i++) { + long long diff = a[i + 1] - a[i]; + long long score = (i % 2 == 0) ? S - diff : S + diff; + min_score = std::min(min_score, score); + } + long long adjust = a[1] - a[0]; + for (int k = 1; k < n; k++) { + adjust += a[2 * k + 1] - 2LL * a[2 * k] + a[2 * k - 1]; + } + min_score = std::min(min_score, S - adjust); + return min_score; +} + +void solve() { + u32 n; + cin >> n; + vector a(n * 2); + for (auto &e : a) + cin >> e; + sort(all(a)); + + cout << findMinScore(a); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + // cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/usaco/bronze/sorting/makefile b/usaco/bronze/sorting/makefile new file mode 100644 index 0000000..78778b6 --- /dev/null +++ b/usaco/bronze/sorting/makefile @@ -0,0 +1,29 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/usaco/bronze/sorting/notlast.cc b/usaco/bronze/sorting/notlast.cc new file mode 100644 index 0000000..967fafd --- /dev/null +++ b/usaco/bronze/sorting/notlast.cc @@ -0,0 +1,104 @@ +#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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +vector cows; + +void solve() { + u32 n; + cin >> n; + + unordered_map m; + u32 x; + string word; + for (u32 i = 0; i < n; ++i) { + cin >> word >> x; + m[word] += x; + } + + u32 small = 1e9, ssmall = 1e9; + if (m.size() < cows.size()) { + small = 0; + } else + for (auto &[_, v] : m) + small = min(small, v); + + string ans = "Tie"; + for (auto it = m.begin(); it != m.end(); ++it) { + if (it->second > small && it->second < ssmall) { + ssmall = it->second; + ans = it->first; + } else if (it->second == ssmall) { + ans = "Tie"; + } + } + + cout << ans; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + cows = {"Bessie", "Elsie", "Daisy", "Gertie", + "Annabelle", "Maggie", "Henrietta"}; + +#define PROBLEM_NAME "notlast" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/sorting/scripts/debug.sh b/usaco/bronze/sorting/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/usaco/bronze/sorting/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/usaco/bronze/sorting/scripts/run.sh b/usaco/bronze/sorting/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/usaco/bronze/sorting/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/usaco/bronze/sorting/scripts/utils.sh b/usaco/bronze/sorting/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/usaco/bronze/sorting/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 +}