Codeforces Round #740 E. Bottom-Tier Reversals

题意:给一个长度为奇数的排列,每次可以选一个长度为奇数的前缀进行翻转,要求在2.5n次操作内使得排列递增

长度为奇数的前缀翻转后,元素所处位置的奇偶性不会变

考虑到最后的排列为1,2,3,...

故要有( m a[i]equiv i pmod 2)​恒成立,可以判掉无解的情况

然后考虑每次将最大的两个数放到序列末尾,递归解决n-2规模的问题

#include<bits/stdc++.h>

using namespace std;

int rd(){
	int ret=0,f=1;char c;
	while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
	while(isdigit(c))ret=ret*10+c-'0',c=getchar();
	return ret*f;
}

const int MAXN = 2042;

int n;

int a[MAXN];

vector<int> ans;
void pans(int x){
	if(x==1) return;
	reverse(a+1,a+1+x);
	if(ans.size()&&ans[ans.size()-1]==x){
		ans.pop_back();
		return;
	}
	ans.push_back(x);
}

void solve(){
	ans.clear();
	n=rd();
	for(int i=1;i<=n;i++) a[i]=rd();
	for(int i=1;i<=n;i++){
		if(a[i]%2!=i%2) {puts("-1");return;}
	}
	for(int tar=n;tar>=3;tar-=2){
		int x=1,y=1;
		while(a[x]!=tar) x++;
		pans(x);
		while(a[y]!=tar-1) y++;
		pans(y-1);
		pans(y+1);
		pans(3);
		pans(tar);
	}
	cout<<ans.size()<<endl;
	for(auto it:ans) cout<<it<<" ";
	if(ans.size()) cout<<endl;
}

int main(){
	int T=rd();
	while(T--) solve();	
}

本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/15186186.html

原文地址:https://www.cnblogs.com/ghostcai/p/15186186.html