Make a note of how to solve the ABC115 C problem It was a simple matter of sorting normally. https://atcoder.jp/contests/abc115/tasks/abc115_c
The problem of choosing K from a large number of trees and making the difference between the maximum and minimum tree heights the smallest. Since the number of trees is up to 6 digits, if you try all the way, it will be TLE with a margin. Also, the feeling that it can be solved with O (N) is amazing. Considering that the difference between the largest tree and the smallest tree is taken from the K books, if you sort and take K books in ascending order, turn it to the nth tree with for, and update ans, you can get the answer. notice
int ans = 1000000000;
for(int i = 0;i + k - 1 < n;i++){
int kari = h[i + k - 1] - h[i]; //The maximum and minimum are clear because they are sorted
ans = Math.min(ans, kari);
}
Recommended Posts