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

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

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

题目大意:

    t组样例,给我们n个长度的火柴棍,让我们建立一个矩形,使得矩形的周长的平方除以面积的值最小,输出需要的火柴棍的长度。

   划重点1:题目保证有解。

   划重点2:这t个样例里的n加起来不超过1e6。

解题报告:

     首先化简公式,根据对号函数我们知道a和b相邻的取值一定是可以取得极小值。于是乎先存下所有合法解,然后遍历这些解,维护最小值。

 

标解代码:(374ms)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<stack>
#include<queue>
#define eps 1e-7
#define N 1000010
using namespace std;
typedef long long ll;
int a[N],b[N];
int main () {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);map<int,int>mp;int ans=0;for(int i=1; i<=n; i++) {scanf("%d",a+i);mp[a[i]]++;if(mp[a[i]]==4) ans=a[i];}if(ans != 0) {printf("%d %d %d %d\n",ans,ans,ans,ans);continue;}sort(a+1,a+1+n);n=unique(a+1,a+1+n)-a-1;int cnt=0;for(int i=1; i<=n; i++) {if(mp[a[i]]>1)b[++cnt]=a[i];}int ans1,ans2;double temp=9999999999;for(int i=2; i<=cnt; i++) {if(((double)b[i]/b[i-1])+((double)b[i-1]/b[i])<temp) {temp=((double)b[i]/b[i-1])+((double)b[i-1]/b[i]);ans1=b[i-1],ans2=b[i];}}printf("%d %d %d %d\n",ans1,ans1,ans2,ans2);}
}

其中,map的作用,也可以都存到一个cnt数组中,可以做到o(1)查询,不过这里无伤大雅了,没有卡这里。

AC代码:(1981ms)

emmm这个代码就比较坑了,刚开始写的时候cnt[100005],然后TLE5了。。。看了一眼样例,是t=166666,n=4,所以会卡memset啊!本来10005就够了,,这件事情告诉我们,数组不能乱开。不过还好修改了之后复杂度o(1e9)限过。。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,qq1,qq2;
int cnt[10005] = {0};
int maxx = -1,ans = -1,cur = 1,tmp,ans1,ans2,minn = 0x3f3f3f3f;int main()
{int t;scanf("%d",&t);while(t--) {scanf("%d",&n);//n=read();memset(cnt,0,sizeof cnt);maxx = -1,ans = -1,cur = 1,tmp,ans1,ans2,minn = 0x3f3f3f3f;double get = 0x3f3f3f3f3f3f3f3f;for(int i = 1; i<=n; i++) {scanf("%d",&tmp);cnt[tmp]++,maxx = max(maxx,tmp),minn = min(minn,tmp);if(cnt[tmp] == 4) {ans = tmp;}}if(ans != -1) {printf("%d %d %d %d\n",ans,ans,ans,ans);continue;}cur = minn;while(1) {if(cur > maxx) break;if(cnt[cur] >= 2) {qq1=cur;cur++;while(1) {if(cur > maxx) break;if(cnt[cur] >= 2) {qq2 = cur;if(4.0*(qq1+qq2)*(qq1+qq2) / (1.0*qq1*qq2) < get) {ans1 = qq1,ans2 = qq2;get = 4.0*(qq1+qq2)*(qq1+qq2) / (1.0*qq1*qq2);}cur--;break;}cur++;}}cur++;}printf("%d %d %d %d\n",ans1,ans1,ans2,ans2);}return 0 ;
}

总结:

    在最里面的那个if中,别忘了更新的时候  也要更新get啊!!!这个地方落下了至少两次了吧??

    对于语法习惯:还是while(1)好用啊!逻辑结构也算很复杂了,终于是没有调试很长时间、、别忘那里cur--就好了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441449.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql数据库业务逻辑_Mysql业务设计(逻辑设计)

逻辑设计数据库设计三大范式数据库设计第一大范式数据库表中所有的字段都只具有单一属性单一属性的列是由基本数据类型所构成设计出来的表都是简单的二维表数据库设计的第二大范式要求表中只有一个业务主键&#xff0c;也就是说符合第二范式的表不能存在非主键列&#xff0c;只…

lua进入压缩包_使用lua语言制作贪吃蛇游戏(love2d)(一)开发环境的搭建

本教程教大家使用lua制作一个贪吃蛇&#xff0c;游戏引擎使用love2d&#xff0c;因为它开源轻巧而且跨平台。1.开发环境搭建&#xff1a;windows系统&#xff1a;在windows系统下&#xff0c;首先我们进入官网www.love2d.org。love2d官网进入官网可以看到Download选项&#xff…

mysql rand() 子查询_MySQL ------ 子查询(十三)

查询&#xff08;query&#xff09;:任何SQL 都是查询&#xff0c;但此术语一般指select 语句子查询&#xff08;subquery&#xff09;:嵌套在查询中的查询&#xff0c;MySQL4.1 引入对子查询的支持。接下来得就比较有意思了&#xff0c;需要你对于表与表之间的关系有所了解&am…

centos 6.5 apache mysql php_CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

简单点的&#xff1a;1.关闭SELINUX&#xff1a;setenfo 0 暂时关闭2.安装Apache&#xff1a;yum install httpd3.安装MySQL&#xff1a;yum install mysql mysql-server4.安装PHP&#xff1a;yum install php5.相关的配置&#xff1a;PHP关联MySQL&#xff1b;httpd出错信息…

mysql 如何调用函数结果_MySQL自定义函数调用不出结果

自定义函数的代码&#xff1a;DROP FUNCTION IF EXISTS fn_HrStaffBase_GetNameFromidCarddelimiter //CREATE FUNCTION fn_HrStaffBase_GetNameFromidCard (a VARCHAR(30))RETURNS VARCHAR(50)beginreturn (SELECT staff_name FROM hr_staff_base where idCard a);END //--…

【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)

