#C925. Distinct Tree Heights
Distinct Tree Heights
Distinct Tree Heights
You are given a forest with n trees, where the height of each tree is provided in an array. You are allowed to cast at most k spells. Each spell can be used to adjust the height of a single tree to any value such that all trees have distinct heights. However, you can only cast a spell on a tree if there is at least one other tree with the same height.
Your task is to determine whether it is possible to make all trees have distinct heights with at most k spells. The approach used in the solution is to compute the maximum frequency of a particular height and check if the value \(\text{max}\_\text{freq} - 1\) is at most \(k\). (Note: In a fully rigorous solution one might count the total number of duplicates, but here we follow the intended approach.)
Input constraints:
- \(1 \leq n \leq 10^5\)
- \(0 \leq k \leq 10^5\)
- Tree heights are positive integers.
inputFormat
The input is given from standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers \(n\) and \(k\) where \(n\) is the number of trees and \(k\) is the maximum number of spells available.
- The second line contains \(n\) space-separated integers representing the heights of the trees.
outputFormat
Output a single line to standard output (stdout) containing either YES
if it is possible to make all tree heights distinct with at most \(k\) spells, or NO
otherwise.
5 3
1 2 2 3 3
YES