cf Educational Codeforces Round 49 C. Minimum Value Rectangle

原題:
You have n 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 S be the area of the rectangle and P be the perimeter of the rectangle.

The chosen rectangle should have the value (p^2)/s minimal possible. The value is taken without any rounding.

If there are multiple answers, print any of them.

Each test case 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 T (T≥1) — the number of lists of sticks in the testcase.
Then 2T lines follow — lines (2i−1) and 2i of them describe the i-th list. The first line of the pair contains a single integer n
(4≤n≤10^6) — the number of sticks in the i-th list. The second line of the pair contains n integers a1,a2,…,an
(1≤aj≤10^4) — lengths of the sticks in the i-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 T lists doesn’t exceed 10^6 in each testcase.

Output
Print T lines. The i-th line should contain the answer to the i-th list of the input. That is the lengths of the four sticks you choose from the i-th list, so that they form a rectangle and the value p^2/s 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
3
4
7 2 2 7
8
2 8 1 4 8 2 1 5
5
5 5 5 5 5
output
2 7 7 2
2 2 1 1
5 5 5 5

中文:

給你一個t,表示有多少個測試用例,每個測試先給出一個n,表示有n根棍子,接下來給你n個數,表示每根棍子的長度。

先問你用這些棍子組成一個矩形,使得這個矩形周長的平方除以面積的值最小。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int t,n;
map<int,int> mi;

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        int x;
        mi.clear();
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            mi[x]++;
        }
        vector<pii> vp;
        for(auto v:mi)
        {
            if(v.second>1)
                vp.push_back(make_pair(v.first,v.second));
        }
        int flag=0;
        for(int i=0;i<vp.size();i++)
        {
            if(vp[i].second>3)
            {
                flag=1;
                cout<<vp[i].first<<" "<<vp[i].first<<" "<<vp[i].first<<" "<<vp[i].first<<endl;
                break;
            }
        }
        if(flag)
            continue;
        double tmp=99999999;
        int ind;
        for(int i=1;i<vp.size();i++)
        {
            if(vp[i].first/(vp[i-1].first*1.0)<tmp)
            {
                tmp=vp[i].first/(vp[i-1].first*1.0);
                ind=i;
            }
        }
        cout<<vp[ind-1].first<<" "<<vp[ind-1].first<<" "<<vp[ind].first<<" "<<vp[ind].first<<endl;

    }
    return 0;
}

解答:

設矩形邊長爲a和b,把周長的平方除以面積的公式整理一下就可以得到爲4×(a/b+b/a+2)
那麼就相當於求解a/b+b/a的最小值。

emm

可以看成高中學過的對勾函數,知道當a==b的時候函數取最小值,也就是如果有一根棍子超過4根,那麼這些棍子就能組成目標最小值。
否則,由於棍子長度爲整數,所以a/b+b/a是單調増的,所以對排過序的棍子找a/b最小的那個即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章