Editorial for KTHSUM
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Nguồn: Free Contest
Subtask 1
Vì nên ta có thể sinh toàn bộ
phần tử của dãy
, sắp xếp theo thứ tự không giảm rồi in ra
phần tử đầu.
Độ phức tạp:
Subtask 2
Thay vì sinh toàn bộ phần tử, ta chỉ sinh
phần tử nhỏ nhất của dãy
bằng cách sử dụng
heap hoặc set như sau:
- Sắp xếp dãy B theo thứ tự không giảm.
- Với mỗi phần tử
của dãy
, thêm vào
heap/setcặp phần tử.
- Thực hiện thao tác sau
lần: Lấy ra cặp phần tử
có tổng nhỏ nhất từ
heap/setvà in ra tổng đó. Thêm vàoheap/setcặp phần tửnếu
.
Độ phức tạp:
Comments