From 69d48861b1dd310079d141e661069c06c7b263d9 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 7 Jan 2026 12:30:51 -0600 Subject: [PATCH] icpc probs --- codeforces/1037/a.cc | 3 +- codeforces/1071/.clang-format | 17 +++++ codeforces/1071/a.cc | 62 ++++++++++++++++++ kattis/10-9-2024/1/input.txt | 0 kattis/10-9-2024/1/script.py | 15 +++++ kattis/10-9-2024/3/input.txt | 2 + kattis/10-9-2024/3/script.py | 44 +++++++++++++ kattis/10-9-2024/4/script.py | 54 ++++++++++++++++ kattis/10-9-2024/contest.md | 3 + kattis/23-9-24/b/input.txt | 23 +++++++ kattis/23-9-24/b/solution.py | 117 ++++++++++++++++++++++++++++++++++ kattis/23-9-24/contest.md | 3 + kattis/23-9-24/d/input.txt | 1 + kattis/23-9-24/d/solution.py | 46 +++++++++++++ kattis/23-9-24/e/solution.py | 22 +++++++ kattis/23-9-24/f/input.txt | 6 ++ kattis/23-9-24/f/solution.py | 32 ++++++++++ 17 files changed, 448 insertions(+), 2 deletions(-) create mode 100644 codeforces/1071/.clang-format create mode 100644 codeforces/1071/a.cc create mode 100644 kattis/10-9-2024/1/input.txt create mode 100644 kattis/10-9-2024/1/script.py create mode 100644 kattis/10-9-2024/3/input.txt create mode 100644 kattis/10-9-2024/3/script.py create mode 100644 kattis/10-9-2024/4/script.py create mode 100644 kattis/10-9-2024/contest.md create mode 100644 kattis/23-9-24/b/input.txt create mode 100644 kattis/23-9-24/b/solution.py create mode 100644 kattis/23-9-24/contest.md create mode 100644 kattis/23-9-24/d/input.txt create mode 100644 kattis/23-9-24/d/solution.py create mode 100644 kattis/23-9-24/e/solution.py create mode 100644 kattis/23-9-24/f/input.txt create mode 100644 kattis/23-9-24/f/solution.py diff --git a/codeforces/1037/a.cc b/codeforces/1037/a.cc index 47ffe9d..bac1114 100644 --- a/codeforces/1037/a.cc +++ b/codeforces/1037/a.cc @@ -82,5 +82,4 @@ int main() { // {{{ } return 0; -} -// }}} +} // vim: set foldmethod=marker foldmarker={{{{{{,}}}}}} diff --git a/codeforces/1071/.clang-format b/codeforces/1071/.clang-format new file mode 100644 index 0000000..59cd6f1 --- /dev/null +++ b/codeforces/1071/.clang-format @@ -0,0 +1,17 @@ +BasedOnStyle: LLVM +IndentWidth: 2 +UseTab: Never + +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortLambdasOnASingleLine: None +AllowShortBlocksOnASingleLine: Never +AllowShortEnumsOnASingleLine: false +AllowShortCaseExpressionOnASingleLine: false + +BreakBeforeBraces: Attach +ColumnLimit: 100 +AlignAfterOpenBracket: Align +BinPackArguments: false +BinPackParameters: false diff --git a/codeforces/1071/a.cc b/codeforces/1071/a.cc new file mode 100644 index 0000000..93807a7 --- /dev/null +++ b/codeforces/1071/a.cc @@ -0,0 +1,62 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +#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(); +#endif + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + u32 tc = 1; + std::cin >> tc; + for (u32 t = 0; t < tc; ++t) { + solve(); + } + return 0; +} + +// }}} + +// vim: set foldmethod=marker foldmarker={{{,}}} diff --git a/kattis/10-9-2024/1/input.txt b/kattis/10-9-2024/1/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/kattis/10-9-2024/1/script.py b/kattis/10-9-2024/1/script.py new file mode 100644 index 0000000..8d37a7d --- /dev/null +++ b/kattis/10-9-2024/1/script.py @@ -0,0 +1,15 @@ +n, a = list(map(int, input().split())) + +es = list(map(int, input().split())) + +es.sort() + +won = 0 + +for e in es: + if a <= e: + break + won += 1 + a -= (e + 1) + +print(won) diff --git a/kattis/10-9-2024/3/input.txt b/kattis/10-9-2024/3/input.txt new file mode 100644 index 0000000..c68b729 --- /dev/null +++ b/kattis/10-9-2024/3/input.txt @@ -0,0 +1,2 @@ +10 15 +10 11 12 13 14 15 diff --git a/kattis/10-9-2024/3/script.py b/kattis/10-9-2024/3/script.py new file mode 100644 index 0000000..af25fa5 --- /dev/null +++ b/kattis/10-9-2024/3/script.py @@ -0,0 +1,44 @@ +from math import gcd + +a, b = map(int, input().split()) +max_num = float('-inf') + +transcript = input().split() + +fizzs = [] +buzzs = [] + +for i, token in enumerate(transcript): + num = a + i + + max_num = max(max_num, num) + + if token.startswith("Fizz") or token.endswith("Fizz"): + fizzs.append(num) + if token.startswith("Buzz") or token.endswith("Buzz"): + buzzs.append(num) + + +def gcf(nums: list[int]) -> int: + if not nums: + return '' + if len(nums) == 1: + return nums[0] + + x, y = nums[0], nums[1] + ans = gcd(x, y) + + for i in range(2, len(nums)): + ans = gcd(ans, nums[i]) + + return ans + + +a, b = gcf(fizzs), gcf(buzzs) + +if a == '': + a = max_num + 1 +if b == '': + b = max_num + 1 + +print(a, b) diff --git a/kattis/10-9-2024/4/script.py b/kattis/10-9-2024/4/script.py new file mode 100644 index 0000000..5fee760 --- /dev/null +++ b/kattis/10-9-2024/4/script.py @@ -0,0 +1,54 @@ +N = int(input()) + +matrix: list[list[int]] = [] +for i in range(N): + matrix[i] = list(map(int, list(input()))) + +row_map = [0] * N +col_map = [0] * N + +ones_above = 0 +ones_above_row = [0] * N + +for i, row in enumerate(matrix): + ones_above_row[i] = ones_above + total = 0 + for cell in row: + total += cell + row_map[i] = total + ones_above += row_map[i] + +ones_left = 0 +ones_left_col = [0] * N + +for col in range(N): + total = 0 + for row in range(N): + total += matrix[row][col] + ones_left_col[col] = ones_left + col_map[col] = total + ones_left += col_map[col] + +found_valid_row = False +for i in range(N): + flips_remain = row_map[i] + total_ones = ones_above_row[-1] - row_map[i] + row_map[-1] + if total_ones <= flips_remain: + row_flag = True + break + +found_valid_col = False +for i in range(N): + flips_remain = col_map[i] + total_ones = ones_left_col[-1] - col_map[i] + col_map[-1] + if total_ones <= flips_remain: + found_valid_col = True + break + +if found_valid_row: + if found_valid_col: + print("+") + else: + print("-") +else: + print("-") diff --git a/kattis/10-9-2024/contest.md b/kattis/10-9-2024/contest.md new file mode 100644 index 0000000..9a0693a --- /dev/null +++ b/kattis/10-9-2024/contest.md @@ -0,0 +1,3 @@ +# contest 1 + +https://open.kattis.com/contests/asurii diff --git a/kattis/23-9-24/b/input.txt b/kattis/23-9-24/b/input.txt new file mode 100644 index 0000000..4165880 --- /dev/null +++ b/kattis/23-9-24/b/input.txt @@ -0,0 +1,23 @@ +2 + +.#.#.#.#.# +#.#.#.#.#. +.#.#.B.#.# +#.#.#.#.#. +.#.#.B.#.# +#.#.W.#.#. +.#.#.#.#.# +#.#.#.B.#. +.#.#.#.#.# +#.#.#.#.#. + +.#.#.#.#.# +#.#.#.#.#. +.#.#.B.#.# +#.B.#.B.#. +.#.#.B.#.# +#.B.W.#.#. +.#.B.B.#.# +#.#.#.#.#. +.#.B.B.#.# +#.#.#.#.#. diff --git a/kattis/23-9-24/b/solution.py b/kattis/23-9-24/b/solution.py new file mode 100644 index 0000000..e95ce65 --- /dev/null +++ b/kattis/23-9-24/b/solution.py @@ -0,0 +1,117 @@ +""" +Backtracking Framework +- Set of choices +- Limited by constraints +- To reach a goal + +1. Understand +- Board of W/B, can capture; looking for max consecutive sequence of captures using same piece +- Capture in any direction "skipping over"; land on open square + +Similar Problems: N Queens + +Set of candidates (W pieces) limited in moves (capturing B pieces) to reach max # captures (i.e. capturing moves) +2. Develop + +- Given board state, w/ W candidates +- Consider every candidate as the piece yielding correct answer +- For every candidate, consider every capturing move they can make + - For each move: + - Make the move, and recursively find max # captures using this piece after this capture + - update/restore state on failure + - Update max if new max found + +consider after: caching/optimization + +backtracking function: def max_captures(board, w) -> int: + + +3. Carry Out +4. Revise + +At any particular position, +""" + +BOARD_SIZE = 10 + + +def parse_board() -> tuple[list[list[str]], list[tuple[int, int]]]: + board = [] + candidates: list[tuple[int, int]] = [] + + input() + + for r in range(BOARD_SIZE): + board.append(list(input())) + + candidates.extend((r, c) for c, cell in enumerate(board[-1]) if cell == "W") + + return board, candidates + + +def valid(board, r, c): + return 0 <= r < len(board) and 0 <= c < len(board[0]) + + +# all capturing moves white piece can make +def capturing_moves(board, r, c) -> list[tuple[int, int]]: + if not valid(board, r, c): + return [] + + moves = [] + + for dr, dc in [(-1, -1), (1, 1), (-1, 1), (1, -1)]: + if ( + valid(board, r + 2 * dr, c + 2 * dc) + and board[r + dr][c + dc] == "B" + and board[r + 2 * dr][c + 2 * dc] not in "BW" + ): + moves.append((dr, dc)) + + return moves + + +def max_candidate_captures(board, r, c) -> int: + max_captures = 0 + + for dr, dc in capturing_moves(board, r, c): + # place + board[r][c] = "." + board[r + dr][c + dc] = "." + board[r + dr * 2][c + dc * 2] = "W" + + ans = max_candidate_captures(board, r + dr * 2, c + dc * 2) + max_captures = max(max_captures, 1 + ans) + + # unplace + board[r + dr * 2][c + dc * 2] = "." + board[r][c] = "W" + board[r + dr][c + dc] = "B" + + return max_captures + + +def max_captures(board, candidates: list[tuple[int, int]]) -> int: + max_captures = 0 + + for r, c in candidates: + max_captures = max(max_captures, max_candidate_captures(board, r, c)) + + return max_captures + + +def solve() -> None: + T = int(input()) + + while T: + board, candidates = parse_board() + print(max_captures(board, candidates)) + T -= 1 + + +def main() -> None: + solve() + + +if __name__ == "__main__": + main() diff --git a/kattis/23-9-24/contest.md b/kattis/23-9-24/contest.md new file mode 100644 index 0000000..065a704 --- /dev/null +++ b/kattis/23-9-24/contest.md @@ -0,0 +1,3 @@ +# contest 2 + +https://open.kattis.com/contests/wrrf23 diff --git a/kattis/23-9-24/d/input.txt b/kattis/23-9-24/d/input.txt new file mode 100644 index 0000000..b188259 --- /dev/null +++ b/kattis/23-9-24/d/input.txt @@ -0,0 +1 @@ +(2+(2*2)+2) diff --git a/kattis/23-9-24/d/solution.py b/kattis/23-9-24/d/solution.py new file mode 100644 index 0000000..68054c9 --- /dev/null +++ b/kattis/23-9-24/d/solution.py @@ -0,0 +1,46 @@ +def solve(equation: str) -> None: + stack = [] + paren_pairs = [] + + for i, token in enumerate(equation): + if token == "(": + stack.append([i, None]) + elif token == ")": + l, r = stack.pop() + r = i + paren_pairs.append((l, r)) + + P = [[]] + + for paren_pair in paren_pairs: + P.extend([[paren_pair] + p for p in P]) + + def format(permutation): + output = list(equation) + + for l, r in permutation: + output[l] = None + output[r] = None + + return "".join(filter(lambda token: token, output)) + + seen = set() + ans = [] + for permutation in P[1:]: + output = format(permutation) + if output not in seen: + seen.add(output) + ans.append(output) + + for x in sorted(ans): + print(x) + + +def main() -> None: + equation = input() + + solve(equation) + + +if __name__ == "__main__": + main() diff --git a/kattis/23-9-24/e/solution.py b/kattis/23-9-24/e/solution.py new file mode 100644 index 0000000..32811c7 --- /dev/null +++ b/kattis/23-9-24/e/solution.py @@ -0,0 +1,22 @@ +ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_." + +character_to_index = {c: i for i, c in enumerate(ALPHABET)} +index_to_character = {i: c for i, c in enumerate(ALPHABET)} + +while line := input(): + split = line.split() + + if len(split) == 1: + break + + k, string = int(split[0]), split[1] + + backwards = string[::-1] + + ans: list[str] = [] + + for letter in backwards: + index = character_to_index[letter] + ans.append(index_to_character[(index + k) % len(ALPHABET)]) + + print("".join(ans)) diff --git a/kattis/23-9-24/f/input.txt b/kattis/23-9-24/f/input.txt new file mode 100644 index 0000000..35b50fd --- /dev/null +++ b/kattis/23-9-24/f/input.txt @@ -0,0 +1,6 @@ +Will Smith +Agent Smith +Peter Pan +Micky Mouse +Minnie Mouse +Peter Gunn diff --git a/kattis/23-9-24/f/solution.py b/kattis/23-9-24/f/solution.py new file mode 100644 index 0000000..dbfae37 --- /dev/null +++ b/kattis/23-9-24/f/solution.py @@ -0,0 +1,32 @@ +import sys +from collections import Counter + + +def solve() -> None: + counter: Counter[str] = Counter() + + ans = [] + + lines = sys.stdin.readlines() + for line in lines: + first, last = line.split() + + counter[first] += 1 + + ans.append((last, first)) + + ans.sort() + + for last, first in ans: + if counter[first] > 1: + print(f"{first} {last}") + else: + print(first) + + +def main() -> None: + solve() + + +if __name__ == "__main__": + main()