You are given a list of n
integers a1,a2,…,an
. You need to pick 8
elements from the list and use them as coordinates of four points. These four points should be corners of a rectangle which has its sides parallel to the coordinate axes. Your task is to pick coordinates in such a way that the resulting rectangle has the maximum possible area. The rectangle can be degenerate, i. e. its area can be 0
. Each integer can be used as many times as it occurs in the list (or less).
Input
The first line contains one integer t
(1≤t≤25000
) — the number of test cases.
The first line of each test case contains one integer n
(8≤n≤2⋅105
).
The second line of each test case contains n
integers a1,a2,…,an
(−109≤ai≤109
).
Additional constraint on the input: the sum of n
over all test cases does not exceed 2⋅105
.
Output
For each test case, print the answer as follows:
if it is impossible to construct a rectangle which meets the constraints from the statement, print a single line containing the word NO (case-insensitive);
otherwise, in the first line, print YES (case-insensitive). In the second line, print 8
integers x1,y1,x2,y2,x3,y3,x4,y4
— the coordinates of the corners of the rectangle. You can print the corners in any order.
Example
InputCopy
3
16
-5 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10
8
0 0 -1 2 2 1 1 3
8
0 0 0 0 0 5 0 5
OutputCopy
YES
1 2 1 7 6 2 6 7
NO
YES
0 0 0 5 0 0 0 5 please type correctly in c++.Please!!!
Assistant
Sure, I'll provide a C++ solution to the problem described. The problem requires identifying whether it is possible to form a rectangle with sides parallel to the coordinate axes using eight elements from a given list of integers. If possible, we must select the rectangle with the maximum possible area.
To solve this problem efficiently, especially given the constraints with up to 25,000 test cases and a total of 200,000 elements across all test cases, we need to carefully manage our approach:
Frequency Counting: For each test case, we count the frequency of each integer. Only integers that appear at least twice can potentially be used as either x or y coordinates because each coordinate of the rectangle corners needs to appear twice.
Candidate Selection: We sort the unique integers that have a frequency of at least 2. To maximize the area, we should focus on the smallest and largest values since they are likely to provide the maximum possible difference (and hence area). We select a subset of these values (for example, up to 10 from the smallest and 10 from the largest) to reduce the number of combinations to evaluate.
Rectangle Validation: For each possible combination of x1, x2, y1, and y2 from the selected candidates, we check if forming a rectangle is feasible by ensuring the required frequencies are met. Specifically:
If x1 == x2 and y1 == y2, the integer must appear at least four times.
If only x1 == x2 or y1 == y2, the overlapping integer must appear at least four times, while the other integers must appear at least twice.
Otherwise, all four integers must appear at least twice.
Max Area Calculation: We calculate the area for each valid rectangle and track the one with the maximum area.
Output: If a valid rectangle is found, we output "YES" followed by the eight coordinates. Otherwise, we output "NO".
Here is the C++ implementation of the solution:
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<long long> a(n);
for(auto &x:a) cin >> x;
sort(a.begin(), a.end());
// Count frequencies
vector<pair<long long, int>> freq;
int cnt=1;
for(int i=1;i<n;i++){
if(a[i]==a[i-1]){
cnt++;
}
else{
freq.emplace_back(make_pair(a[i-1], cnt));
cnt=1;
}
}
freq.emplace_back(make_pair(a[n-1], cnt));
// Collect unique list with freq >=2
vector<long long> unique_list;
for(auto &[num, count]: freq){
if(count >=2) unique_list.push_back(num);
}
int k = unique_list.size();
if(k <1){
// Check if any number has freq >=4 for degenerate rectangle
bool possible = false;
for(auto &[num, count]: freq){
if(count >=4){
possible = true;
break;
}
}
if(possible){
// Find a number with freq >=4
long long num;
for(auto &[nume, count]: freq){
if(count >=4){
num = nume;
break;
}
}
// Output rectangle with all points as (num, num)
cout << "YES\n";
cout << num << " " << num << " " << num << " " << num << " " << num << " " << num << " " << num << " " << num << "\n";
}
else{
cout << "NO\n";
}
continue;
}
// If we have at least two unique numbers, try to form rectangle
// To maximize area, take candidates from smallest and largest
int m = min(10, k); // adjust m as needed
vector<long long> candidates;
for(int i=0;i<m;i++) candidates.push_back(unique_list[i]);
for(int i=max((int)k -m, 0); i<k;i++) candidates.push_back(unique_list[i]);
// Remove duplicates
sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
// Now iterate over all possible x1, x2 and y1, y2
// and find the rectangle with the maximum area
long long max_area = -1;
vector<long long> best_rect;
int cand_size = candidates.size();
for(int i=0;i<cand_size;i++){
for(int j=i; j<cand_size; j++){
long long x1 = candidates[i];
long long x2 = candidates[j];
for(int p=0; p<cand_size; p++){
for(int q=p; q<cand_size; q++){
long long y1 = candidates[p];
long long y2 = candidates[q];
// Calculate required frequencies
// Initialize required counts
// Use a map with at most 4 keys
// To optimize, use temporary variables
// Check cases
if(x1 == x2 && y1 == y2){
if(x1 == y1){
// Need freq[x1] >=4
// Check if x1 exists with freq >=4
// Find in freq
int required = 4;
bool ok = false;
for(auto &[num, count]: freq){
if(num == x1){
if(count >=4){
ok = true;
}
break;
}
}
if(ok){
long long area = 0;
if(area > max_area){
max_area = area;
best_rect = {x1, y1, x1, y1, x1, y1, x1, y1};
}
}
}
else{
// Need freq[x1] >=2 and freq[x2] >=2
// x1 ==x2 is false here
// Wait, if x1 ==x2 and y1 ==y2 but x1 != y1
// Need freq[x1] >=4 and freq[y1] >=2 and freq[y2} >=2
// But y1 == y2, so y2 == y1
// Thus, need freq[x1} >=4 and freq[y1} >=2
bool ok = false;
int cnt_x =0, cnt_y=0;
for(auto &[num, count]: freq){
if(num == x1){
cnt_x = count;
}
if(num == y1){
cnt_y = count;
}
}
if(cnt_x >=4 && cnt_y >=2){
long long area =0;
if(area > max_area){
max_area = area;
best_rect = {x1, y1, x1, y1, x1, y1, x1, y1};
}
}
}
}
else if(x1 == x2){
// Need freq[x1] >=4
// and y1 != y2 and freq[y1} >=2 and freq[y2} >=2
if(y1 == y2){
// Rectangle would have all points same if x1==x2 and y1==y2
// Already handled above
continue;
}
bool ok = false;
int cnt_x=0, cnt_y1=0, cnt_y2=0;
for(auto &[num, count]: freq){
if(num == x1){
cnt_x = count;
}
if(num == y1){
cnt_y1 = count;
}
if(num == y2){
cnt_y2 = count;
}
}
if(cnt_x >=4 && cnt_y1 >=2 && cnt_y2 >=2){
// Calculate area
long long area = abs(x1 -x2) * abs(y1 -y2);
if(area > max_area){
max_area = area;
best_rect = {x1, y1, x1, y2, x1, y1, x1, y2};
}
}
}
else if(y1 == y2){
// Need freq[y1} >=4
// and x1 != x2 and freq[x1} >=2 and freq[x2} >=2
bool ok = false;
int cnt_y=0, cnt_x1=0, cnt_x2=0;
for(auto &[num, count]: freq){
if(num == y1){
cnt_y = count;
}
if(num == x1){
cnt_x1 = count;
}
if(num == x2){
cnt_x2 = count;
}
}
if(cnt_y >=4 && cnt_x1 >=2 && cnt_x2 >=2){
// Calculate area
long long area = abs(x1 -x2) * abs(y1 -y2);
if(area > max_area){
max_area = area;
best_rect = {x1, y1, x2, y1, x1, y1, x2, y1};
}
}
}
else{
// x1 !=x2 and y1 != y2
// Need freq[x1} >=2, freq[x2} >=2, freq[y1} >=2, freq[y2} >=2
// Additionally, if any number is used in both x and y, freq >=4
// Count overlapping
// Count how many times each number is used
// Since x1 !=x2 and y1 !=y2, the overlaps can be 0,1,2
// Implement accordingly
// Count occurrences
// Use a map with at most 4 keys
// Implement manually
// Initialize counts
int cnt_x1 =0, cnt_x2=0, cnt_y1=0, cnt_y2=0;
for(auto &[num, count]: freq){
if(num == x1){
cnt_x1 = count;
}
if(num == x2){
cnt_x2 = count;
}
if(num == y1){
cnt_y1 = count;
}
if(num == y2){
cnt_y2 = count;
}
}
bool ok = true;
// Check for overlaps
// If x1 == y1 or y2
int required_x1 = 2, required_x2 =2, required_y1=2, required_y2=2;
if(x1 == y1) required_x1 +=2;
if(x1 == y2) required_x1 +=2;
if(x2 == y1) required_x2 +=2;
if(x2 == y2) required_x2 +=2;
// Now check
if(cnt_x1 < required_x1 || cnt_x2 < required_x2 ||
(y1 !=x1 && cnt_y1 <2) ||
(y2 !=x1 && y2 !=x2 && cnt_y2 <2)){
ok = false;
}
if(ok){
// Calculate area
long long area = abs(x1 -x2) * abs(y1 -y2);
if(area > max_area){
max_area = area;
best_rect = {x1, y1, x1, y2, x2, y1, x2, y2};
}
}
}
}
}
}
}
if(max_area == -1){
cout << "NO\n";
}
else{
cout << "YES\n";
for(int i=0;i<8;i++) cout << best_rect[i] << (i<7?" ":"\n");
}
}
}