これは数学の問題です。
辺の長さがa、ba、bであると仮定しますA 、b、次に:
P 2 S =(2 a + 2 b)2 ab = 4 a 2 + 8 ab + 4 b 2 ab = 4(ab + ba)+ 8 \ dfrac {P ^ 2} {S} = \ dfrac {(2a + 2b)^ 2} {ab} = \ dfrac {4a ^ 2 + 8ab + 4b ^ 2} {ab} = 4(\ dfrac {a} {b} + \ dfrac {b} {a})+ 8 SP2=a b(2 a+2 b )2=a b4 a2+8 a b+4 b2=4 (bA+AB)+8
基本的な不等式から、
4(ab + ba)+8≥164(\ dfrac {a} {b} + \ dfrac {b} {a})+ 8 \ geq 16 4 (bA+AB)+8≥1 6
そして、a、ba、bのときA 、bが近いほど、結果は小さくなります。
したがって、この質問は、どちらの2本のスティックの差が最も小さいかということになります。
シーケンスに偶数のスティックを追加し(スティックが4つ以上ある場合は、複数回挿入します)、並べ替えて見つけます。
コード:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 10, MAXA = 1e4 + 10;
int t, n, book[MAXA], fir, sec, d[MAXN];
double ans;
int read()
{
int sum = 0, fh = 1; char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {
sum = (sum << 3) + (sum << 1) + (ch ^ 48); ch = getchar();}
return sum * fh;
}
int main()
{
t = read();
while (t--)
{
memset(book, 0, sizeof(book));
fir = sec = 0; ans = 2147483647.0;
n = read(); d[0] = 0;
for (int i = 1; i <= n; ++i)
{
int tmp = read(); book[tmp]++;
if (!(book[tmp] & 1)) d[++d[0]] = tmp;
}
sort(d + 1, d + d[0] + 1);
for (int i = 1; i < d[0]; ++i)
{
double k = (double)4 * ((double)d[i] / d[i + 1] + (double)d[i + 1] / d[i]) + 8.0;
if (k < ans)
{
ans = k; fir = d[i]; sec = d[i + 1];
}
}
printf("%d %d %d %d\n", fir, fir, sec, sec);
}
return 0;
}