Codeforces Round #756 (Div. 3)
C. Polycarp Recovers the Permutation
题意:给你一个数组P然后让你判断这个数组最小的一位是前面还是后面所以可以理解成P[0]最前面还有P[n-1]最后面的,最后多加几个判断就可以了,但是注意题目最后说了如果有多个答案输出任意一个…这一点卡了我半个小时 最后整体输出就行了,还有几个边界😄
最后就是代码啦!
也没啥难的,就是细节很多,还有C题带的光环吓人/(ㄒoㄒ)/~~
#include<bits/stdc++.h>
using namespace std;
int p[200005],t,n,spot,maxx,ans[200005];
int main() {
cin>>t;
while (t--) {
maxx=0;
cin>>n;
for (int i=0;i<n;i++){
cin>>p[i];
maxx=max(p[i],maxx);
}
//where to start from
//if the left is min and the right is max
if (p[n-1]>p[0]&&p[n-1]==maxx) spot=n-1;
//start from the end
//if the right is min and the left is max
else if (p[n-1]<p[0]&&p[0]==maxx) spot=0;
//or start from the start
else if (n==1){//special time
cout<<1<<endl;
continue;
}
else {//negtive one if you can't decide
cout<<-1<<endl;
continue;
}
if(spot==n-1){//if it starts from the end
ans[n-1]=maxx;//start it from the end and proceed to the start
for (int i=0;i<n-1;i++){
ans[n-2-i]=p[i];//start from the end and ans[n-1] is already given
}
}
if(spot==0){//if it starts from the start
ans[n-1]=maxx;//ans[n-1] is still given
for(int i=0;i<n-1;i++){
ans[n-2-i] = p[i+1];//same thing just that because p[0] was already used it needs to all plus one
}
}
for(int i=0;i<n;i++){
cout<<ans[i]<<' ';
}//output
cout<<endl;
}
return 0;
}
有问题问我哦!说好更新我就更新,明天来更新D题!(ง •_•)ง