C. Polycarp Recovers the Permutation

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题!(ง •_•)ง

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值