C. Minimum Value Rectangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have nn sticks of the given lengths.
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 SS be the area of the rectangle and PP be the perimeter of the rectangle.
The chosen rectangle should have the value P2SP2S 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 TT (T≥1T≥1) — the number of lists of sticks in the testcase.
Then 2T2T lines follow — lines (2i−1)(2i−1) and 2i2i of them describe the ii-th list. The first line of the pair contains a single integer nn (4≤n≤1064≤n≤106) — the number of sticks in the ii-th list. The second line of the pair contains nn integers a1,a2,…,ana1,a2,…,an (1≤aj≤1041≤aj≤104) — lengths of the sticks in the ii-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 TT lists doesn't exceed 106106 in each testcase.
Output
Print TT lines. The ii-th line should contain the answer to the ii-th list of the input. That is the lengths of the four sticks you choose from the ii-th list, so that they form a rectangle and the value P2SP2S 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
Copy
3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5
output
Copy
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 22 and 77, its area is 2⋅7=142⋅7=14, perimeter is 2(2+7)=182(2+7)=18. 18214≈23.14318214≈23.143.
The second list contains subsets of four sticks that can form rectangles with sides (1,2)(1,2), (2,8)(2,8) and (1,8)(1,8). Their values are 622=18622=18, 20216=2520216=25 and 1828=40.51828=40.5, respectively. The minimal one of them is the rectangle (1,2)(1,2).
You can choose any four of the 55 given sticks from the third list, they will form a square with side 55, which is still a rectangle with sides (5,5)(5,5).
.题意:
是说给我们一些长度的火柴棍,让我们建立一个矩形,并且矩形的面积和长度得满足那个公式;输出需要的火柴棍
思路:
自己写了个代码超时,还是在贪心策略的选择上。
我们要进来选择b/a+a/b小的矩形,然后我们发现a和b最接近的时候,才满足这个式子尽可能小。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 20
#define INF 0x3f3f3f3f
using namespace std;
int a[10000];//每个数据大小不超过1e4
int b[1000000];//总共有1e6个数据
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,p=0,i;
for(i=0;i<=10000;i++)a[i]=0;//一般超时可能会是memset的原因,改成这样就没问题了
scanf("%d",&n);
for(i=1; i<=n; i++)
{
int m;
scanf("%d",&m);
a[m]++;
if(a[m]==2||a[m]==4)//凡是出现了4次或者2次的都加进去,都可以做边
b[++p]=m;
}
sort(b+1,b+1+p);
int x,y;
double minn=inf;// 注意数据类型
for(i=1; i<p; i++)
{
if(b[i]==b[i+1])
{
x=b[i],y=b[i+1];//如果相等,就是出现了大于等于4次的情况
break;
}
double m=b[i+1]*1.0/b[i];//由于那个公式,化简一下P^2/S,边长是a,b的话,就是4*(a/b+b/a+2);
//基本不等式只有当a==b的时候等式才成立,在这个题中只有当a和b靠的很近的时候才满足
if(m<minn){minn=m;x=b[i];y=b[i+1];}
}
printf("%d %d %d %d\n",x,x,y,y);
}
return 0;
}