JOJ2329 Maximal

2329: Maximal


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K375126Standard
Give you some positive integers, you should tell me the max of (a + b + c) where a, b, c can make up a triangle, and in each traingle, every edge you choose can't be choosed twice or more.

Input

Multiply test case, first line of each case will be a positive integers N, which indicate the number of integers, 0 <= N <= 200000, follow the line will N positive integers. each of them will be in [0,200000].

Output

For each case output the max of (a + b + c) where a, b, c can make up a triangle, otherwise output 0.

Sample Input

5
1
2
2
3
3
1
1

Sample Output

8
0

Hint

8 = 2 + 3 + 3,this 3 is not considered twice because 3 emerges two times, (2, 2, 2) is forbidden because 2 only emerges two times ,you use it three times ,so 2 must be choosed twice or more(three times).

 

Problem Source: kscinow

 


This problem is used for contest: 54 


 

刚开始看到这道题第一感觉就是深度优先枚举,这往往也是众多悲剧发生的原因,一看是多组测试数据而且又是N<=200000,深度优先枚举肯定TLE,所以就只能另寻他法,上网搜了搜,有了想法.

 

思路:先对所有的边进行一次排序,然后从后往前一次检测连续三条边能否构成三角形,如果找到了就跳出循环,输出结果,如果一直没用找到就输出0。

为什么这么做就可以呢?我们不妨假设被我们排好序的边中连续的三条是a,b,c(从小到大,可以相等),我们知道判定三角形的一个指标是两边之和大于第三边,另一个是两边之和小于第三边,其实我们只要检测前者即可,因为前者满足了后者就一定满足(我个人认为,没有证明过),所以对于a,b,c三条边,b,c都大于等于a,所以b+c>a,又因为c>=b所以c+a>b,所以我们只需要判定a+b是否大于c就可以了。

这样子问题是不是就很简单了。

 

注:排序的算法也很讲究啊,可别告诉我你要用冒泡法,我试了一下qsort,又试了一下STL里的sort,后者快了0.14s看来以后要试着C&C++混编了。

 

Code:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值