ARC121C
Sol
机房大佬 @wmrqwq 讲了这题,感觉实现很巧妙,遂来写篇题解。
不难发现 \(n\le 3\) 一定有解,这对后面做法很有帮助。
本人的思路是把 \(1\sim 3\) 移到前三个,然后后面可以冒泡排序,对于奇偶不符的可以在前面消耗一次操作。
但是这种做法有个瑕疵:就是 \(n\) 较小的时候不方便实现。
注意到从 \(n\) 到 \(4\) 每一个按序移到答案的位置一定同样满足条件。考虑如何处理奇偶不符的问题:直接在 \(1\sim 4\) 中选择一个下标交换,不难发现一定能找到一个下标满足条件。
更多细节可以看看代码,感谢 @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;
}

浙公网安备 33010602011771号