A. Bad Triangle

传送门

A. Bad Triangle

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a1,a2,…,an, which is sorted in non-decreasing order (ai≤ai+1)Find three indices i, j, k such that 1≤i

Input
The first line contains one integer t(1≤t≤1000) — the number of test cases.
The first line of each test case contains one integer n(3≤n≤5⋅104) — the length of the array a The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109; ai−1≤ai) — the array a It is guaranteed that the sum of n over all test cases does not exceed 105

Output
For each test case print the answer to it in one line.If there is a triple of indices i, j, k (i Otherwise, print -1.

Example
Input

3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000

Output

2 3 6
-1
1 2 3

Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.

题意:判断能否构成三角形,能则输出-1,不能则输出任意三组不能构成的数字的下标。

思路:用特征值判断,因为是按升序输入,所以判断两组特解即可,可以是1,2,n;或者1,n-1,n;判断条件(a+b<=c||a+c<=b||c+b<=a);
或者只判断输出-1的情况,满足(a+b>c),其他情况均输出1,2,n;

AC代码
1

#include
#include
#include
#include
const int maxn=100000;
using namespace std;
int main()
{
    long long  t,n;
   long long a[maxn];
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
               cin>>a[i];
            if(a[1]+a[2]<=a[n]||a[2]+a[n]<=a[1]||a[n]+a[1]<=a[2])
               {
                   cout<<1<<' '<<2<<' '<<n<<endl;}
            else if(a[1]+a[n-1]<=a[n]||a[1]+a[n]<=a[n-1]||a[n-1]+a[n]<=a[1])
                {cout<<1<<' '<<n-1<<' '<<n<<endl;}
            else{cout<<-1<<endl;}
    }return 0;
}

AC代码
2

#include 
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll a[maxn];
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
		int n;
		scanf("%d",&n);
		for(int i= 1;i<=n;++i)
        scanf("%lld",&a[i]);
        /*cin>>a[i]*/
		if(a[1]+a[2]>a[n])
        cout<<-1<<endl;
		else
		cout<<1<<' '<<2<<' '<<n<<endl;
    }
    return 0;
}

你可能感兴趣的:(codeforces,算法)