icpc probs
This commit is contained in:
parent
4c531b965f
commit
69d48861b1
17 changed files with 448 additions and 2 deletions
|
|
@ -82,5 +82,4 @@ int main() { // {{{
|
|||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
} // vim: set foldmethod=marker foldmarker={{{{{{,}}}}}}
|
||||
|
|
|
|||
17
codeforces/1071/.clang-format
Normal file
17
codeforces/1071/.clang-format
Normal file
|
|
@ -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
|
||||
62
codeforces/1071/a.cc
Normal file
62
codeforces/1071/a.cc
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
#include <version>
|
||||
#ifdef __cpp_lib_ranges_enumerate
|
||||
#include <ranges>
|
||||
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 <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::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={{{,}}}
|
||||
0
kattis/10-9-2024/1/input.txt
Normal file
0
kattis/10-9-2024/1/input.txt
Normal file
15
kattis/10-9-2024/1/script.py
Normal file
15
kattis/10-9-2024/1/script.py
Normal 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)
|
||||
2
kattis/10-9-2024/3/input.txt
Normal file
2
kattis/10-9-2024/3/input.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
10 15
|
||||
10 11 12 13 14 15
|
||||
44
kattis/10-9-2024/3/script.py
Normal file
44
kattis/10-9-2024/3/script.py
Normal 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)
|
||||
54
kattis/10-9-2024/4/script.py
Normal file
54
kattis/10-9-2024/4/script.py
Normal 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("-")
|
||||
3
kattis/10-9-2024/contest.md
Normal file
3
kattis/10-9-2024/contest.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# contest 1
|
||||
|
||||
https://open.kattis.com/contests/asurii
|
||||
23
kattis/23-9-24/b/input.txt
Normal file
23
kattis/23-9-24/b/input.txt
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
2
|
||||
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.#.W.#.#.
|
||||
.#.#.#.#.#
|
||||
#.#.#.B.#.
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.B.#.B.#.
|
||||
.#.#.B.#.#
|
||||
#.B.W.#.#.
|
||||
.#.B.B.#.#
|
||||
#.#.#.#.#.
|
||||
.#.B.B.#.#
|
||||
#.#.#.#.#.
|
||||
117
kattis/23-9-24/b/solution.py
Normal file
117
kattis/23-9-24/b/solution.py
Normal 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()
|
||||
3
kattis/23-9-24/contest.md
Normal file
3
kattis/23-9-24/contest.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# contest 2
|
||||
|
||||
https://open.kattis.com/contests/wrrf23
|
||||
1
kattis/23-9-24/d/input.txt
Normal file
1
kattis/23-9-24/d/input.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
(2+(2*2)+2)
|
||||
46
kattis/23-9-24/d/solution.py
Normal file
46
kattis/23-9-24/d/solution.py
Normal 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()
|
||||
22
kattis/23-9-24/e/solution.py
Normal file
22
kattis/23-9-24/e/solution.py
Normal 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))
|
||||
6
kattis/23-9-24/f/input.txt
Normal file
6
kattis/23-9-24/f/input.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Will Smith
|
||||
Agent Smith
|
||||
Peter Pan
|
||||
Micky Mouse
|
||||
Minnie Mouse
|
||||
Peter Gunn
|
||||
32
kattis/23-9-24/f/solution.py
Normal file
32
kattis/23-9-24/f/solution.py
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue