Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.
Let S be the area of the rectangle and P be the perimeter of the rectangle.
The chosen rectangle should have the value (P^2)/(S) minimal possible. The value is taken without any rounding.
If there are multiple answers, print any of them.
Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately.
Input
The first line contains a single integer T (T ≥ 1) — the number of lists of sticks in the testcase.
Then 2T lines follow — lines (2i - 1) and 2i of them describe the i-th list. The first line of the pair contains a single integer n (4 ≤ n ≤ 10^6) — the number of sticks in the i-th list. The second line of the pair contains n integers a_1, a_2, ..., a_n (1 ≤ a_j ≤ 10^4) — lengths of the sticks in the i-th list.
It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle.
The total number of sticks in all T lists doesn't exceed 10^6 in each testcase.
Output
Print T lines. The i-th line should contain the answer to the i-th list of the input. That is the lengths of the four sticks you choose from the i-th list, so that they form a rectangle and the value (P^2)/(S) of this rectangle is minimal possible. You can print these four lengths in arbitrary order.
If there are multiple answers, print any of them.
Example
Input
3
4
7 2 2 7
8
2 8 1 4 8 2 1 5
5
5 5 5 5 5
Output
2 7 7 2
2 2 1 1
5 5 5 5
Note
There is only one way to choose four sticks in the first list, they form a rectangle with sides 2 and 7, its area is 2 ⋅ 7 = 14, perimeter is 2(2 + 7) = 18. (18^2)/(14) ≈ 23.143.
The second list contains subsets of four sticks that can form rectangles with sides (1, 2), (2, 8) and (1, 8). Their values are (6^2)/(2) = 18, (20^2)/(16) = 25 and (18^2)/(8) = 40.5, respectively. The minimal one of them is the rectangle (1, 2).
You can choose any four of the 5 given sticks from the third list, they will form a square with side 5, which is still a rectangle with sides (5, 5).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given T lists of sticks. For each list, we must choose exactly 4 sticks that can form a rectangle: two pairs of equal lengths, each pair forms opposite sides. The rectangle sides are x and y (x and y are positive integers, lengths of sticks). So we need to pick two sticks of length x and two sticks of length y, with x and y not necessarily distinct (square allowed). The sticks are provided; we can only use each stick once, so we need at least two sticks of a given length to use as a side. So effectively, we need to select a pair (x, y) from the multiset where there are at least 2 copies of x and at least 2 copies of y, and if x == y, then at least 4 copies of that length. Among all such feasible rectangles, we want to minimize f = P^2 / S, where P = 2*(x+y), S = x*y.
Compute f(x,y) = (2(x+y))^2 / (xy) = 4 (x+y)^2 / (xy) = 4 (x^2 + 2xy + y^2)/(xy) = 4 (x/y + 2 + y/x). More precisely: = 4( (x/y) + (y/x) + 2). Let t = x/y > 0. Then f = 4(t + 1/t + 2) = 4 ( (sqrt(t) - 1/sqrt(t))^2 + 4 )? Actually t + 1/t + 2 = (sqrt(t) + 1/sqrt(t))^2? Let's see: (sqrt(t) + 1/sqrt(t))^2 = t + 2 + 1/t. Yes! So f = 4 ( sqrt(t) + 1/sqrt(t) )^2 = [2( sqrt(t) + 1/sqrt(t) )]^2? Actually 4*(sqrt(t) + 1/sqrt(t))^2 = (2 (sqrt(t) + 1/sqrt(t)))^2, but f is not squared, it's the value. Anyway.
Thus f = 4*(t + 1/t + 2) = 4 * (sqrt(t) + 1/sqrt(t))^2. For given sum, the product? Actually, f is minimized when t + 1/t is minimized, i.e., when t=1 (square). So for a fixed set of available lengths, the rectangle that is most square-like (x and y as close as possible) gives minimal f. However, we need to consider the constraint that x and y must be available with counts >=2. So the problem reduces to: from the multiset of stick lengths, consider lengths that appear at least twice. For each such length we have potential side. For pairs (x,y) with x <= y? Actually, order doesn't matter. We want to minimize f(x,y) = 4 (x/y + y/x + 2). Since f is symmetric and increasing in |x-y|? Let's examine: f(x,y) = 4(x^2 + y^2 + 2xy)/(xy) = 4( (x^2+y^2)/(xy) + 2) = 4( x/y + y/x + 2). For fixed sum maybe? Actually it's decreasing as x and y get closer? Let's define ratio r = max(x,y)/min(x,y) >= 1. Then x/y + y/x = r + 1/r. That's increasing for r>1. So f is increasing with r. So to minimize f, we want the ratio as close to 1 as possible, i.e., choose two side lengths that are as equal as possible. But also note that if we have a length that appears at least 4 times, we can take x=y, giving ratio 1, which is optimal. So if there's any length with count >=4, then the square of that length gives minimal possible f (since ratio=1). But there could be multiple such lengths? If multiple lengths with >=4, any square gives f=16? Actually f(s,s) = 4(s/s + s/s + 2) = 4(1+1+2)=4*4=16. So any square gives f=16 exactly. But wait compute: P = 4s, P^2=16 s^2, S = s^2, ratio =16. Yes. So all squares have f=16. So if any length has >=4 sticks, we can simply output that length four times (any order). That's optimal.
Otherwise, no length has count >=4. Then we need two distinct lengths, each with count >=2. Among those pairs, we want to minimize f, i.e., minimize r + 1/r where r = y/x (with x <= y). Since r + 1/r is decreasing for r in [1, ...? Actually derivative 1 - 1/r^2 >0 for r>1, so increasing. So the smaller the ratio, the better, i.e., r as close to 1 as possible. So we need to select two available lengths (with counts >=2) that are as close as possible in value.
But careful: Could there be a case where using same length with count exactly 2? That's not possible because we need two sticks of each side. So same length needs at least 4 for a square. So if count = 2 for a length, we cannot use it as both sides; we must pair it with another length (maybe same if count>=4). So indeed we need two distinct lengths each with count >=2. And we want them as close as possible.
However, note the example: second list: sticks: 2 8 1 4 8 2 1 5. The counts: 1:2, 2:2, 4:1, 5:1, 8:2. Available pairs: (1,2), (1,8), (2,8). Ratio for (1,2): r=2, f=4(2+0.5+2)=4(4.5)=18? Actually 4(1/2+2+2? Wait x/y+y/x+2: if x=1,y=2 => 1/2+2+2? y/x=2, x/y=0.5, sum=0.5+2+2? +2? Actually x/y + y/x + 2 = 0.5+2+2 = 4.5, times 4 = 18. Yes. (2,8): ratio 4 => 4+0.25+2=6.25, times4=25. (1,8): ratio 8 => 8+0.125+2=10.125, times4=40.5. So indeed (1,2) is closest ratio. So algorithm: find two distinct lengths with frequency >=2 such that the absolute difference is minimized. If there are multiple pairs with same difference, any works because ratio same? Actually ratio depends on larger/smaller. The difference minimal likely leads to ratio close to 1, but if lengths 100 and 101 vs 1 and 2? 100 and 101 ratio=1.01, difference=1; 1 and 2 ratio=2, difference=1. The ratio is more important: we want min ratio = max/min as close to 1, so we want the two numbers to be as close as possible in value, but also the smaller one matters? Actually ratio = larger/smaller. For given difference d, the ratio is (small+d)/small = 1 + d/small. So for same d, larger small gives smaller ratio. So we might need to consider not just difference but the ratio directly. So we must minimize r = y/x (with x<y). This is equivalent to minimizing (y-x)/x? Actually r = y/x. So among all pairs with counts>=2, we want to minimize y/x. Since x and y are positive integers. So we need to pick two lengths where the larger divided by the smaller is as close to 1. This is equivalent to sorting the available lengths (those with freq>=2) and then among consecutive elements in sorted order, compute the ratio (next/current) and pick the smallest ratio. Because if we have lengths L1 < L2 < L3, then L3/L1 > L2/L1 and also L3/L2 > L2/L1? Actually compare L3/L1 vs L2/L1: obviously L3/L1 > L2/L1. Also L3/L2 might be smaller than L3/L1 but likely larger than L2/L1? Not necessarily: L3/L2 could be less than L2/L1? Example: lengths 2,3,100: ratios: 3/2=1.5, 100/3≈33, 100/2=50. So the smallest ratio is 1.5 from consecutive (2,3). Indeed for sorted list, the minimal ratio occurs among consecutive elements because if you skip one, the ratio is larger: For any i<j, we have L_j / L_i >= L_{i+1} / L_i, since L_j >= L_{i+1} and denominator same, so ratio larger. Also, maybe L_j / L_i could be less than L_{j} / L_{j-1}? Actually if we fix j, the smallest ratio with L_j is when the other is as large as possible but less than L_j, i.e., L_{j-1}. So minimal ratio overall is indeed min over consecutive pairs. Therefore, the optimal rectangle (if no square) is to take two lengths that are consecutive in the sorted list of available lengths with freq>=2.
But note: If there is a length with freq>=4, we take square; that yields ratio=1 which is better than any ratio >1. So that is optimal.
Edge Cases: Could there be a tie? Multiple consecutive pairs with same ratio? Since lengths are integers, ratio may not be exactly same; we just pick any that minimizes the ratio. However, because lengths are integers, ratio is rational; we might need to compare fractions accurately. But we can compare cross-multiplication: For two candidate pairs (a,b) and (c,d) with a<b, c<d, we want to compare b/a vs d/c => compare bc vs ad. Smaller ratio is better. So we can iterate over sorted list and keep track of pair with smallest bc? Actually b/a < d/c iff bc < ad. So we can track minimal product bc? Wait bc < ad? That's not symmetrical. But for consecutive pairs we have a = L[i], b=L[i+1]. So we can compute ratio = L[i+1]/L[i]. The minimal ratio corresponds to minimal L[i+1]/L[i] and can be compared via cross multiplication: For two consecutive pairs (p,q) and (r,s) with p<q, r<s, we compare q/p vs s/r => compare qr vs ps. So we can track the pair that yields minimal q/p.
But also we must ensure we have at least two sticks for each length. So we need to consider lengths with frequency at least 2. So we first build a frequency map of lengths. Then we extract the list of lengths that appear at least 2 times, sorted. If any appears at least 4 times, answer: print that length four times. Otherwise, we have at least two distinct lengths with freq >=2 (guaranteed existence because problem ensures a rectangle is possible). Then we need to find two consecutive lengths (in sorted list) minimizing the ratio. That gives the optimal rectangle. Then answer: print those two lengths twice each (order arbitrary).
Now complexities: n up to 10^6 per test case, total sum 10^6, so O(n log n) per test or O(n) possible. Since we need to sort the distinct lengths with freq>=2, but the number of distinct lengths is at most min(n, 10000) because a_i <= 10^4, so there are at most 10000 distinct possible lengths. Actually a_i <= 10^4, so there are only 10000 possible values. So we can use counting array of size 10001 to get frequencies, then iterate over 1..10000 to collect lengths with freq>=2. That's O(10000) per test, which is fine. Even if a_i could be larger, but constraint says up to 10^4. So we can do counting array. Also note total n sum is 1e6, but T might be up to maybe many? However total sticks is 1e6, and each test we do O(max_length) which is 10000, but if T is large (like 100 tests each with small n) then O(T*10000) might be up to 10^6? Actually worst-case T could be up to 250000? But total sticks is 1e6, but if each test has n=4, then T could be 250000. Doing O(10000) per test would be 2.5e9, too high. However constraints: total number of sticks <= 1e6 in each testcase (the problem statement: "The total number of sticks in all T lists doesn't exceed 10^6 in each testcase." So T could be up to 250000 if each n=4. Doing 10000 per test would be 2.5e9 which is too much. So we need a more efficient method: we cannot iterate up to 10000 for each tiny test. So we need to only iterate over lengths that actually appear in the test. Since a_i <= 10^4, the number of distinct lengths present is bounded by n, but if n is small, we can just process the distinct lengths we have. For each test, we can read the array, count frequencies using a map (like unordered_map) or using a global array but only reset used indices. However resetting a full array of 10001 for each test might be okay if T is not huge? But worst-case T=250k, resetting 10001 ints per test would be 2.5 billion operations, not okay. However resetting an array of size 10001 is about 10001 assignments, which for 250k tests is 2.5e9 assignments, which might be borderline but likely too slow in C++ (2.5e9 operations > time limit). So we need to avoid O(10000) per test when T is large but total n is 1e6. So we can use a hash map (e.g., unordered_map) to store frequencies only for lengths that appear. The total number of distinct lengths across all tests is at most sum of n, but each stick length may be repeated; however distinct per test might be small. The total number of distinct length entries across tests could be up to total n (1e6). So using unordered_map per test, cleared each time, would be okay because total operations across tests is O(total n). But we also need to sort the lengths that have freq>=2. The number of such distinct lengths per test is at most number of distinct lengths in that test, which is <= n_i. But sorting them could be O(d_i log d_i) where sum of d_i across tests <= total distinct lengths? Actually each test's distinct lengths d_i <= n_i, and sum of n_i = 1e6, so sum of d_i might be up to 1e6 as well (worst-case all sticks unique, but then there would be no rectangle possible; but it's guaranteed rectangle possible, so there must be at least two duplicates, but distinct count could still be high, but worst-case: many unique plus a few duplicates, e.g., n_i=1000 with 2 pairs duplicates and 996 unique; d_i=1000. Sum d_i across tests could be up to 1e6. So sorting each test's distinct lengths list would total O( sum d_i log d_i ) which could be up to 1e6 * log(1e6) ~ 20e6, acceptable. However we also need to filter lengths with freq>=2. That's easy.
But we need to be careful: we need to sort only the lengths that appear at least twice. That list size may be smaller than d_i.
So algorithm per test:
Read n, then read array a of size n.
Build frequency count: we can use unordered_map<int,int> freq; or since a_i <= 10000, we could use a global array freq[10001] but we need to reset only those indices we touch. For many small tests, resetting the whole array each time is heavy. We can maintain a vector of used indices: for each test, we store the lengths we encounter, and after processing we clear freq for those indices (set to 0). That would be O(n_i). So we can use a static array freq[10001] and a vector used. For each a_i, if freq[a_i]==0 then used.push_back(a_i); then freq[a_i]++. After reading, we have counts. Then we can extract lengths where freq[l] >= 2. If any freq[l] >= 4, then answer: print l four times. Else, we need to find two distinct lengths from this list (freq[l]>=2) that minimize l2/l1 (with l1<l2). We'll sort the candidate list. Then iterate consecutive pairs to find minimal ratio.
But note: the candidate list must have at least two lengths. Guaranteed. Then we choose the pair with smallest ratio.
Implementation details:
For ratio comparison: use cross-multiplication to avoid floating errors. We'll keep best pair (x,y). Initialize with (0,0). For each consecutive (a,b) in sorted candidates, compare b/a with best_ratio. Since we want minimal b/a, we can compute if (b * best_x) < (a * best_y) ??? Let's derive.
Let current pair: (a,b) with a<b. We want to check if b/a < best_b/best_a? Actually if we have best pair (best_a, best_b) where best_a < best_b. Then condition b/a < best_b/best_a is equivalent to b * best_a < a * best_b. If this holds, then current pair is better. So we can maintain best_a, best_b.
But careful: if best not initialized, set first pair as best.
Alternatively, we could compute double ratio = (double)b/a, but cross multiplication is safe with integers because values up to 10^4, product up to 10^8, fits in 32-bit. Actually b up to 10000, best_a up to 10000, product up to 10^8, fits in 32-bit signed (2^31-1=2.147e9). So fine.
Edge Cases:
If there are multiple lengths with freq>=2 but we might consider same length? Actually square case already handled separately. So we only consider distinct lengths.
What about using a length with freq >= 2 but also there is another length with freq >=2 that is equal? If there is a length with freq >=4, we took square. If freq == 2 or 3 (3 could be used as two for one side? Actually we need two sticks of each side. If we have three sticks of length L, we can use two of them for one side, and the third cannot be used as other side unless we have another length. So for square we need 4, so freq >=4 only for square. So for distinct lengths, we only need freq>=2 for each. Having freq=3 is okay, we just need 2 for each side, the extra stick unused.
So algorithm robust.
But check: Could there be a scenario where using a length with freq >=2 and the other length same? That's square case covered. So no.
Now after we choose the two lengths x and y (with x <= y), we output: x x y y (or any order) as per example: they output "2 7 7 2" for first, but any order allowed. So we can print x, y, x, y or whatever.
Now we need to ensure we output exactly four integers separated by spaces. So do that.
Now testing with given examples:
Test1: n=4, sticks:7 2 2 7. Frequencies: 2:2,7:2. No >=4. Sorted candidates: [2,7]. Only pair: (2,7). Output 2 2 7 7 or 2 7 7 2 etc. Correct.
Test2: sticks: 2,8,1,4,8,2,1,5. Frequencies:1:2,2:2,4:1,5:1,8:2. Candidates:1,2,8. Sorted: [1,2,8]. Consecutive: (1,2): ratio=2; (2,8): ratio=4. Min ratio=2 from (1,2). Output 1 1 2 2. Example output is "2 2 1 1", any order. Good.
Test3: 5, all 5: freq[5]=5>=4 -> square, output four 5's. Example output "5 5 5 5". Good.
Now we must consider if there is any nuance: The problem statement: "The chosen rectangle should have the value (P^2)/(S) minimal possible." Since we derived that it's minimized when ratio of sides closest to 1, and square is optimal, our approach is correct.
But wait: Let's double-check the expression: (P^2)/S = (2(x+y))^2/(xy)=4(x+y)^2/(xy). For fixed x+y, product is maximal when x=y, and then the ratio? Actually for fixed perimeter, area is max when square, and (P^2)/S = P^2 / S, so if P fixed, then S larger gives smaller value. But here x and y can vary; we are comparing across different pairs. Our analysis shows it's purely a function of ratio: 4(x/y + y/x + 2). So it's minimized when x/y =1.
Thus correct.
Now complexity: For each test, we read n numbers O(n). We build freq using a global array of size 10001 and a used vector. That's O(n). Then we need to extract candidate lengths (freq>=2). We can do by iterating over used vector and checking freq[l] >=2, add to a vector cand. That's O(number of distinct lengths). Then sort cand: O(k log k). Then find best consecutive pair O(k). Then output. The sum of n across tests is up to 1e6, sum of distinct lengths across tests is also <= 1e6 (since each distinct length appears at least once). So overall O(total n log max distinct per test) which is acceptable. Even if we sort each test separately, total sorting cost is sum k_i log k_i <= total distinct * log (max per test). Since max per test could be up to 1e6 if all sticks distinct but that can't happen because rectangle must be possible, so there must be at least one duplicate; but distinct could be up to 1e6 -? Actually n per test can be up to 1e6, and they could all be distinct except maybe two duplicates? For rectangle possible we need at least two pairs of equal lengths, so there must be at least 2 duplicates. That still allows many distinct others. So distinct could be up to n - 2. So worst-case per test: n = 1e6, distinct = 999,998? Actually to have two sticks equal, you need at least two identical, the rest can be all distinct. So distinct count could be (n-1) essentially. So k (cand = lengths with freq>=2) would be maybe small (just a few lengths have freq>=2). Actually cand includes only lengths with freq>=2. In worst case, if we have many distinct lengths, only one or two lengths have freq>=2. So cand size is small. So sorting cand is cheap. However building used vector: we iterate over all n to fill freq. Then to extract cand, we need to iterate over used vector (which contains all distinct lengths). That's size up to n. But we can combine: while reading we can also record freq. Then after reading, we can iterate over used and check if freq[l] >=2 to add to cand. That iteration is O(distinct). In worst case distinct ~ n, which is okay because total n across tests is 1e6. So overall fine.
But for performance, we should be careful with I/O: use fast input (scanf or cin with sync off) and output.
Now we should consider possible tie-breaking: if multiple pairs have same ratio, any is fine.
Now we must also consider that if there is a length with freq >= 4, we output that. But what if there are multiple lengths with freq >=4? Then any works; we can pick the first encountered or smallest. So we can just find any such length while building freq or while iterating used.
Implementation steps per test:
Read n.
Allocate/clear freq array? Use static int freq[10001] maybe global; but we need to reset for each test. Since total tests can be many, but we have used list. We'll maintain freq as global array, initialized to 0 before all tests. Then for each test, after reading, we will for each length in used: freq[length] = 0. But if T is large and total n is 1e6, resetting per used length is O(total distinct) which is okay. However we need to clear freq for used lengths only, not the whole array.
But we must also consider that the array size is only 10001, which is small; resetting the whole array for each test (using memset) would be O(10001) per test, which if T=250k, would be 2.5 billion bytes set; that is 2.5 GB of writes, which might be borderline but maybe still okay if efficient? 2.5e9 operations is high. It's safer to use the used list.
Implementation:
Global freq[10001] = {0}
For each test:
vector used; (or we can reuse a global vector and clear each test)
read n, then loop i=0..n-1: read val; if (freq[val] == 0) used.push_back(val); freq[val]++.
After reading, first check for any length with freq[l] >=4. We can iterate over used and check. If found, output that length four times. Then clear freq for used (set to 0) and continue to next test.
If not, then build vector cand; for each l in used: if freq[l] >=2: cand.push_back(l). Then sort(cand.begin(), cand.end()).
Then if cand.size() < 2? But guarantee rectangle possible so >=2.
Iterate over cand indices 0..size-2: let a = cand[i], b = cand[i+1]. Compare b/a with best ratio: if (b * best_a < a * best_b) (with best initialized to first pair), then update best.
After loop, output best_a, best_a, best_b, best_b (or any order).
Then clear freq for used: for each l in used: freq[l] = 0.
But careful: The used list contains all distinct lengths. But after we have processed the test, we set freq to 0. That's fine.
Edge Cases: If we found a length with freq >=4, we should also clear freq for used before next test. So we can do that after output.
Now check: Is there any scenario where the optimal rectangle might use two same lengths that are not a square? No, because square uses same length, but that requires at least 4 sticks. If a length appears exactly 2 or 3, we cannot use it as both sides because we need 2 sticks per side, total 4 sticks: if we use same length for both sides, we need 4 sticks of that length. So no.
Thus algorithm stands.
But wait, could there be a scenario where we have three lengths with freq >=2, but the best ratio is not between consecutive in sorted order? For ratio, as argued, minimal ratio occurs among consecutive. However, is it possible that due to counts we might have to pick non-consecutive because the consecutive pair might not be both available? If we have sorted list L1 < L2 < L3. Both L1 and L2 are available (freq>=2). So they are consecutive in the sorted list of available lengths. So ratio L2/L1 is candidate. L3/L2 is also candidate. Any other pair like L3/L1 has ratio larger than L2/L1 because L3 > L2, denominator same, so ratio larger. So indeed consecutive is minimal.
Thus algorithm correct.
Now let's think about proof: f(x,y) depends only on ratio. For fixed set, we want minimal ratio. Ratio = max(x,y)/min(x,y) >= 1. So we need min possible. Since both x and y must be in the set of lengths with count>=2, the minimal ratio is indeed achieved by the pair of available lengths that are closest to each other. That is, after sorting, the minimal ratio occurs among consecutive elements.
Thus fine.
Now we should ensure we handle large T efficiently. Use fast input: for C++, use scanf or cin with ios::sync_with_stdio(false);cin.tie(0). Read ints.
Now memory: freq array of size 10001 ints (maybe 40KB). used vector per test can be up to distinct lengths. But we can allocate a local vector each test; it will be freed. That's fine.
Now what about the possibility of a_i being 0? a_j >= 1 so okay.
Now I'll produce final answer.
But there is nuance: The function f = P^2 / S, with P = 2(x+y), S = xy. For a square, f = 16. For rectangles, f > 16. So indeed any square is best. So if any square possible, output it.
Now we need to ensure that if there are multiple squares (different lengths with >=4), any is fine.
Now check a tricky case: Suppose we have lengths: 1,1,2,2,3,3. Here we have three lengths each with count 2. Squares none. Candidates: (1,2): ratio 2; (2,3): ratio 1.5; (1,3): ratio 3. So minimal is (2,3). Our algorithm: sorted cand [1,2,3]; consecutive: (1,2) ratio 2; (2,3) ratio 1.5; picks (2,3). Good.
Another: 10,10,20,20,100,100. Consecutive: (10,20) ratio 2; (20,100) ratio 5. So pick (10,20) even though (20,100) have larger ratio. So correct.
Now, confirm the minimal ratio principle with cross-multiplication for many pairs.
Implementation details for cross multiplication:
Initialize best_a = -1, best_b = -1.
Loop over consecutive pairs:
if (best_a == -1) best_a = a, best_b = b;
else if (b * best_a < a * best_b) { best_a = a; best_b = b; }
But careful: when best_a and best_b are set from first pair, we need to ensure a and b are positive integers. So fine.
Potential integer overflow: b * best_a can be up to 10^4 * 10^4 = 10^8, within int (2e9). So safe.
Now we must also consider that the pair might be such that a and b are equal? In cand list, since we only include distinct lengths with freq>=2, and if there was a length with freq>=4 we already handled square. So cand will have distinct lengths. So a != b.
Now one more nuance: Could there be a case where we have a length with freq >=4 but there also exists a pair of distinct lengths with ratio extremely close to 1, maybe even 1.001? But square gives exactly 1, so square is strictly better (16 vs >16). So square wins.
Now confirm: f = 4 (x/y + y/x + 2). For square, x/y=1, f=16. For rectangle with ratio 1.001, x/y ≈1.001, then x/y+y/x ≈ 1.001+0.999 = 2.000001? Actually 1.001 + 1/1.001 ≈ 1.001 + 0.999001 = 2.000001, plus 2 => 4.000001, times 4 = 16.000004. So indeed slightly larger. So square is optimal.
Now we must also consider if there is a scenario where we have both a square and a rectangle, but due to some constraints the square might not be allowed? No, square is a rectangle, allowed. So yes.
Thus algorithm final.
Now we should test some edge cases.
Case: n=4, sticks: 1 1 1 1? But a_j >=1, so possible. freq[1]=4 => square, output 1 1 1 1.
Case: n=5, sticks: 1 2 2 3 3? But rectangle possible? We need two equal pairs. Here we have 2:2 (two sticks), 3:2 (two sticks), 1:1 (not enough). So rectangle (2,3). So output 2 2 3 3.
Case: n=6, sticks: 5 5 6 6 7 7. Candidates: (5,6) ratio 1.2, (6,7) ratio ~1.1667, (5,7) ratio 1.4. So pick (6,7) because ratio 7/6≈1.1667 < 6/5=1.2. So algorithm picks (6,7). Good.
Now, we must also verify that we have to output exactly four sticks. We can output any order. So we can output for square: l l l l. For rectangle: x x y y. Or mix: x y x y etc.
Now, ensure that for each test, we output on a separate line.
Now, consider performance: The input size total 1e6 numbers, so reading using scanf or fast IO is fine.
Now, an alternative method: Since a_i <= 10000, we can just use a global freq array and for each test, after reading, we can iterate from 1 to 10000 to find lengths with freq>=2. That would be O(10000 * T). As argued, if T=250k, that's 2.5e9 operations, too high. So we must avoid that. So we will use used list.
But we should also consider that the sum of n is 1e6, so T could be up to 250k as reasoned. But can T be that high? n >=4 for each test, so max T = 1e6/4 = 250k. That's possible. So we need to ensure per test overhead is O(n_i + number of distinct). In worst-case, per test n_i=4, distinct could be up to 4. So per test we do O(4) work plus maybe a small constant overhead. That's fine. Sorting distinct lengths (cand) with up to maybe 4 elements is trivial. So overall total operations ~ sum n_i + sum distinct_i ~ O(1e6). So that's good.
Thus using used list is efficient.
Implementation specifics in C++:
We'll declare:
const int MAXL = 10000; // maybe 10000 is max value? Actually a_j <= 10^4, so up to 10000 inclusive. But check: constraints say a_j ≤ 10^4, so max value 10000. So we need array size 10001.
int freq[10001]; // global, initialize to 0 before first test.
Then inside main:
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector used;
used.reserve(min(n, 10000)); // optional
for (int i=0; i<n; ++i) {
int a;
cin >> a;
if (freq[a] == 0) used.push_back(a);
++freq[a];
}
// check for square
int square_len = -1;
for (int l : used) {
if (freq[l] >= 4) {
square_len = l;
break;
}
}
if (square_len != -1) {
// output square
cout << square_len << ' ' << square_len << ' ' << square_len << ' ' << square_len << '\n';
// clear freq
for (int l : used) freq[l] = 0;
continue;
}
// else, gather lengths with freq >=2
vector cand;
for (int l : used) {
if (freq[l] >= 2) cand.push_back(l);
}
sort(cand.begin(), cand.end());
int best_a = -1, best_b = -1;
for (size_t i = 0; i+1 < cand.size(); ++i) {
int a = cand[i];
int b = cand[i+1];
if (best_a == -1) {
best_a = a; best_b = b;
} else {
// compare b/a vs best_b/best_a
if (b * best_a < a * best_b) {
best_a = a; best_b = b;
}
}
}
// output best_a twice and best_b twice
cout << best_a << ' ' << best_a << ' ' << best_b << ' ' << best_b << '\n';
// clear freq
for (int l : used) freq[l] = 0;
}
We need to ensure that cand has at least 2 elements. By problem guarantee, there is at least one rectangle, so either square or at least two distinct lengths with freq>=2. In the non-square case, we must have at least two distinct lengths with freq>=2. So cand.size() >=2.
Potential pitfalls: In the used list, we have all distinct lengths, but some lengths may have freq=1, which we ignore when building cand. So cand may be smaller. But guarantee rectangle exists, so either square or at least two distinct with freq>=2. So fine.
Now we also need to consider the possibility that there is a square and also some other pair, but we already output square. So that's fine.
Now check for memory: used vector may contain up to 10000 distinct lengths per test, but okay.
Now we should test with a custom case: T=1, n=6, sticks: 2 2 2 2 3 3. freq[2]=4, freq[3]=2. Square exists (2). So answer should be four 2's. Our algorithm: used includes 2 and 3. Check for freq>=4: for l=2, freq[2]=4 -> square_len=2, break. Output 2 four times. That's correct.
Now test where multiple squares: e.g., 2:4, 5:4. We break on first encountered (2). That's acceptable.
Now test where no square but multiple consecutive pairs. Works.
Now test with large values but within 10000.
Now one more nuance: The problem says "If there are multiple answers, print any of them." So our selection is fine.
Now confirm the output format: print four numbers separated by spaces. Example: "2 7 7 2". That's fine.
Now confirm that the output order is arbitrary, so we can output "2 2 7 7" or "2 7 2 7". All good.
Now we should think if there is any scenario where using a length with freq >=2 but also maybe we could use a length with freq >= 2 but combine with same length if freq >= 2? That would be square if freq >=4 else not possible. So no.
Now think about another approach: Could there be a scenario where the optimal rectangle might involve using a length that appears 3 times? No, because we need exactly 2 sticks for each side; if we have 3 sticks of length L, we can only use 2 of them; the third is extra. But it doesn't affect the rectangle's shape; it's just availability. So for rectangle shape, only the existence of at least 2 matters.
Now ensure we properly clear freq for used after each test. Because we reused the global array, and we push lengths into used. At the end, for each l in used, set freq[l]=0. But careful: When we break early after square, we still need to clear freq. So we have to clear before continue. We'll do that.
Now one more optimization: Instead of iterating over used again to clear after output, we could clear after each test regardless of path. So after output (before continue), we clear. That's what we have.
Now we need to handle large T with many small tests: In each test we allocate vectors used and cand. That's okay because they are small. But repeated allocation could cause overhead; we can reuse vectors by clearing them, but it's fine.
Now check potential bug: If we find square_len in the used loop, we break. However we haven't processed all used. That's okay because we output square and then clear freq. But note: we haven't yet built cand. That's fine. However, the used vector still contains all distinct lengths we encountered. We need to clear freq for all of them. That's done after the if block. But we also need to skip the rest of the loop for this test. So we have a continue after output and clearing. Good.
Now if we don't find square, we go to cand building. After that we output and clear. Good.
Now, for cross multiplication, we use b * best_a < a * best_b to decide if current ratio is smaller. That's correct because if b/a < best_b/best_a <=> b * best_a < a * best_b.
Now what if best_a, best_b are from first pair and later we find a pair with same ratio? The condition uses <, so we keep the first. That's fine because any works.
Now we must consider that cand list may contain only two elements, then we only have one pair, and best set correctly.
Now test with cand having more than two: works.
Now one more nuance: Since we are only considering lengths with freq>=2, but what about the case where there is a length with freq >=4 but we also have other lengths; we output square. That's optimal. So fine.
Now, could there be a scenario where the best rectangle is not given by the two closest lengths due to something like the P^2/S formula? Let's double-check the derivation: f = P^2/S = 4 (x+y)^2 / (xy) = 4 (x^2 + 2xy + y^2)/(xy) = 4(x/y + y/x + 2). Yes only depends on ratio. So minimizing ratio is correct.
But wait: The ratio is max/min. If we have lengths a < b, ratio = b/a. But if we have lengths a < b < c, we compare b/a vs c/b. Could there be a scenario where b/a > c/b but the rectangle formed by a and b yields a smaller f than by b and c? Because f = 4 (b/a + a/b + 2) = 4 (b/a + a/b + 2). Since a/b = 1/(b/a). So f is a function g(r)=4(r + 1/r + 2) with r>=1. g(r) is increasing for r>=1. So indeed smaller r yields smaller f. So we want smallest possible r. So the pair with smallest ratio is best. Among sorted list, the smallest ratio is among consecutive elements. So correct.
Now confirm monotonicity of g(r): derivative g'(r)=4(1 - 1/r^2) >0 for r>1. So yes.
Now ensure that we treat r as larger/smaller. So if we pick (2,3) ratio 1.5, vs (3,4) ratio 1.333? Actually 4/3≈1.333 < 1.5, so (3,4) better. Indeed g(1.333) smaller. So we need to compute ratio correctly: b/a where a < b. So consecutive in sorted order yields candidate ratios. The minimal ratio among them is the answer.
Now, what about case where multiple consecutive pairs have same ratio? e.g., lengths 2,4,8 are in geometric progression: ratios 2,2. Both same. We can pick either.
Now, cross multiplication will treat equal as not less, so first will be kept.
Now finalize.
Potential issues: The input may contain large n, but we use vector used; we push when freq[a]==0. However, if we have many duplicates, used size is small. That's fine.
Now confirm memory: freq array is of size 10001; that's small.
Now, we need to handle the case where there is a length with frequency >=4 but also a length with frequency >=2? Already covered.
Now, let's also consider possibility of integer overflow in freq counting: freq values up to n (1e6) which fits in int.
Now test with random data to ensure algorithm picks correct rectangle.
I'll do a quick mental test: sticks: 10,10,11,11,12,12,100,100. cand: [10,11,12,100]. Ratios: 11/10=1.1, 12/11≈1.0909, 100/12≈8.33. Min is 12/11≈1.0909 from pair (11,12). So answer: 11,11,12,12. That would have f=4(12/11+11/12+2)=4(1.0909+0.9167+2)=4(4.0076)=16.0304. Compare to (10,11): f=4(1.1+0.9091+2)=4(4.0091)=16.0364. So (11,12) slightly better. Good.
Now, but note that (10,12) ratio 1.2 yields f=4(1.2+0.8333+2)=4(4.0333)=16.1333. So indeed consecutive gives best.
Thus algorithm stands.
Now, there's a nuance: the problem says "It is guaranteed that it is always possible to choose such sticks." So we don't need to worry about no solution.
Now, think about corner case: n=4, all sticks distinct: That can't form rectangle because we need two pairs equal. But guarantee ensures not happen. So ignore.
Now, implementation in C++.
Potential speed-ups: Use C-style input (scanf) to be safe. But with ios_base sync off, cin is fine for 1e6 numbers. However, some judges might be strict; we can use scanf.
I'll use scanf.
Now, for output, we can use printf for each line: printf("%d %d %d %d\n", a, a, b, b); But careful with square case: printf("%d %d %d %d\n", l, l, l, l);
Now, we must ensure that we output exactly four numbers and newline.
Now, final code:
#include
#include
#include
using namespace std;
const int MAXL = 10000;
int freq[MAXL+1]; // index up to 10000
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
vector used;
used.reserve(min(n, MAXL+1));
for (int i=0; i<n; ++i) {
int a;
scanf("%d", &a);
if (freq[a] == 0) used.push_back(a);
++freq[a];
}
// check for square
int square_len = -1;
for (int l : used) {
if (freq[l] >= 4) {
square_len = l;
break;
}
}
if (square_len != -1) {
printf("%d %d %d %d\n", square_len, square_len, square_len, square_len);
// clear frequencies
for (int l : used) freq[l] = 0;
continue;
}
// gather lengths with at least two sticks
vector cand;
for (int l : used) {
if (freq[l] >= 2) cand.push_back(l);
}
sort(cand.begin(), cand.end());
int best_a = cand[0];
int best_b = cand[1]; // at least 2 elements
for (size_t i = 1; i+1 < cand.size(); ++i) {
int a = cand[i];
int b = cand[i+1];
// compare b/a vs best_b/best_a
if (b * best_a < a * best_b) {
best_a = a;
best_b = b;
}
}
printf("%d %d %d %d\n", best_a, best_a, best_b, best_b);
// clear frequencies
for (int l : used) freq[l] = 0;
}
return 0;
}
Now, check: We initialized best_a and best_b to first pair (cand[0], cand[1]). Then we loop from i=1 to size-2. That's correct: we already considered pair (cand[0], cand[1]), then consider (cand[1], cand[2]), etc.
But careful: If cand.size() is exactly 2, we skip loop. That's fine.
Now we used condition (b * best_a < a * best_b) to update. But note that we might also want to consider ties? Not needed.
Now, ensure that best_a and best_b are the smaller and larger respectively: Since cand is sorted ascending, cand[i] < cand[i+1] for all, so best_a < best_b. So output ordering fine.
Now, one more nuance: In square case, we break early in loop over used. But used may contain many elements. We clear all at end. That's fine.
Now, we should also consider the scenario where the square length is encountered but there might be another square length later; we break on first, fine.
Now, memory: used vector might contain duplicates? No, because we push only when freq[a]==0. So unique.
Now, potential bug: Since we increment freq before pushing used? Let's examine: We check if (freq[a] == 0) before increment. Since initially freq is 0 for all, we push when first occurrence. After we increment, freq[a] becomes 1. So later occurrences will see freq[a] >0 and not push again. So used contains each distinct length exactly once. Good.
Now, after we finish a test, we clear freq for each used length. This resets them to 0 for next test. Good.
Now, we should also consider that the value MAXL is 10000, but a_j could be up to 10^4 inclusive, so array index up to 10000. We define size 10001.
Now test with sample:
Input:
3
4
7 2 2 7
8
2 8 1 4 8 2 1 5
5
5 5 5 5 5
Our code:
Test1: used = [7,2]? Actually order: first read 7: freq[7]==0 -> used push 7, then ++ ->1. read 2: freq[2]==0 -> used push 2, freq[2]=1. read 2: freq[2]!=0, ++ ->2. read 7: ++ ->2. used = [7,2]. Check square: 7->freq=2 (<4), 2->freq=2 (<4). No square. cand: both have freq>=2: cand = [7,2]. sort -> [2,7]. best_a=2, best_b=7. Loop? cand.size=2 skip. Output "2 2 7 7". That's valid but example output is "2 7 7 2". Both fine.
Test2: used distinct: 2,8,1,4,5. Frequencies: 2:2,8:2,1:2,4:1,5:1. square? none. cand: lengths with freq>=2: 1,2,8. Sort -> [1,2,8]. best_a=1, best_b=2. Loop i=1: a=2,b=8; compare 81=8 vs 22=4? Actually 81=8, abest_b = 22=4? Wait condition: if (b * best_a < a * best_b) -> 81=8 < 2*2=4? false. So keep (1,2). Output "1 1 2 2". Example output "2 2 1 1". Both fine.
Now confirm the output format: each answer on separate line. Good.
Now think about potential pitfalls with I/O: using scanf and printf is fine.
Now we need to consider that T can be 0? The problem says T >= 1, so fine.
Now about total stick count: sum n <= 10^6, but we must allocate used vector per test with at most distinct counts; this sum across tests is <= 10^6 distinct entries (each stick could contribute to used if it's first occurrence; but first occurrence is per distinct per test, not across tests). But each test's used size is at most distinct in that test, which sum across tests could be up to total n because each stick could be the first occurrence of a length that hasn't been seen before in that test. However, if many tests have many distinct lengths, sum of used sizes across tests could be up to total n (since each test's used size <= n_i, and sum n_i = 1e6). So total memory allocation across tests is linear.
Now, is there any scenario where the algorithm fails due to using only consecutive pairs? Let's double-check with a constructed case: Suppose we have lengths: 2 (freq>=2), 3 (freq>=2), and 4 (freq>=2). Sorted: 2,3,4. Ratios: 3/2=1.5, 4/3≈1.333. So best is (3,4). That yields ratio 1.333. Could there be a rectangle using (2,4) with ratio 2? Worse. So fine.
Now consider lengths: 6,8,9. Ratios: 8/6=1.333, 9/8=1.125. So best (8,9). Good.
Now, what about lengths: 100, 101, 200. Ratios: 101/100=1.01, 200/101≈1.980. So best is (100,101). Good.
Now, any scenario where a non-consecutive pair yields a ratio that is actually smaller than any consecutive? Suppose sorted L1 < L2 < L3. Could L3/L1 be less than L2/L1? No, because L3 > L2, so L3/L1 > L2/L1. Could L3/L1 be less than L3/L2? That would be L3/L1 < L3/L2 => 1/L1 < 1/L2 => L2 < L1, contradiction. So no.
Thus algorithm is correct.
Now consider case where there is a square but also a pair with ratio extremely close to 1 but square is better. So we output square.
Now, problem statement: "Your task is to choose exactly four of them in such a way that they can form a rectangle." That means we must select exactly four sticks. So output should be the four chosen sticks, not just lengths. So we output lengths of the four sticks. In our answer, we output four numbers: for square we output the length four times; for rectangle we output each length twice. That's correct because we are selecting two sticks of each length. But we must ensure that there are indeed at least 2 sticks of each chosen length. We check that.
Now consider that if there is a length with frequency exactly 3 and we choose square? No, need 4. So square only if >=4.
Now, if there is a length with frequency 3 and another length with frequency 3, we can choose rectangle (x,y) using two of each, leaving one stick of each unused. That's fine.
Now, we need to ensure that our algorithm does not inadvertently output a rectangle that uses a length that appears only once (but we have filtered cand to freq>=2). So good.
Now, one more nuance: The problem says "The total number of sticks in all T lists doesn't exceed 10^6 in each testcase." The phrase "in each testcase" might mean per input file? Possibly means overall across T. So we treat as total across tests <= 1e6. So our approach is fine.
Now, potential improvement: We could avoid sorting cand by scanning through used frequencies in order since values are up to 10000. But we have used vector; sorting cand is cheap. However, if we want to avoid sorting, we could simply iterate i from 1 to 10000 and keep track of previous length with freq>=2. That would be O(10000) per test, which as argued might be heavy if T huge. But with used list we avoid that. But we could also do: after building cand, we can sort. Since cand size is at most number of lengths with freq>=2, which is usually small. Even if all 10000 lengths have freq>=2, then cand size = up to 10000. That would require sorting 10000 numbers, which is trivial (10000 log 10000 ~ 132k comparisons). But such case would have n at least 20000 (since each length at least 2). So n would be large, T would be small. So sorting per test is fine.
But the issue is per test overhead of scanning 10000 might be heavy for many small tests. So using used list is better.
Now, we should consider if there is any scenario where the rectangle could be formed by using a length that appears exactly 2 times and another length that appears exactly 2 times, but our algorithm chooses the pair with minimal ratio. That's correct.
Now, one more nuance: Could there be a scenario where there are two pairs of lengths with same ratio, but one pair yields a different value of P^2/S? Since f only depends on ratio, same ratio yields same f. So either is fine.
Now, we can produce final answer.
But I'd like to double-check the mathematical derivation: (P^2)/S = (2*(x+y))^2/(x*y) = 4(x+y)^2/(xy) = 4(x^2+2xy+y^2)/(xy) = 4(x/y + y/x + 2). Indeed.
Now confirm with example: x=2,y=7, P^2=18^2=324, S=14, 324/14=23.142857, and 4*(2/7+7/2+2)=4*(0.2857+3.5+2)=4*5.7857=23.1428. Good.
Now, we should consider if the problem might have a twist: Could the rectangle be formed with sides (a,b) where the sticks used are not necessarily paired as opposite sides? But rectangle definition: opposite sides equal. So to form rectangle, we need two sticks of length a and two of length b. Could we form rectangle with sides a and b but using sticks of lengths a, b, a, b? Yes. So that's the only way.
Thus solution stands.
Now, I will finalize.
But before final, we should consider potential overflow in the product b * best_a when best_a and best_b are not initialized? We initialize them with first pair, so fine.
Now, consider if cand.size() < 2? That can't happen because rectangle exists, and no square, so there must be at least two distinct lengths with freq>=2. So safe.
Now, to be extra safe, we could assert.
Now, implement in code.
Edge Cases for I/O: Some judges require not to use fast I/O with both scanf and cin mixing. We'll stick with scanf and printf.
Now, I'll produce final answer with explanation.
But before final, maybe there is a more subtle nuance: The value (P^2)/S = 4(x/y + y/x + 2). For fixed x/y, it's minimal at x/y=1. But maybe there is a case where the rectangle with sides (x,y) such that x and y are not the closest ratio? Already disproven.
Now, double-check with large numbers: if x=1, y=10000, ratio=10000, f huge. So we want close.
Now, I think it's correct.
Now, I'll prepare final answer.
But I'll also test with a random small case by mental simulation to be safe.
Case: n=6, sticks: 3,3,4,4,5,5. Frequencies: 3:2,4:2,5:2. No square. Cand sorted: [3,4,5]. Ratios: 4/3≈1.3333, 5/4=1.25. So best pair (4,5). Output 4,4,5,5. f=4(5/4+4/5+2)=4(1.25+0.8+2)=4(4.05)=16.2. Compare (3,4): 4(1.333+0.75+2)=4(4.08333)=16.3333. So indeed (4,5) better.
Now, if we had 2,2,3,3,10,10: cand [2,3,10]. Ratios: 3/2=1.5, 10/3≈3.33 -> best (2,3). So output 2,2,3,3. f=4(1.5+0.6667+2)=4(4.1667)=16.6668. Could (3,10) be better? No. (2,10) ratio 5 -> f=4(5+0.2+2)=4(7.2)=28.8.
So correct.
Now, note that the values are not integers necessarily, but we don't need to compute.
Now, finalize.
One more improvement: For square case, we don't need to build cand, but we still need to clear freq. So we are good.
Now, what about memory leaks? No.
Now, final code.
But before final, double-check that we don't need to handle the scenario where there are multiple squares and also other lengths, but we output a square. That's optimal.
Now, produce final answer with explanation.
Now, regarding the note in problem: Example output: for second list "2 2 1 1" not "1 1 2 2". So order doesn't matter.
Now, answer.
We'll provide a solution description: Count frequencies, if any length appears at least 4, output that length four times. Else, consider all lengths that appear at least twice, sort them, then among consecutive pairs, find the one with the smallest ratio (larger/smaller), output that pair twice each. This minimizes (P^2)/S.
Now, I'll prepare final ChatGPT answer with explanation and code. ```cpp
#include
#include
#include
using namespace std;
const int MAXL = 10000;
int freq[MAXL + 1]; // global frequency array (a_i ≤ 10^4)
int main() {
int T;
scanf("%d", &T);
code
while (T--) {
int n;
scanf("%d", &n);
vector<int> used; // distinct lengths encountered in this test
used.reserve(min(n, MAXL + 1));
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
if (freq[a] == 0) {
used.push_back(a);
}
++freq[a];
}
// 1. Check for a square (any length with at least 4 sticks)
int square_len = -1;
for (int l : used) {
if (freq[l] >= 4) {
square_len = l;
break;
}
}
if (square_len != -1) {
printf("%d %d %d %d\n", square_len, square_len, square_len, square_len);
// clean up frequencies for the next test
for (int l : used) freq[l] = 0;
continue;
}
// 2. No square -> need two distinct sides, each with frequency ≥ 2
vector<int> cand; // lengths that appear at least twice
for (int l : used) {
if (freq[l] >= 2) {
cand.push_back(l);
}
}
sort(cand.begin(), cand.end());
// cand is guaranteed to have at least two elements (rectangle exists)
int best_a = cand[0];
int best_b = cand[1];
// Among consecutive lengths, choose the pair with smallest ratio b/a
for (size_t i = 1; i + 1 < cand.size(); ++i) {
int a = cand[i];
int b = cand[i + 1];
// Compare b/a with best_b/best_a without floating point:
// b/a < best_b/best_a <=> b * best_a < a * best_b
if (b * best_a < a * best_b) {
best_a = a;
best_b = b;
}
}
printf("%d %d %d %d\n", best_a, best_a, best_b, best_b);
// clean up frequencies for the next test
for (int l : used) freq[l] = 0;
}
return 0;