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程式碼
/*
*郵箱:unique_powerhouse@qq.com
*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;
}
相關文章
- C. Lose it!(思維)Codeforces Round #565 (Div. 3)
- Codeforces Round #688 (Div. 2) C. Triangles(思維,數學)
- acm-(思維、奇偶性、矩陣)Codeforces Round #682 (Div. 2) C. Engineer ArtemACM矩陣
- Codeforces Round #446 (Div. 2) C. PrideIDE
- Codeforces Round #242 (Div. 2) C. Magic FormulasORM
- Codeforces Round #215 (Div. 2) C. Sereja and AlgorithmGo
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- Codeforces Round #228 (Div. 1) C. Fox and Card GameGAM
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Arraydev
- Codeforces Round #690 (Div. 3)
- Codeforces Round 943 (Div. 3)
- Codeforces Round 962(Div. 3)
- Codeforces Round 962 (Div. 3)
- Codeforces Round 957 (Div. 3)
- Codeforces Round 950 (Div. 3)
- Codeforces Round 954 (Div. 3)
- Codeforces Round 966 (Div. 3)
- Codeforces Round 946 (Div. 3)
- Codeforces Round 974 (Div. 3)
- Codeforces Round 981 (Div. 3)
- 【CodeForces】 Codeforces Round #672 (Div. 2) B.Rock and Lever (思維&位運算)
- Codeforces Round 944 (Div. 4) G(思維 + 位運算性質)
- Codeforces C. Colored Rooks 構造 (Codeforces Round #518 (Div. 2) )
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- Codeforces Round 933 (Div. 3)
- Codeforces Round 938 (Div. 3) E
- Codeforces Round 916 (Div. 3)
- Codeforces Round 966 (Div. 3) VP
- Codeforces Round 970 (Div. 3)A~F
- Codeforces Round 981 (Div. 3)(A~E)
- Codeforces Round #373 (Div. 1) C. Sasha and Array 線段樹
- Codeforces Round #713 (Div. 3)AB題
- Codeforces Round 962 (Div. 3) 題解
- Codeforces Round 946 (Div. 3) 題解
- Codeforces Round 970 (Div. 3) ABCDEFGH
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水題
- Codeforces Round #615 (Div. 3) (題解)