Codeforces Round #655 (Div. 2) C. Omkar and Baseball

C. Omkar and Baseball

题目链接-C. Omkar and Baseball
Codeforces Round #655 (Div. 2) C. Omkar and Baseball_第1张图片
Codeforces Round #655 (Div. 2) C. Omkar and Baseball_第2张图片
题目大意
给定一种操作可以使得某区间内所有数字任意排列,但是要满足排列后该区间所有数字都不能在自己原来的位置上,问最少需要多少次操作可以将原本的序列变为递增排列

解题思路

  • 我们可以找位置不对的区间有多少个:
  1. 如果有 0 0 0个,显然该序列本身就是升序的,不需要操作
  2. 如果只有 1 1 1个,那么直接一次操作就行
  3. 如果位置不对的区间大于 1 1 1个,我们可以通过一次操作先把中间正确位置的元素换换位置,这样就把序列转化为只有一个位置不对区间的序列,然后再经过一次操作就能排列成升序数列
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int a[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int ass=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i-1]!=i-1&&a[i]==i)
				ass++;
		}
		if(a[n]!=n) ass++;
		if(ass==0)
			cout<<0<<endl;
		else{
			if(ass==1) cout<<1<<endl;
			else cout<<2<<endl;
		} 
	}
	return 0;
}

你可能感兴趣的:(codeforces)