icpc probs

This commit is contained in:
Barrett Ruth 2026-01-07 12:30:51 -06:00
parent 4c531b965f
commit 69d48861b1
17 changed files with 448 additions and 2 deletions

View file

View file

@ -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)

View file

@ -0,0 +1,2 @@
10 15
10 11 12 13 14 15

View file

@ -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)

View file

@ -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("-")

View file

@ -0,0 +1,3 @@
# contest 1
https://open.kattis.com/contests/asurii

View file

@ -0,0 +1,23 @@
2
.#.#.#.#.#
#.#.#.#.#.
.#.#.B.#.#
#.#.#.#.#.
.#.#.B.#.#
#.#.W.#.#.
.#.#.#.#.#
#.#.#.B.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.B.#.#
#.B.#.B.#.
.#.#.B.#.#
#.B.W.#.#.
.#.B.B.#.#
#.#.#.#.#.
.#.B.B.#.#
#.#.#.#.#.

View file

@ -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()

View file

@ -0,0 +1,3 @@
# contest 2
https://open.kattis.com/contests/wrrf23

View file

@ -0,0 +1 @@
(2+(2*2)+2)

View file

@ -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()

View file

@ -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))

View file

@ -0,0 +1,6 @@
Will Smith
Agent Smith
Peter Pan
Micky Mouse
Minnie Mouse
Peter Gunn

View file

@ -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()