CF1027C Minimum Value Rectangle 题解

这是一道数学题。

假设边长为 \(a,b\),那么:

\[\dfrac{P^2}{S}=\dfrac{(2a+2b)^2}{ab}=\dfrac{4a^2+8ab+4b^2}{ab}=4(\dfrac{a}{b}+\dfrac{b}{a})+8 \]

由基本不等式,

\[4(\dfrac{a}{b}+\dfrac{b}{a})+8 \geq 16 \]

而当 \(a,b\) 越接近时,结果越小。

于是这道题就变成了哪两个木棍差值最小。

我们将偶数根数的木棍加入一个序列(如果有 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;
}
posted @ 2022-04-14 21:13  Plozia  阅读(28)  评论(0编辑  收藏  举报