TJ
2026-08-12 21:14:17
发布于:陕西
1阅读
0回复
0点赞
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> h(n);
vector<int> sorted_h(n);
//读入数据
for(int i = 0; i < n; i++){
cin >> h[i];
sorted_h[i] = h[i];
}
sort(sorted_h.begin(), sorted_h.end());
//计算得到正确序列
int m = 0;
for(int i = 0; i < n; i++){
if(h[i] != sorted_h[i]){
for(int j = i + 1; j < n; j++){
if(h[j] == sorted_h[i] && h[j] != sorted_h[j]){
swap(h[i], h[j]);
m++;
break;
}
}
}
}
cout << m << '\n';
return 0;
}
这里空空如也







有帮助,赞一个