CF EDU 99 D - Sequence and Swaps

D - Sequence and Swaps

枚举

由于最后一定是排好序的,且数据范围很小,所以可以枚举最终的序列是什么,即枚举最后的 x 是哪个数,剩下的数组成了最终的序列

求每种情况的操作次数即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 5e2 + 10;
int n, x, nowx;
int a[N], b[N], c[N];
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n >> x;
		for (int i = 1; i <= n; i++)
			cin >> a[i], b[i] = a[i];
		b[n+1] = x;
		sort(b + 1, b + n + 2);
		int ans = 1e9;
		for (int k = 1; k <= n + 1; k++)
		{
			int now = 0;
			int t = 0;
			nowx = x;
			for (int i = 1; i <= n + 1; i++)
			{
				if (i == k)
					continue;
				c[++t] = b[i];
			}
			// for (int i = 1; i <= n; i++)
				// cout << c[i] << " ";
			// cout << endl;
			for (int i = 1; i <= n; i++)
			{
				if (c[i] == a[i])
					continue;
				if (c[i] > a[i])
				{
					now = 1e9;
					break;
				}
				if (c[i] != nowx)
				{
					now = 1e9;
					break;
				}
				nowx = a[i];
				now++;
			}
			// cout << now << endl;
			ans = min(ans, now);
		}
		cout << (ans == 1e9 ? -1 : ans) << endl;
	}
	return 0;
}

posted @ 2022-05-28 21:39  hzy0227  阅读(21)  评论(0编辑  收藏  举报