There are n piranhas with sizes a1,a2,…,an 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 i can eat the piranha i−1 if the piranha i−1 exists and ai−1<ai.
The piranha i can eat the piranha i+1 if the piranha i+1 exists and ai+1<ai.
When the piranha i eats some piranha, its size increases by one (ai becomes ai+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 a=[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 a becomes [5,5–,4,5] (the underlined piranha is our candidate).
The piranha eats the third piranha and a becomes [5,6–,5].
The piranha eats the first piranha and a becomes [7–,5].
The piranha eats the second piranha and a becomes [8–].
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2≤n≤3⋅105) — the number of piranhas in the aquarium. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤109), where ai is the size of the i-th piranha.
It is guaranteed that the sum of n does not exceed 3⋅105 (∑n≤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.
Example
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
题目大意是这一群鱼中,能否有一只能吃掉所有的鱼,若有,输出任意可能的一种结果。
这道题有点意思,就像脑筋急转弯一样,可以想到一种最简单的方法,如果存在一只最大的,且它比两边其中一只大,那么它一定能存活到最后。如果不存在,说明所有鱼都一样大,那么都能存活到最后。
我最开始想复杂了,用模拟跑了一遍,输出了下标最小的可能存活到最后的鱼的一种可能,结果当然是超时了。
#include <iostream>
#include <string.h>
using namespace std;
#define inf 1000000001
int n,a[300005];
bool judge(int b[],int pos)
{
int i,l,r,sum=0;
l=pos-1;
r=pos+1;
sum=b[pos];
while(l>=1||r<=n)
{
if(sum>b[l])
{
l--;
sum++;
if(l==0&&r==n+1)
break;
}
else if(sum>b[r])
{
r++;
sum++;
if(l==0&&r==n+1)
break;
}
else {
return false;
}
}
return true;
}
int main()
{
int i,t,flag=0;
cin>>t;
while(t--)
{
cin>>n;
flag=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
cin>>a[i];
}
a[0]=inf;
a[n+1]=inf;
for(i=1;i<=n;i++)
{
if(a[i]>a[i-1]||a[i]>a[i+1])
{
if(judge(a,i))
{
cout<<i<<endl;
flag=1;
break;
}
}
}
if(flag==0)
{
cout<<-1<<endl;
}
}
return 0;
}
最后成功ac的代码:
#include <iostream>
#include <string.h>
using namespace std;
#define inf 1000000001
int a[300001];
int main()
{
int i,x,y,n,t,flag=0,maxx,maxi;
cin>>t;
while(t--)
{
cin>>n;
flag=0;
maxx=-1;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]>maxx)
{
maxx=a[i];
maxi=i;
}
}
a[0]=inf;
a[n+1]=inf;
for(i=1;i<=n;i++)
{
if(a[i]==maxx&&(a[i]>a[i-1]||a[i]>a[i+1]))
{
flag=1;
cout<<i<<endl;
break;
}
}
if(flag==0)
{
cout<<-1<<endl;
}
}
return 0;
}