题干&#xff1a; It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th of th…

git object 很大_这才是真正的Git——Git内部原理

本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码和变更历史的、更改一个文件时&#xff0c;Git内部是怎么变化的、Git这样实现的好处等等。TL;DR本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码…

【CodeForces - 195D】Analyzing Polyline (思维,卡精度的处理方式)

题干&#xff1a; As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son …

【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)

题干&#xff1a; You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbe…

scrapy 分布式 mysql_Scrapy基于scrapy_redis实现分布式爬虫部署的示例

准备工作1.安装scrapy_redis包,打开cmd工具,执行命令pip install scrapy_redis2.准备好一个没有BUG,没有报错的爬虫项目3.准备好redis主服务器还有跟程序相关的mysql数据库前提mysql数据库要打开允许远程连接,因为mysql安装后root用户默认只允许本地连接,详情请看此文章部署过程…

(精)DEVC++的几个实用小技巧

依赖 DEV C 5.11 最新版 下载安装DEV C后&#xff0c;使用DEV C打开一个随便的cpp文件&#xff0c;你看到的应该是这样的界面。&#xff08;为了节约读者的流量&#xff0c;图片进行了有损压缩&#xff0c;但是字看得清楚&#xff09; 重点是确认工具栏有AStyle选项。 相信…

win10一按右键就闪屏_升级Win10正式版后屏幕一直闪烁正确的解决办法

Win10正式版屏幕一直闪烁怎么办呢&#xff1f;升级到Win10正式版并进入Windows桌面后&#xff0c;发现屏幕一直不断的闪烁&#xff0c;此时无法执行任务操作。小编最近在升级到Win10正式版后才遇到了这个问题&#xff0c;后台经过反复思考和探索&#xff0c;终于解决了问题&…

*【CodeForces - 195B】After Training (多解,模拟)

题干&#xff1a; After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from l…

pandas打印全部列_python——pandas练习题1-5

练习1-开始了解你的数据探索Chipotle快餐数据相应数据集&#xff1a;chipotle.tsvimport pandas as pd chipopd.read_csv("exercise_data/chipotle.tsv",sept) chipo.head(5)chipo.shape[0] #查看有多少行4622chipo.shape[1] #查看有多少列5chipo.columns #打印所…

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

题干&#xff1a; Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered from 1 to n. Mike starts walking from his house located a…

java utf-8 gbk_Java 字符转码之UTF-8转为GBK/GB2312

java跟python类似的做法&#xff0c;在java中字符串的编码是java修改过的一种Unicode编码&#xff0c;所以看到java中的字符串&#xff0c;心理要默念这个东西是java修改过的一种Unicode编码的编码。packagestring;importjava.nio.charset.Charset;public classUTF82GBK {publi…

java好的博客_推荐5个万博爆款Java开源博客,是我目前用过最好用的博客系统

1.OneBlog一个简洁美观、功能强大并且自适应的Java博客&#xff0c;使用springboot开发&#xff0c;前端使用Bootstrap&#xff0c;支持移动端自适应&#xff0c;配有完备的前台和后台管理功能。功能简介多种编辑器、自动申请友情链接、百度推送、评论系统、权限管理、SEO、实时…

中介者模式java_图解Java设计模式之中介者模式

智能家庭项目1)智能家庭包括各种设备&#xff0c;闹钟、咖啡机、电视机、窗帘等2)主人要看电视时&#xff0c;各个设备可以协同工作&#xff0c;自动完成看电视的准备工作&#xff0c;比如流程为 &#xff1a;闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放…

【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)

题干&#xff1a; Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing …

java servlet 转发和重定向_JavaWeb(一)Servlet中乱码解决与转发和重定向的区别

前言前面其实已经把Servlet中所有的内容都介绍完了&#xff0c;这篇讲补充一点乱码和重定向与转发之间的区别&#xff01;一、request请求参数出现乱码问题1.1、get请求1)乱码示例get请求的参数是在url后面提交过来的&#xff0c;也就是在请求行中。结果&#xff1a;Servlet_de…