0%

Triangle

2019中山大学程序设计竞赛(重现赛)
虽然是签到题,但是自己没有AC…

Triangle

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description
After Xiaoteng took a math class, he learned a lot of different shapes, but Xiaoteng’s favorite triangle is because he likes stable shapes, just like his style.
After the Xiaoxun knew it, he wanted to give a triangle as a gift to Xiaoteng. He originally planned to do one, but he was too tired. So he decided to bring some wooden sticks to Xiaoteng and let Xiaoteng make a triangle himself.
One day, Xiaoxun brought n sticks to Xiaoteng. Xiaoteng wanted to try to use three of them to form a triangle, but the number is too much, Xiaoteng stunned, so he can only ask for your help.

Input
There are mutiple test cases.
Each case starts with a line containing a integer $(1≤n≤5×10^6)$ which represent the number of sticks and this is followed by n positive intergers(smaller than $2^{31}−1$)separated by spaces..

Output
YES or NO for each case mean Xiaoteng can or can’t use three of sticks to form a triangle.

Sample Input
4
1 2 3 4

Sample Output
YES

题意: 给你n个数,问是否能从给的n个数从中组成一个三角形。
题解:如果给的数成斐波那契数列,则不能构成三角形,且因为给的数是在$2^{32}-1$范围内,第50项斐波那契数为12586269025已经超过这个范围,即如果给了超过50个数,则必然能构成三角形。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<bits/stdc++.h>
using namespace std;
int n;
int vec[5000000+5];
bool fun(int a,int b,int c){
if(a<c+b)
return true;
return false;
}
bool compare(int a,int b){
return a>b;
}
int main()
{
int a;
bool flag;
while(scanf("%d",&n)!=EOF){
flag=0;
for(int i=0;i<n;i++){
scanf("%d",&vec[i]);
}
if(n<100){
sort(vec,vec+n,compare);
for(int i=0;i<n-2;i++){
flag=fun(vec[i],vec[i+1],vec[i+2]);
if(flag)
break;
}
}
else flag=1;
if(flag)
printf("YES\n");
else
printf("NO\n");

}
return 0;
}

当时做并没有想到斐波那契数列….毕竟菜…
如果不通过斐波那契数列来进行大判断会超时,然后我想换堆排序进行操作,用优先队列储存(实现了堆排序),结果卡内存…

-------------本文结束感谢您的阅读-------------