From bf585a88f9a1034e1007693a5f88b1a37f79aec0 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 26 Apr 2025 14:16:04 -0400 Subject: [PATCH] feat: usaco --- codeforces/1003/c.cc | 8 +-- codeforces/1020/f.cc | 93 +++++++++++++++++++++++++++ codeforces/1020/io/f.in | 13 ++++ codeforces/1020/io/f.out | 24 +++++++ usaco/blocks.cc | 48 -------------- usaco/blocks.in | 4 -- usaco/blocks.out | 26 -------- usaco/bronze/.clang-format | 9 +++ usaco/bronze/compile_flags.txt | 30 +++++++++ usaco/bronze/debug_flags.txt | 12 ++++ usaco/bronze/io/mixmilk.in | 3 + usaco/bronze/io/mixmilk.out | 6 ++ usaco/bronze/io/rut.in | 7 +++ usaco/bronze/io/rut.out | 9 +++ usaco/bronze/io/shell-game.in | 4 ++ usaco/bronze/io/shell-game.out | 4 ++ usaco/bronze/io/speeding.in | 7 +++ usaco/bronze/io/speeding.out | 4 ++ usaco/bronze/makefile | 27 ++++++++ usaco/bronze/mixmilk.cc | 90 ++++++++++++++++++++++++++ usaco/bronze/rut.cc | 112 +++++++++++++++++++++++++++++++++ usaco/bronze/scripts/debug.sh | 29 +++++++++ usaco/bronze/scripts/run.sh | 29 +++++++++ usaco/bronze/scripts/utils.sh | 53 ++++++++++++++++ usaco/bronze/shell.cc | 97 ++++++++++++++++++++++++++++ usaco/bronze/speeding.cc | 106 +++++++++++++++++++++++++++++++ 26 files changed, 770 insertions(+), 84 deletions(-) create mode 100644 codeforces/1020/f.cc create mode 100644 codeforces/1020/io/f.in create mode 100644 codeforces/1020/io/f.out delete mode 100644 usaco/blocks.cc delete mode 100644 usaco/blocks.in delete mode 100644 usaco/blocks.out create mode 100644 usaco/bronze/.clang-format create mode 100644 usaco/bronze/compile_flags.txt create mode 100644 usaco/bronze/debug_flags.txt create mode 100644 usaco/bronze/io/mixmilk.in create mode 100644 usaco/bronze/io/mixmilk.out create mode 100644 usaco/bronze/io/rut.in create mode 100644 usaco/bronze/io/rut.out create mode 100644 usaco/bronze/io/shell-game.in create mode 100644 usaco/bronze/io/shell-game.out create mode 100644 usaco/bronze/io/speeding.in create mode 100644 usaco/bronze/io/speeding.out create mode 100644 usaco/bronze/makefile create mode 100644 usaco/bronze/mixmilk.cc create mode 100644 usaco/bronze/rut.cc create mode 100644 usaco/bronze/scripts/debug.sh create mode 100644 usaco/bronze/scripts/run.sh create mode 100644 usaco/bronze/scripts/utils.sh create mode 100644 usaco/bronze/shell.cc create mode 100644 usaco/bronze/speeding.cc diff --git a/codeforces/1003/c.cc b/codeforces/1003/c.cc index 04eaf46..529cbd0 100644 --- a/codeforces/1003/c.cc +++ b/codeforces/1003/c.cc @@ -76,13 +76,9 @@ void solve() { a[0] = min(a[0], x - a[0]); for (int i = 1; i < n; ++i) { - if (a[i] >= a[i - 1] && x - a[i] >= a[i - 1]) { + if (x - a[i] >= a[i - 1]) { a[i] = min(a[i], x - a[i]); - } else if (a[i] >= a[i - 1]) { - ; - } else if (x - a[i] >= a[i - 1]) { - a[i] = x - a[i]; - } else { + } else if (a[i] < a[i - 1]) { prln("NO"); return; } diff --git a/codeforces/1020/f.cc b/codeforces/1020/f.cc new file mode 100644 index 0000000..7a0a5bb --- /dev/null +++ b/codeforces/1020/f.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() { + u32 n; + cin >> n; + + string s; + cin >> s; + + // NOTE: thought could loop thru the entire thing + // knew memory was O(n^2), but didn't actually say: + // ok what do i want to do - loop thru. this is impractical + string prev(n, '1'); + // union_find uf; + for (u32 i = 0; i < n; ++i) { + if (i) + s[i] = ('0' + 1 - (s[i] - '0')); + s[i] = ('0' + 1 - (s[i] - '0')); + cout << "i: " << i << endl; + cout << s << endl; + } +} + +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/codeforces/1020/io/f.in b/codeforces/1020/io/f.in new file mode 100644 index 0000000..690d78b --- /dev/null +++ b/codeforces/1020/io/f.in @@ -0,0 +1,13 @@ +6 +3 +000 +4 +0010 +7 +1011001 +4 +0001 +2 +11 +1 +0 diff --git a/codeforces/1020/io/f.out b/codeforces/1020/io/f.out new file mode 100644 index 0000000..6c4bdd1 --- /dev/null +++ b/codeforces/1020/io/f.out @@ -0,0 +1,24 @@ +100 +010 +001 +1010 +0110 +0000 +0011 +0011001 +1111001 +1001001 +1010001 +1011101 +1011011 +1011000 +1001 +0101 +0011 +0000 +01 +10 +1 + +[code]: 0 +[time]: 4.11367 ms \ No newline at end of file diff --git a/usaco/blocks.cc b/usaco/blocks.cc deleted file mode 100644 index efb74ec..0000000 --- a/usaco/blocks.cc +++ /dev/null @@ -1,48 +0,0 @@ -#include -using namespace std; - -#define all(x) (x).begin(), (x).end() -#define sz(x) static_cast((x).size()) -#define FOR(x) for (size_t i = 0; i < (x).size(); ++i) - -#ifndef LOCAL -#define dbg(x) cerr << #x << " = " << (x) << '\n' -#else -#define dbg(x) -#endif - -#define PROBLEM_NAME "blocks" - -void solve() { - int N; - cin >> N; - vector ans(26, 0); - - for(int i = 0; i < N; ++i) { - string left, right; - cin >> left >> right; - vector count_left(26, 0); - for(char c : left) count_left[c - 'a']++; - vector count_right(26, 0); - for(char c : right) count_right[c - 'a']++; - for(int j = 0; j < 26; ++j) { - ans[j] += max(count_left[j], count_right[j]); - } - } - - for(int count : ans) { - cout << count << '\n'; - } -} - -int main() { - ios::sync_with_stdio(false); - cin.tie(nullptr); - - freopen(PROBLEM_NAME ".in", "r", stdin); - freopen(PROBLEM_NAME ".out", "w", stdout); - - solve(); - - return 0; -} diff --git a/usaco/blocks.in b/usaco/blocks.in deleted file mode 100644 index d0be452..0000000 --- a/usaco/blocks.in +++ /dev/null @@ -1,4 +0,0 @@ -3 -fox box -dog cat -car bus diff --git a/usaco/blocks.out b/usaco/blocks.out deleted file mode 100644 index 52ed73e..0000000 --- a/usaco/blocks.out +++ /dev/null @@ -1,26 +0,0 @@ -2 -2 -2 -1 -0 -1 -1 -0 -0 -0 -0 -0 -0 -0 -2 -0 -0 -1 -1 -1 -1 -0 -0 -1 -0 -0 diff --git a/usaco/bronze/.clang-format b/usaco/bronze/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/usaco/bronze/.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/usaco/bronze/compile_flags.txt b/usaco/bronze/compile_flags.txt new file mode 100644 index 0000000..c6f2a50 --- /dev/null +++ b/usaco/bronze/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 +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL diff --git a/usaco/bronze/debug_flags.txt b/usaco/bronze/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/usaco/bronze/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/usaco/bronze/io/mixmilk.in b/usaco/bronze/io/mixmilk.in new file mode 100644 index 0000000..b74be8d --- /dev/null +++ b/usaco/bronze/io/mixmilk.in @@ -0,0 +1,3 @@ +10 3 +11 4 +12 5 diff --git a/usaco/bronze/io/mixmilk.out b/usaco/bronze/io/mixmilk.out new file mode 100644 index 0000000..3aac00d --- /dev/null +++ b/usaco/bronze/io/mixmilk.out @@ -0,0 +1,6 @@ +0 +10 +2 + +[code]: 0 +[time]: 4.47321 ms \ No newline at end of file diff --git a/usaco/bronze/io/rut.in b/usaco/bronze/io/rut.in new file mode 100644 index 0000000..cdfeb8a --- /dev/null +++ b/usaco/bronze/io/rut.in @@ -0,0 +1,7 @@ +6 +E 3 5 +N 5 3 +E 4 6 +E 10 4 +N 11 2 +N 8 1 diff --git a/usaco/bronze/io/rut.out b/usaco/bronze/io/rut.out new file mode 100644 index 0000000..8e41936 --- /dev/null +++ b/usaco/bronze/io/rut.out @@ -0,0 +1,9 @@ +5 +3 +Infinity +Infinity +2 +5 + +[code]: 0 +[time]: 11.8356 ms \ No newline at end of file diff --git a/usaco/bronze/io/shell-game.in b/usaco/bronze/io/shell-game.in new file mode 100644 index 0000000..2fcc4ad --- /dev/null +++ b/usaco/bronze/io/shell-game.in @@ -0,0 +1,4 @@ +3 +1 2 1 +3 2 1 +1 3 1 diff --git a/usaco/bronze/io/shell-game.out b/usaco/bronze/io/shell-game.out new file mode 100644 index 0000000..2eee0ec --- /dev/null +++ b/usaco/bronze/io/shell-game.out @@ -0,0 +1,4 @@ +2 + +[code]: 0 +[time]: 5.1527 ms \ No newline at end of file diff --git a/usaco/bronze/io/speeding.in b/usaco/bronze/io/speeding.in new file mode 100644 index 0000000..e15e420 --- /dev/null +++ b/usaco/bronze/io/speeding.in @@ -0,0 +1,7 @@ +3 3 +40 75 +50 35 +10 45 +40 76 +20 30 +40 40 diff --git a/usaco/bronze/io/speeding.out b/usaco/bronze/io/speeding.out new file mode 100644 index 0000000..c89a97b --- /dev/null +++ b/usaco/bronze/io/speeding.out @@ -0,0 +1,4 @@ +5 + +[code]: 0 +[time]: 4.50397 ms \ No newline at end of file diff --git a/usaco/bronze/makefile b/usaco/bronze/makefile new file mode 100644 index 0000000..ef28b72 --- /dev/null +++ b/usaco/bronze/makefile @@ -0,0 +1,27 @@ +.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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/usaco/bronze/mixmilk.cc b/usaco/bronze/mixmilk.cc new file mode 100644 index 0000000..71fc674 --- /dev/null +++ b/usaco/bronze/mixmilk.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 f64 = double; +using f128 = long double; + +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() +#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() { + vector c(3), m(3); + for (u32 i = 0; i < 3; ++i) { + cin >> c[i] >> m[i]; + } + + for (u32 i = 0; i < 100; ++i) { + u64 pour = min(m[i % 3], c[(i + 1) % 3] - m[(i + 1) % 3]); + m[i % 3] -= pour; + m[(i + 1) % 3] += pour; + } + + for (u32 i = 0; i < 3; ++i) { + cout << m[i] << '\n'; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "mixmilk" + +#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/rut.cc b/usaco/bronze/rut.cc new file mode 100644 index 0000000..ab284ee --- /dev/null +++ b/usaco/bronze/rut.cc @@ -0,0 +1,112 @@ +#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; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +#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 +// }}} + +struct Cow { + u32 i; + i64 x, y; +}; + +void solve() { + u32 n; + cin >> n; + + vector east_cows, north_cows; + vector ans(n, -1); + + char dir; + i64 x, y; + for (u32 i = 0; i < n; ++i) { + cin >> dir >> x >> y; + + Cow cow{i, x, y}; + if (dir == 'E') { + east_cows.emplace_back(cow); + } else { + north_cows.emplace_back(cow); + } + } + + sort(all(east_cows), [](auto const& c1, auto const& c2) { + return c1.y < c2.y; + }); + sort(all(north_cows), [](auto const& c1, auto const& c2) { + return c1.x < c2.x; + }); + + for (auto& nc : north_cows) { + for (auto& ec : east_cows) { + if (ec.x > nc.x || nc.y >= ec.y || ans[ec.i] != -1) + continue; + + auto et = nc.x - ec.x; + auto nt = ec.y - nc.y; + + if (nt < et) { + ans[ec.i] = et; + } + if (et < nt) { + ans[nc.i] = nt; + break; + } + } + } + + for (auto& e : ans) { + if (e == -1) { + cout << "Infinity"; + } else { + cout << e; + } + cout << '\n'; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "rut" + +#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/scripts/debug.sh b/usaco/bronze/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/usaco/bronze/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/scripts/run.sh b/usaco/bronze/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/usaco/bronze/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/scripts/utils.sh b/usaco/bronze/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/usaco/bronze/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/shell.cc new file mode 100644 index 0000000..e2e0bb0 --- /dev/null +++ b/usaco/bronze/shell.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; + +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() +#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() { + vector shell_locs(3), shell_counts(3, 0); + iota(all(shell_locs), 0); + + u32 n, a, b, g; + cin >> n; + for (u32 i = 0; i < n; ++i) { + cin >> a >> b >> g; + + --a; + --b; + --g; + for (u32 j = 0; j < shell_locs.size(); ++j) { + if (shell_locs[j] == a) + shell_locs[j] = b; + else if (shell_locs[j] == b) + shell_locs[j] = a; + shell_counts[j] += shell_locs[j] == g; + } + } + + cout << *max_element(all(shell_counts)) << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "shell" + +#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/speeding.cc b/usaco/bronze/speeding.cc new file mode 100644 index 0000000..b89d924 --- /dev/null +++ b/usaco/bronze/speeding.cc @@ -0,0 +1,106 @@ +#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; + +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() +#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() { + i32 n, m; + cin >> n >> m; + + vector> limit(n), cow(m); + for (u32 i = 0; i < n; ++i) { + cin >> limit[i].ff >> limit[i].ss; + if (i) + limit[i].ff += limit[i - 1].ff; + } + for (u32 i = 0; i < m; ++i) { + cin >> cow[i].ff >> cow[i].ss; + if (i) + cow[i].ff += cow[i - 1].ff; + } + + i32 i = -1, j = -1; + + i32 ans = 0; + while (j < m) { + if (i == -1) { + ++i; + ++j; + } else if (i + 1 < n && limit[i + 1].ff <= cow[j].ff) { + ++i; + } else { + ++j; + } + ans = max(ans, cow[j].ss - limit[i].ss); + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "speeding" + +#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; +} +// }}}