codeforces Round #677 (Div. 3) 1433C Dominant Piranha

Topic link

Insert picture description here

Title translation:

There are in the aquarium n piranhas, their size are A . 1 , A 2 , ..., A n , and numbered from left to right.
Scientists at Berland State University want to know if there are dominant fish in the tank . If a fish can eat all the fish in the tank (not including itself, of course), then the fish is said to be the dominant fish. When other fish are eaten by the dominant fish, they have no room for resistance.
Because the fish tank is extremely narrow and long, in one movement, the piranha can only eat any one of the two adjacent fish and can move as many times as necessary. More precisely:

  • Piranha i can eat piranha i-1 if piranha i-1 exists and a i-1 <a i .
  • Piranha i can eat piranha i+1 if piranha i+1 exists and a i+1 <a i .

When piranha i eats a piranha, its size increases by one (a i becomes a i +1).
Your mission is to find any one dominant fish, or there is no answer to the dominant fish.
Note that you only need to find any one dominant fish, do not need to find all the dominant fish.
For example, if a=[5,3,4,4,5], then the third fish is the dominant fish:

  • It is second only to eat fish, then it will become a [5, 5 , 4, 5] (bold is likely to be the dominant candidate fish fish)
  • After eating the third fish, a becomes [5, 6 ,5].
  • After eating the first fish, a becomes [ 7,5 ].
  • After eating the second fish, a becomes [ 8 ].

You need to answer t independent test cases.

Problem-solving ideas:

The first thing I thought was whether the biggest fish is the dominant fish? Then found that if a=[5,5,5], then there is no dominant fish. Therefore, a restriction must be added: and one of the left and right fish of the fish is smaller than the fish. In this case, this fish will immediately become the only largest fish after eating the fish on the left or right, and can eat all the remaining fish.

Code:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstdio>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 300010;
int a[N];
int main(){
    
    
//	freopen("1.txt","r",stdin);
	int t,n;
	cin>>t;
	while(t--){
    
    
		cin>>n;
		int maxn=-1;
		for(int i=0;i<n;i++){
    
    
			cin>>a[i];
			maxn=max(maxn,a[i]);
		}
		int ans=-1,flag;
		for(int i=0;i<n;i++){
    
    
			if(a[i]!=maxn) continue;
			flag=0;
			if(i-1>=0&&a[i-1]<a[i]||i+1<n&&a[i+1]<a[i]){
    
    
				flag=1;
			}
			if(flag){
    
    
				ans=i+1;
				break;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/lmmmmmmmmmmmmmmm/article/details/109220886