Loading…
Bài 1 (5,0 điểm) Thời gian
Code tham khảo:
def end_exam_times(N, exam_times):
total_time = 100 # Thời gian tối đa
start_time = 8 * 60 # 8 giờ sáng đổi thành phút
end_times = []
for time in exam_times:
# Tính thời điểm kết thúc bài thi của mỗi thí sinh
end_time = min(start_time + time, start_time + total_time)
# Đổi thời gian từ phút thành giờ:phút
end_hour = end_time // 60
end_minute = end_time % 60
# Thêm vào danh sách thời điểm kết thúc
end_times.append(f"{end_hour:02d}:{end_minute:02d}")
# Cập nhật thời gian bắt đầu cho thí sinh kế tiếp
start_time = max(start_time + 10, end_time)
return end_times
with open('TG.INP', 'r') as f:
N = int(f.readline().strip())
exam_times = [int(f.readline().strip()) for _ in range(N)]
# Tìm thời điểm kết thúc của từng thí sinh
result = end_exam_times(N, exam_times)
with open('TG.OUT', 'w') as f:
for time in result:
f.write(time + '\n')
Bài 2 (5 điểm). Mật mã
Code tham khảo:
def count_unique_integers(secret_message):
integers_set = set()
current_integer = ''
for char in secret_message:
if char.isdigit():
current_integer += char
elif current_integer:
integers_set.add(int(current_integer))
current_integer = ''
if current_integer:
integers_set.add(int(current_integer))
return len(integers_set)
with open('MM.INP', 'r') as f:
secret_message = f.readline().strip()
# Tìm số lượng số nguyên phân biệt trong mật thư
result = count_unique_integers(secret_message)
with open('MM.OUT', 'w') as f:
f.write(str(result))
Bài 3 (4 điểm). Trạm phát sóng
Code tham khảo:
def min_broadcast_range(N, locations, K):
locations.sort() # Sắp xếp các vị trí của trạm thu sóng
# Hàm kiểm tra xem có thể đặt trạm phát sóng với phạm vi given_range
def can_place_broadcast_stations(given_range):
count_stations = 1 # Số lượng trạm phát sóng đã đặt
last_station = locations[0] # Vị trí của trạm phát sóng cuối cùng
for i in range(1, N):
if locations[i] - last_station > given_range * 2:
count_stations += 1
last_station = locations[i]
return count_stations <= K
# Tìm kiếm nhị phân để xác định phạm vi phát sóng nhỏ nhất
low, high = 0, 10**6
result = -1
while low <= high:
mid = (low + high) // 2
if can_place_broadcast_stations(mid):
result = mid
high = mid - 1
else:
low = mid + 1
return result
with open('TPS.INP', 'r') as f:
N = int(f.readline().strip())
locations = [int(f.readline().strip()) for _ in range(N)]
K = int(f.readline().strip())
# Tìm phạm vi phát sóng nhỏ nhất của K trạm phát
result_range = min_broadcast_range(N, locations, K)
with open('TPS.OUT', 'w') as f:
f.write(str(result_range))
Bài 4 (3 điểm). Triển lãm
Code tham khảo:
def maximize_profit(N, paintings):
paintings.sort(key=lambda x: x[0]) # Sắp xếp theo kích thước tăng dần
A_min = paintings[0][0]
A_max = paintings[0][0]
S = paintings[0][1]
max_profit = 0
for i in range(1, N):
A_min = min(A_min, paintings[i][0])
A_max = max(A_max, paintings[i][0])
S += paintings[i][1]
max_profit = max(max_profit, S - (A_max - A_min))
return max_profit
with open('TL.INP', 'r') as f:
N = int(f.readline().strip())
paintings = [list(map(int, f.readline().split())) for _ in range(N)]
# Tìm H lớn nhất
result = maximize_profit(N, paintings)
with open('TL.OUT', 'w') as f:
f.write(str(result))
Bài 5 (3 điểm). Dãy đẹp
code tham khảo:
def count_beautiful_subarrays(N, A):
result = 0
count = 1
for i in range(1, N):
if A[i] > A[i - 1]:
count += 1
else:
result += count * (count + 1) // 2
count = 1
result += count * (count + 1) // 2
return result
with open('DD.INP', 'r') as f:
N = int(f.readline().strip())
A = list(map(int, f.readline().split()))
# Đếm số lượng dãy con đẹp của A
result = count_beautiful_subarrays(N, A)
with open('DD.OUT', 'w') as f:
f.write(str(result))