For a value to be the of a subsequence, two things must be true:
Every number smaller than (0, 1, \dots, v-1) must be included in the subsequence.
The value itself must be absent.
Wait! If we only want the minimum possible , we actually only need to satisfy the second condition. If we can form any subsequence of length that excludes , then the of that subsequence will be at most .
Crucial Observation: is a candidate for the minimum if there are enough elements in the array that are not equal to to fill a subsequence of length .
Let be the frequency of value in the original array of size .
The number of elements available that are not is exactly n - f(v).
If n - f(v) \ge k, then we can definitely pick elements such that is missing.
In this case, the for length is .
Notice that if a value is a candidate for length , it is automatically a candidate for all lengths shorter than (e.g., k-1, k-2, \dots, 1).
To find the absolute minimum for each :
For each , calculate the maximum possible length K_{max} = n - f(v).
This is now a potential "ceiling" for the answer at k = K_{max}.
Since also works for all k < K_{max}, we simply need to take the best (minimum) available as we move from larger to smaller .
Complexity :
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n ;
cin >> n ;
vector <int> f(n + 3) ;
for(int i = 0 ; i < n ; i ++) {
int x ;
cin >> x ;
if(x <= n + 1) f[x] ++ ;
}
vector <int> ans(n + 1 , n + 2) ;
for(int v = 0 ; v <= n + 1 ; v ++) {
ans[f[v]] = min(ans[f[v]] , v) ;
}
for(int i = 1 ; i <= n ; i ++) ans[i] = min(ans[i] , ans[i - 1]) ;
for(int v = 1 ; v <= n ; v ++) cout << ans[n - v] << " " ;
cout << "\n" ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
int t ;
cin >> t ;
while(t --) {
solve() ;
}
return 0;
}