Description
给出n个整数a[i],问是否存在一个数ans使得a序列中任意两数和ans都能组成三角形
Input
第一行一整数n表示a序列长度,之后n个整数a[i] (2<=n<=2e5,1<=a[i]<=1e9)
Output
如果存在满足条件的ans则输出YES并输出任一解,否则输出NO
Sample Input
2
3 4
Sample Output
YES
2
Solution
先给a序列排个序,那么ans > a[i] - a[j], i > j, i,j=0,1,…,n-1,进而ans>=a[n-1]-a[0]+1,同时应用ans < a[i] + a[j],i>j, i,j=0,1,…,n-1,进而ans<=a[0]+a[1]-1,故判断一下a[n-1]-a[0]+1<=a[0]+a[1]-1是否成立即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 222222
int n,a[maxn];
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n);
int ans=a[n-1]-a[0]+1;
if(ans<a[0]+a[1])printf("YES\n%d\n",ans);
else printf("NO\n");
}
return 0;
}