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
type in c++ greedy approach pleaseee
Assistant
Here's a C++ solution for the problem using a greedy approach. The solution involves the following steps:
Frequency Counting and Sorting:
For each test case, sort the list of integers and count the frequency of each unique number.
Collect all numbers with a frequency of at least 2 into a sorted list sorted_S.
Selecting Candidates for x1 and x2:
To maximize the area, we consider candidates from both ends of the sorted list. Specifically, we take the first k elements (smallest numbers) and the last k elements (largest numbers) as potential candidates for x1 and x2.
This reduces the number of combinations we need to check, keeping the solution efficient.
Finding y1 and y2:
For each valid pair (x1, x2), we adjust the frequencies to account for the usage of x1 and x2.
We then find the smallest and largest eligible numbers (y1 and y2) from sorted_S that can serve as the y coordinates.
Selecting the Best Rectangle:
We keep track of the maximum possible area and the corresponding rectangle coordinates.
If no valid rectangle with two distinct x and y coordinates is found, we check for degenerate rectangles where x1 = x2 or y1 = y2.
Output:
For each test case, if a valid rectangle is found, print "YES" followed by the coordinates of the four corners.
If no such rectangle exists, print "NO".
Here is the complete C++ code implementing the above approach:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<ll> a(n);
for(auto &x: a) cin >> x;
sort(a.begin(), a.end());
vector<pair<ll, int>> freq_list;
if(n ==0){
// Edge case, not possible due to constraints.
}
else{
ll current = a[0];
int count =1;
for(int i=1; i<n; ++i){
if(a[i] == current){
count++;
}
else{
freq_list.emplace_back(make_pair(current, count));
current = a[i];
count =1;
}
}
freq_list.emplace_back(make_pair(current, count));
}
// Collect sorted_S
vector<ll> sorted_S;
for(auto &[num, cnt]: freq_list){
if(cnt >=2){
sorted_S.emplace_back(num);
}
}
// Now, proceed to find selection
int m = sorted_S.size();
ll max_area = -1;
tuple<ll, ll, ll, ll> selection;
if(m >=1){
// Define k
int k = min(100, m);
vector<ll> x_candidates;
for(int i=0; i<k; ++i){
x_candidates.emplace_back(sorted_S[i]);
}
for(int i=max(0, m -k); i<m; ++i){
x_candidates.emplace_back(sorted_S[i]);
}
// Remove duplicates
sort(x_candidates.begin(), x_candidates.end());
x_candidates.erase(unique(x_candidates.begin(), x_candidates.end()), x_candidates.end());
// Iterate through x1 and x2
for(auto x1: x_candidates){
for(auto x2: x_candidates){
if(x1 > x2) continue;
// Check frequencies
// Get freq[x1] and freq[x2]
int freq_x1 = 0, freq_x2 =0;
// Binary search in freq_list
// Since freq_list is sorted, can use lower_bound
// Implement a lambda to find frequency
auto it1 = lower_bound(freq_list.begin(), freq_list.end(), make_pair(x1, 0),
[&](const pair<ll, int> &a, const pair<ll, int> &b)->bool{
return a.first < b.first;
});
if(it1 != freq_list.end() && it1->first == x1){
freq_x1 = it1->second;
}
auto it2 = lower_bound(freq_list.begin(), freq_list.end(), make_pair(x2, 0),
[&](const pair<ll, int> &a, const pair<ll, int> &b)->bool{
return a.first < b.first;
});
if(it2 != freq_list.end() && it2->first == x2){
freq_x2 = it2->second;
}
if(x1 ==x2){
if(freq_x1 <4) continue;
}
else{
if(freq_x1 <2 || freq_x2 <2) continue;
}
// Now, find y1 and y2
// Find y1
ll y1 = 0, y2 =0;
bool found_y1 = false, found_y2 = false;
for(auto y: sorted_S){
if(y == x1 && y ==x2){
if(x1 ==x2){
if(freq_x1 -4 >=2){
y1 = y;
found_y1 = true;
break;
}
}
else{
if(freq_x1 -2 >=2){
y1 = y;
found_y1 = true;
break;
}
}
}
else if(y ==x1 || y ==x2){
if(x1 ==x2){
// y ==x1 ==x2
if(freq_x1 -4 >=2){
y1 = y;
found_y1 = true;
break;
}
}
else{
if(freq_x1 >=2 && y ==x1 && (freq_x1 -2)>=2){
y1 = y;
found_y1 = true;
break;
}
if(freq_x2 >=2 && y ==x2 && (freq_x2 -2)>=2){
y1 = y;
found_y1 = true;
break;
}
}
}
else{
if(it1 != freq_list.end() && it1->first == y){
if(it1->second >=2){
y1 = y;
found_y1 = true;
break;
}
}
else{
// y not in freq_list or freq <2
// But sorted_S only contains freq >=2
y1 = y;
found_y1 = true;
break;
}
}
}
if(!found_y1){
continue;
}
// Find y2
for(auto it = sorted_S.rbegin(); it != sorted_S.rend(); ++it){
ll y = *it;
if(y ==x1 && y ==x2){
if(x1 ==x2){
if(freq_x1 -4 >=2){
y2 = y;
found_y2 = true;
break;
}
}
else{
if(freq_x1 -2 >=2){
y2 = y;
found_y2 = true;
break;
}
}
}
else if(y ==x1 || y ==x2){
if(x1 ==x2){
// y ==x1 ==x2
if(freq_x1 -4 >=2){
y2 = y;
found_y2 = true;
break;
}
}
else{
if(freq_x1 >=2 && y ==x1 && (freq_x1 -2)>=2){
y2 = y;
found_y2 = true;
break;
}
if(freq_x2 >=2 && y ==x2 && (freq_x2 -2)>=2){
y2 = y;
found_y2 = true;
break;
}
}
}
else{
auto it_freq = lower_bound(freq_list.begin(), freq_list.end(), make_pair(y, 0),
[&](const pair<ll, int> &a, const pair<ll, int> &b)->bool{
return a.first < b.first;
});
if(it_freq != freq_list.end() && it_freq->first == y){
if(it_freq->second >=2){
y2 = y;
found_y2 = true;
break;
}
}
else{
// y not in freq_list or freq <2
// But sorted_S only contains freq >=2
y2 = y;
found_y2 = true;
break;
}
}
}
if(!found_y2){
continue;
}
// Now, check y1 and y2
if(y1 < y2){
ll area = abs(x2 -x1) * abs(y2 -y1);
if(area > max_area){
max_area = area;
selection = make_tuple(x1, x2, y1, y2);
}
}
else if(y1 == y2){
// Check if y1 has freq >=4 if y1 == y2
bool ok = false;
if(y1 ==x1 && y1 ==x2){
if(freq_x1 >=6){
ok = true;
}
}
else if(y1 ==x1 || y1 ==x2){
if(x1 ==x2){
if(freq_x1 >=6){
ok = true;
}
}
else{
if((y1 ==x1 && freq_x1 >=4) || (y1 ==x2 && freq_x2 >=4)){
ok = true;
}
}
}
else{
// y1 is different from x1 and x2
// Since sorted_S only has freq >=2, need freq[y1] >=4 if y1==y2
auto it_y = lower_bound(freq_list.begin(), freq_list.end(), make_pair(y1, 0),
[&](const pair<ll, int> &a, const pair<ll, int> &b)->bool{
return a.first < b.first;
});
if(it_y != freq_list.end() && it_y->first == y1 && it_y->second >=4){
ok = true;
}
}
if(ok){
ll area =0;
if(area > max_area){
max_area =0;
selection = make_tuple(x1, x2, y1, y2);
}
}
}
}
}
}
if(max_area >=0){
// Build the coordinates
ll x1, x2, y1, y2;
tie(x1, x2, y1, y2) = selection;
// Output any order of four points
// Let's output (x1,y1), (x1,y2), (x2,y1), (x2,y2)
cout << "YES\n";
cout << x1 << " " << y1 << " " << x1 << " " << y2 << " " << x2 << " " << y1 << " " << x2 << " " << y2 << "\n";
}
else{
cout << "NO\n";
}
}
}