1. 程式人生 > >Codeforces-C. Minimum Value Rectangle

Codeforces-C. Minimum Value Rectangle

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).

題意:找出四個小棍可以拼成矩形,要求(周長的平方)/(面積)最小。

歸類:數論。

題解:可以根據不等式推匯出當 a=b 的時候可以取到最小值,所以我們只要求 a 與 b 最接近的兩個數就行。經過轉化可以變為:a/b = 1 。只要求 a/b 的最大值即可。

程式碼:

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;

int a[1000005];
int b[1000005];
int main()
{
//書寫程式碼的地方 ya
	int t;
	scanf("%d",&t);
	for (int ti = 0; ti < t; ti++)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);		//排序 
		int cnt=0;			
		//找有兩個相同長度的木棒 
		for(int i=0;i<n-1;i++)
		{
			if(a[i]==a[i+1])
			{
				b[cnt++]=a[i];
				i++;
			}
		}
		//列舉每個木棒的 a/b 的值 ,記錄最小的值 
		int x,y;
		double Min=0;
		for(int i=1;i<cnt;i++)
		{
			if((double)((double)b[i-1]/b[i])>Min)
			{
				Min=(double)((double)b[i-1]/b[i]);
				x=b[i];
				y=b[i-1];
			}
		}
		printf("%d %d %d %d\n",x,x,y,y);
	}
	return 0;
}

相關推薦

Codeforces-C. Minimum Value Rectangle

C. Minimum Value Rectangle 題目連結 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output sta

codeforces-1027 C Minimum Value Rectangle

can ++ bsp efi map end imu cout str 1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #inc

Educational Codeforces Round 49 (Rated for Div. 2)-C-Minimum Value Rectangle(貪心)

C. Minimum Value Rectangle time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard

C. Minimum Value Rectangle(基本不等式等式成立的條件)

題目連結 C. Minimum Value Rectangle time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstan

CodeForces - 1027C】Minimum Value Rectangle (數學,公式化簡,思維,卡常卡memset)

題幹: 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.

Minimum Value Rectangle(數論,思維)

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

CF1027C Minimum Value Rectangle

mini 推導 我們 倒數 這就是 ora com ont 常數 之前做的時候沒想出來...現在來數學推導一波。 題意:從n個木棒中選出4個拼成一個矩形,使得 (周長)2/面積 最小。 解:設矩形寬a長b。我們要最小化下面這個式子: 去掉常數,不妨設b = a + len

LeetCode - 939. Minimum Area Rectangle (C++)

LeetCode - 939. Minimum Area Rectangle Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, wi

939. Minimum Area Rectangle (C++)

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x

Codeforces C - String Reconstruction

pre div als log ++ codeforce tdi 父親 span C - String Reconstruction 方法一:把確定的點的父親節點設為下一個點,這樣訪問過的點的根節點都是沒訪問過的點。 代碼: #include<bits/stdc++

Codeforces C - Om Nom and Candies

end pro name color ron cin urn 復雜度 define C - Om Nom and Candies 思路:貪心+思維(或者叫數學)。假設最大值max(wr,wb)為wr,當c/wr小於√c時,可以枚舉r糖的數量(從0到c/wr),更新答案,復雜

codeforces C. Functions again

map efi ios 連續 eps cti n) space log   題意:給定了一個公式,讓你找到一對(l,r),求解出公式給定的F值。   當時沒有想到,我把(-1)^(i-l)看成(-1)^i,然後思路就完全錯了。其實這道題是個簡單的dp+最長連續子序列。   

CF910 C.Minimum Sum

sca div ret namespace spa urn emp sum == 題目鏈接:http://codeforces.com/problemset/problem/910/C 題目大意:將a~j分別賦值0~9,然後計算表達式的最小值 一道普通的模擬題,思路在於將a

Codeforces 915F Imbalance Value of a Tree(並查集)

路徑 second long long air bit force 題意 for href 題目鏈接 Imbalance Value of a Tree 題意 給定一棵樹。求樹上所有簡單路徑中的最大權值與最小權值的差值的和。 首先考慮求所有簡單路徑中的最大權值

CodeForces - 805D Minimum number of steps

mini png turn 增加 include blog 裏的 nbsp style 題目大意:把一個串裏的ab改為bba,求最少改幾次就沒有ab了 具體思路:可以把一次操作看成把a放到b右邊,再增加一個b,要求把所有的a都移到b右邊 發現1個a過一個b要1次操作 2

CF 910 C. Minimum Sum

min ios pow 不能 n) fine clas 出現的次數 amp 鏈接 [http://codeforces.com/group/1EzrFFyOc0/contest/910/problem/C] 題意 給你n個字符串,每個字符串的字符是a~j,每個字符都可以是0

codeforces C. Vasya And The Mushrooms (思維+字首+目標值最大+走格子)

題意:給定一個2*n的矩形方格,每個格子有一個權值,從(0,0)開始出發,要求遍歷完整個網格(不能重複走一個格子),求最大權值和,(權值和是按照step*w累加,step步數從0開始)。 題解:一開始我的想法是用dfs來求取最大的目標值,提交後tle,自己加了幾個剪枝也是tle,由於n最大是

CodeForces C. Maximal Intersection

http://codeforces.com/contest/1029/problem/C   You are given nn segments on a number line; each endpoint of every segment has integer c

codeforces 1072D Minimum path bfs+剪枝 好題

file space tor cas 超時 msu after oar ssi 題目傳送門 題目大意:   給出一幅n*n的字符,從1,1位置走到n,n,會得到一個字符串,你有k次機會改變某一個字符(變成a),求字典序最小的路徑。 題解:   (先吐槽一句,cf 標簽是df

codeforces 1072D Minimum path bfs+剪枝 好題

題目傳送門 題目大意:   給出一幅n*n的字元,從1,1位置走到n,n,會得到一個字串,你有k次機會改變某一個字元(變成a),求字典序最小的路徑。 題解:   (先吐槽一句,cf 標籤是dfs題????)   這道題又學到了,首先會發現,從原點出發,走x步,所有的情況都是在一條斜線上的,而再走一步