练习cf1433C. Dominant Piranha

题目如下
C. Dominant Piranha
time limit per test2 seconds
memory limit per test256 megabytes
There are 𝑛 piranhas with sizes 𝑎1,𝑎2,…,𝑎𝑛 in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.

Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.

Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely:

The piranha 𝑖 can eat the piranha 𝑖−1 if the piranha 𝑖−1 exists and 𝑎𝑖−1<𝑎𝑖.
The piranha 𝑖 can eat the piranha 𝑖+1 if the piranha 𝑖+1 exists and 𝑎𝑖+1<𝑎𝑖.
When the piranha 𝑖 eats some piranha, its size increases by one (𝑎𝑖 becomes 𝑎𝑖+1).

Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.

Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.

For example, if 𝑎=[5,3,4,4,5], then the third piranha can be dominant. Consider the sequence of its moves:

The piranha eats the second piranha and 𝑎 becomes [5,5⎯⎯,4,5] (the underlined piranha is our candidate).
The piranha eats the third piranha and 𝑎 becomes [5,6⎯⎯,5].
The piranha eats the first piranha and 𝑎 becomes [7⎯⎯,5].
The piranha eats the second piranha and 𝑎 becomes [8⎯⎯].
You have to answer 𝑡 independent test cases.

Input
The first line of the input contains one integer 𝑡 (1≤𝑡≤2⋅104) — the number of test cases. Then 𝑡 test cases follow.

The first line of the test case contains one integer 𝑛 (2≤𝑛≤3⋅105) — the number of piranhas in the aquarium. The second line of the test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109), where 𝑎𝑖 is the size of the 𝑖-th piranha.

It is guaranteed that the sum of 𝑛 does not exceed 3⋅105 (∑𝑛≤3⋅105).

Output
For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.

题目大意
大鱼吃小鱼,根据每条鱼的自带的值判断大小,但是只能从邻近的两条鱼开始吃,是否存在这样的一条鱼可以吃遍所有的鱼,每次吃完一条鱼,自增1,有的话输出索引。

题目分析
要找到这样一条鱼,可以先找值最大的鱼,最有可能吃遍剩下所有鱼。
然后,可能存在多条值最大的鱼,那么遍历所有值最大的鱼,判断是否能吃掉邻近的左右两条鱼,可以的话说明这条鱼就能吃遍剩下所有鱼,直接输出索引;否则继续遍历下一个值最大的鱼。

代码如下

点击查看代码
#include <iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        long long fish[300005];
        int max = -1, index;
        for(int i = 0; i < n; i++){
            cin >> fish[i];
            if(fish[i] > max){
                max = fish[i];
            }
        }
        bool can = false;
        for(int i = 0; i < n; i++){
            if(fish[i] == max){
                if(i > 0 && fish[i - 1] < fish[i]){
                    can = true;
                }
                if(i < n - 1 && fish[i + 1] < fish[i]){
                    can = true;
                }
                if(can){
                    index = i + 1;
                    break;
                }else{
                    index = -1;
                }
            }
        }
        cout << index << endl;
    }
    return 0;
}
注意比较时鱼的索引,若是在最两边每条鱼只能比较一边。
posted @ 2025-07-25 21:12  sirro1uta  阅读(11)  评论(0)    收藏  举报