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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话