练习cf1398A. Bad Triangle
题目如下
A. Bad Triangle
time limit per test1 second
memory limit per test256 megabytes
You are given an array 𝑎1,𝑎2,…,𝑎𝑛, which is sorted in non-decreasing order (𝑎𝑖≤𝑎𝑖+1).
Find three indices 𝑖, 𝑗, 𝑘 such that 1≤𝑖<𝑗<𝑘≤𝑛 and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to 𝑎𝑖, 𝑎𝑗 and 𝑎𝑘 (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases.
The first line of each test case contains one integer 𝑛 (3≤𝑛≤5⋅104) — the length of the array 𝑎.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109; 𝑎𝑖−1≤𝑎𝑖) — the array 𝑎.
It is guaranteed that the sum of 𝑛 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 𝑖, 𝑗, 𝑘 (𝑖<𝑗<𝑘) such that it is impossible to construct a non-degenerate triangle having sides equal to 𝑎𝑖, 𝑎𝑗 and 𝑎𝑘, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
题目大意
在现有的递增排列的数组中找出不能构成三角形的三个数并输出索引
题目分析
由于已经给出的数组是递增的,按照两边之和大于第三边构成三角形的原则,只要满足两边之和小于第三边就无法构成三角形,为了避免更多的遍历,根据递增数组,只要把第一第二项(最小的两项)的和与最后一项比较即可,最有可能构不成三角形
点击查看代码
#include <stdio.h>
int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
unsigned long long tra[500005];
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%llu", &tra[i]);
}
if(tra[0] + tra[1] <= tra[n - 1]){
printf("1 2 %d\n", n);
} else {
printf("-1\n");
}
}
return 0;
}

浙公网安备 33010602011771号