2329: Maximal
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 375 | 126 | Standard |
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: