Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The array is sorted, but the real thing you need is just the frequency of each value.
A balanced subsequence has some common occurrence count : every chosen value appears exactly times.
If a value appears times in the original array, it can be used in a balanced subsequence with common count exactly when .
For a fixed , there is no reason to skip any value with frequency at least , because adding it contributes another elements and keeps the subsequence balanced.
Try every possible from to , compute , and take the maximum.
A balanced subsequence means all values that appear in it appear the same number of times.
So the subsequence has a hidden parameter: the common count. Call it .
If we choose values , then the subsequence length is:
because each chosen value appears exactly times.
Counting Frequencies
Since , we can count how many times each value appears. Let be the number of occurrences of value in the original array.
For a value to be usable with common count , we need:
because we can delete extra copies, but we cannot invent missing ones. Shocking, I know.
Fixed
Suppose we decide every chosen value must appear exactly times.
Then every value with can be included. Including it adds exactly elements and does not break balance. So for this fixed , the best choice is to include all such values.
If there are values with frequency at least , the best balanced subsequence length for this is:
Trying All Possibilities
The common count cannot be more than , so we just try all from to .
For each :
This covers every possible balanced subsequence because every balanced subsequence has some common occurrence count .
Why This Is Correct
Take any optimal balanced subsequence. Suppose each chosen value appears exactly times in it. Then every chosen value must have appeared at least times in the original array.
Our algorithm, when checking this same , includes every value with frequency at least , including all values from that optimal subsequence and possibly more. Therefore, our candidate length for this is at least as large as that optimal subsequence.
Since we try every , we cannot miss the optimum.
Complexity
There are at most possible values and possible values of , so the solution is per test case. With , this is tiny. The CPU barely gets out of bed.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
int ans = 0;
for (int k = 1; k <= n; k++) {
int cnt = 0;
for (int v = 1; v <= n; v++) {
if (freq[v] >= k) cnt++;
}
ans = max(ans, k * cnt);
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
int ans = 0;
for (int k = 1; k <= n; k++) {
int cnt = 0;
for (int v = 1; v <= n; v++) {
if (freq[v] >= k) cnt++;
}
ans = max(ans, k * cnt);
}
cout << ans << '\n';
}
return 0;
}