Educational Codeforces Round 49 C Minimum Value Rectangle

题目地址:http://codeforces.com/contest/1027/problem/C

贪心,每次一定要找到最优,不要猜最优的情况。。。
设矩形长为a,宽为b
p^2/ s = (2a + 2b)^2 / (a * b) 化简可得:4 * (b / a + a / b) + 8
所以按b / a + a / b 选取最优情况
先将长度存在两次的存在一个数组中,再排序,相邻的两个长度比较取最小的情况
详细请看代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e4 + 5;

int a[N];
int b[1000005];


int main()
{
    int t;
    scanf("%d",&t);
    int n;
    while(t--){
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        int len = 0;
        for(int i = 0;i < n;++i){
            int x;
            scanf("%d",&x);
            a[x]++;
            if(a[x] == 2){
                b[len++] = x;
                a[x] = 0;
            }
        }
        sort(b,b + len);
        double MIN = inf;
        int p = b[0],q = b[1];
        for(int i = 1;i < len;++i)
        {
            double x = (b[i] * 1.0) / b[i - 1];
            if(x < MIN){
                MIN = x;
                p = b[i];
                q = b[i - 1];
            }
        }
        printf("%d %d %d %d\n",p,p,q,q);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/82178687