Triangle
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7179 Accepted Submission(s): 856
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 n(1≤n≤5×106) which represent the number of sticks and this is followed by n positive intergers(smaller than 231−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条边是否满足是一个三角形。
分析:
官方题解:
比赛的时候写了一个大根堆水过。
正解
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=5000010;
int a[maxn];
int main(){
int N;
while(~scanf("%d",&N)){
rep(i,1,N) scanf("%d", &a[i]);
bool F=0;
if(N<100){
sort(a+1,a+N+1);
rep(i,1,N-2){
if(a[i]+a[i+1]>a[i+2]){ F=1; break; }
}
}
else F=true;
puts(F?"YES":"NO");
}
return 0;
}
<
水解:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 5000000+5;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
using namespace std;
int main() {
int n;
while(scanf("%d",&n)!=EOF){
priority_queue<LL> Q;
bool flag=false;
for(int i=1;i<=n;i++){
LL x;
scanf("%lld",&x);
if(!flag)
Q.push(x);
if(Q.size()>3&&!flag){
LL a=Q.top();Q.pop();
LL b=Q.top();Q.pop();
LL c=Q.top();
if(a<b+c)
flag=true;
Q.push(a);
Q.push(b);
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}