Editorial for Chênh lệch nhỏ nhất
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:
Tiến hành sắp xếp mảng tăng dần. Với mỗi truy vấn, tìm chỉ số nhỏ nhất thỏa mãn giá trị phần tử lớn hơn hoặc bằng
thông qua Tìm kiếm nhị phân (có thể sử dụng hàm lower_bound), gọi chỉ số đó là
. Khi đó, chỉ số lớn nhất thỏa mãn giá trị phần tử nhỏ hơn
là
.
Đáp án bài toán là độ chênh lệch giữa với một trong hai phần tử tại chỉ số
hoặc
.
.
Độ phức tạp: .
Comments