ARC121C

Sol

机房大佬 @wmrqwq 讲了这题,感觉实现很巧妙,遂来写篇题解。

不难发现 n3 一定有解,这对后面做法很有帮助。

本人的思路是把 13 移到前三个,然后后面可以冒泡排序,对于奇偶不符的可以在前面消耗一次操作。

但是这种做法有个瑕疵:就是 n 较小的时候不方便实现。

注意到从 n4 每一个按序移到答案的位置一定同样满足条件。考虑如何处理奇偶不符的问题:直接在 14 中选择一个下标交换,不难发现一定能找到一个下标满足条件。

更多细节可以看看代码,感谢 @wmrqwq 对本人写代码的帮助!

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define pf push_front
#define desktop "C:\\Users\\incra\\Desktop\\"
#define IOS ios :: sync_with_stdio (false),cin.tie (0),cout.tie (0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
template <typename T1,typename T2> bool tomax (T1 &x,T2 y) {
	if (y > x) return x = y,true;
	return false;
}
template <typename T1,typename T2> bool tomin (T1 &x,T2 y) {
	if (y < x) return x = y,true;
	return false;
}
LL power (LL a,LL b,LL p) {
	LL ans = 1;
	while (b) {
		if (b & 1) ans = ans * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return ans;
}
int fastio = (IOS,0);
#define endl '\n'
#define puts(s) cout << (s) << endl
const int N = 510;
int n;
int a[N],b[N];
vector <int> ans;
void opt (int x) {
	swap (a[x],a[x + 1]);
	swap (b[a[x]],b[a[x + 1]]);
	ans.pb (x);
}
void solve (int tmp) {
	while (1) {
		bool f = 1;
		for (int i = 1;i <= 3;i++) f &= a[i] == i;
		if (f) break;
		if (!tmp) opt (2);
		else opt (1);
		tmp ^= 1;
	}
}
void mian () {
	ans.clear ();
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i],b[a[i]] = i;
	if (n == 1) {
		puts ("0");
		return ;
	}
	if (n == 2) {
		if (a[1] > a[2]) puts ("1\n1");
		else puts ("0");
		return ;
	}
	if (n == 3) {
		solve (1);
		cout << ans.size () << endl;
		for (int i : ans) cout << i << ' ';
		cout << endl;
		return ;
	}
	int tmp = 1;
	for (int i = n;i >= 4;i--) {
		if (b[i] % 2 == tmp) {
			for (int j = b[i];j <= i - 1;j++) opt (j),tmp ^= 1;
			continue;
		}
		if (tmp) {
			if (a[1] != i && a[2] != i) opt (1);
			else opt (3);
		}
		else {
			if (a[2] != i && a[3] != i) opt (2);
			else opt (4);
		}
		tmp ^= 1;
		for (int j = b[i];j <= i - 1;j++) opt (j),tmp ^= 1;
	}
	solve (tmp);
	cout << ans.size () << endl;
	for (int i : ans) cout << i << ' ';
	cout << endl;
}
int main () {
	int T = 1;
	cin >> T;
	while (T--) mian ();
	return 0;
}
posted @   incra  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
历史上的今天:
2023-03-17 HDU 6514 Monitor
点击右上角即可分享
微信分享提示