A. Word Rearrangement
This is a straightforward problem requiring only basic input/output handling.
w1, w2 = input().split()
print(w2)
print(w1)
B. Cooking Tangyuan
The key idea is to simulate the process of using packages to fulfill cooking rounds. Each package contributes a fixed number of tangyuan, and excess can carry over.
n, x, k = map(int, input().split())
result = []
current_stock = 0
packages_used = 0
for _ in range(n):
current_stock += x
packages_used += 1
while current_stock >= k:
result.append(packages_used)
current_stock -= k
packages_used = 0
print(len(result))
print(*result)
C. Binary String Optimization
We aim to maximize the difference between the count of '1's and '0's after optionally removing a prefix ending at some postiion. The optimal strategy involves checking all possible split points.
s = input().strip()
n = len(s)
total_ones = s.count('1')
total_zeros = n - total_ones
best = max(0, total_ones - total_zeros)
prefix_ones = 0
remaining_ones = total_ones
remaining_zeros = total_zeros
for i in range(n):
if s[i] == '1':
prefix_ones += 1
remaining_ones -= 1
else:
remaining_zeros -= 1
current_gain = (1 if prefix_ones > 0 else 0) + (remaining_ones - remaining_zeros)
best = max(best, current_gain)
print(best)
D. Array Clearance via Greedy Strategy
Group elements by value and process them in increasing order. When removing a value, propagate deletions to consecutive higher values as much as possible.
from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
freq = Counter(arr)
items = sorted(freq.items())
m = len(items)
ops = 0
i = 0
while i < m:
val, cnt = items[i]
if cnt == 0:
i += 1
continue
ops += cnt
carry = cnt
j = i + 1
while j < m and items[j][0] == val + (j - i) and carry > 0:
if items[j][1] >= carry:
items[j] = (items[j][0], items[j][1] - carry)
break
else:
carry -= items[j][1]
items[j] = (items[j][0], 0)
j += 1
i += 1
print(ops)
E. Dungeon Escape with Dijkstra
Model the grid as a graph where moving into a digit cell costs that digit's value. Use Dijkstra’s algorithm to find the minimal cost path from 'S' to 'T', ensuring total cost stays below the health limit.
import heapq
from math import inf
def can_escape(grid, max_health):
rows, cols = len(grid), len(grid[0])
start = end = None
for r in range(rows):
for c in range(cols):
if grid[r][c] == 'S':
start = (r, c)
elif grid[r][c] == 'T':
end = (r, c)
if not start or not end:
return "No"
dist = [[inf] * cols for _ in range(rows)]
pq = [(0, start[0], start[1])]
dist[start[0]][start[1]] = 0
directions = [(1,0), (-1,0), (0,1), (0,-1)]
while pq:
cost, r, c = heapq.heappop(pq)
if cost != dist[r][c]:
continue
if (r, c) == end:
return "Yes"
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
edge_cost = 0
if grid[nr][nc].isdigit():
edge_cost = int(grid[nr][nc])
new_cost = cost + edge_cost
if new_cost < max_health and new_cost < dist[nr][nc]:
dist[nr][nc] = new_cost
heapq.heappush(pq, (new_cost, nr, nc))
return "No"
t = int(input())
for _ in range(t):
n, m, h = map(int, input().split())
dungeon = [input().strip() for _ in range(n)]
print(can_escape(dungeon, h))