C. Dominant Piranha(思维) Codeforces Round #677 (Div. 3)

原题链接: https://codeforces.com/contest/1433/problem/C

在这里插入图片描述
测试样例

input
6
5
5 3 4 4 5
3
1 1 1
5
4 4 3 4 4
5
5 5 4 3 2
3
1 1 2
5
5 4 3 5 5
output
3
-1
4
3
3
1

Note

The first test case of the example is described in the problem statement.

In the second test case of the example, there are no dominant piranhas in the aquarium.

In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes [4,4,5,4], then it can eat any other piranha in the aquarium.

题意: 在一个鱼缸中有 n n n条食人鱼,它们从 1 1 1 n n n依次编号排列,其中尺寸为 a i a_i ai,有这样的规则,若食人鱼的尺寸大于旁边的一条食人鱼的尺寸,那么这条食人鱼就可以吃了它并尺寸 + 1 +1 +1。请你找到一个优势食人鱼。(即经过一系列操作,这条食人鱼是最终活下来的食人鱼。)

解题思路: 这道题千万别被样例骗了。我们首先要知道什么时候无解,是不是当所有食人鱼尺寸都相同时无解,那么其他情况是不是都有解呢?当然是,我们总能证明可以不断使得一条尺寸最大的食人鱼逐渐吃了其他的所有食人鱼。那么我们关键要找到这一条食人鱼。 那么为了答案的正确性,即通解,我们肯定是让强者更强,试想:我们如果让原先最大尺寸的食人鱼再吃一条,它是不是要比其它的所有食人鱼都要大了?那么结果是不是就出来了?我们只要找到一条最大尺寸的食人鱼且它可以吃掉旁边的任意一只食人鱼即可。那么遍历判断即可得出答案。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 3e5+2;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
ll a[maxn];
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n;
			rep(i,1,n){
    
    
				cin>>a[i];
			}
			ll maxx=a[1];
			int ans=1;//统计是不是所有的元素值相同。
			rep(i,2,n){
    
    
				maxx=max(maxx,a[i]);
				if(a[i]==a[1])ans++;
			}
			if(ans==n){
    
    
				cout<<-1<<endl;
				continue;
			}
			//接下来遍历。
			rep(i,1,n){
    
    
				//对边缘进行判断。
				if(i==1&&a[i]==maxx&&a[i+1]<maxx){
    
    
					cout<<i<<endl;
					break;
				}
				else if(i==n&&a[i]==maxx&&a[i-1]<maxx){
    
    
					cout<<i<<endl;
					break;
				}
				else if(i!=1&&i!=n&&a[i]==maxx&&(a[i-1]<maxx||a[i+1]<maxx)){
    
    
					cout<<i<<endl;
					break;
				}
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109193200