https://www.acwing.com/problem/content/1226/
首先是暴力做法,O(N^2),对于1e4勉强可以过
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10010;
int n;
int cnt;
int a[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(a[i]!=i)
{
for(int j=i+1;j<=n;j++)
if(a[j]==i)swap(a[i],a[j]);
cnt++;
}
}
cout << cnt << endl;
}
对于蒟蒻只会暴力了~~,更巧妙的思路是模拟环,推理得到结论
具体可以参考这篇题解:https://www.acwing.com/solution/content/7917/
标签:content,www,1224,int,交换,瓶子,https,include From: https://www.cnblogs.com/lxl-233/p/16826493.html