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 table cells are mostly a distraction. The final sum is just the number of pairs that land inside the table.
A successful pair needs one chosen number to be a valid row index () and the other chosen number to be a valid column index ().
Any number greater than is completely useless. It cannot be a row or a column in any successful pair.
Every successful pair consumes two useful numbers, so there is a global bound of . Also bound by the number of row-capable and column-capable values.
The answer is \min(\#\{a_i \le h\},\ #\{a_i \le l\},\ \left\lfloor \#\{a_i \le \max(h,l)\}/2 \right\rfloor). Count these three things and print it.
The table itself is bait. We do not care which exact cells get incremented, and duplicates are totally fine: if we make the pair twice, the sum increases by . So the whole problem is just: how many valid pairs can we form?
A pair is valid if one number can be used as the row and the other can be used as the column. So we care about three counts:
Now get the obvious upper bounds out of the way:
Therefore,
The only real question is whether this bound is always achievable. It is.
Assume first that . Then every row-capable number is also column-capable, because implies . The useful numbers are exactly the column-capable numbers, so .
Let
Choose any row-capable numbers to be the row side of the pairs. Since , after removing those numbers, there are still at least other column-capable numbers left. Pair them arbitrarily with the chosen row numbers. Every pair is valid.
If , the same argument works with rows and columns swapped. So the upper bound is tight, and the formula is the answer.
The common wrong assumption is thinking about distinct cells. Nope. The same cell can be hit multiple times, and each hit still adds . Another easy trap is counting row-capable and column-capable values separately without remembering that one array element cannot play both roles in the same pair. That is exactly why the bound exists.
For each test case, just count values , values , and values .
Complexity: per test case, with extra memory.
#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, h, l;
cin >> n >> h >> l;
int row = 0, col = 0, useful = 0;
int mx = max(h, l);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
row += (x <= h);
col += (x <= l);
useful += (x <= mx);
}
cout << min({row, col, useful / 2}) << '\n';
}
}#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, h, l;
cin >> n >> h >> l;
int row = 0, col = 0, useful = 0;
int mx = max(h, l);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
row += (x <= h);
col += (x <= l);
useful += (x <= mx);
}
cout << min({row, col, useful / 2}) << '\n';
}
}