Hints: Right Maximum
After each operation, you remove the rightmost maximum and everything to its right. So the remaining array is always a prefix of the original array.
If after some operations the remaining array is
Answer to Hint 1: The next chosen element is the rightmost maximum in
How can you compute that for all
Answer to Hint 2: Scan left to right and maintain a prefix maximum: for each std::max(pre[i], make_pair(a[i], i)) so that when values are equal, the larger index wins).
So pre[p] = (max value in prefix of length p, index of that maximum). The index is exactly the rightmost position of the max in
pre[p].second. So we remove from that index onward — meaning the new “prefix” has length pre[p].second. Set pre[0] = (-1, -1) or similar, then for pre[i+1] = max(pre[i], (a[i], i)) (so index is 0-based). Then ans = 0, p = n; while p > 0: ans++, p = pre[p].second. Output ans.