题解
2025-08-06 09:21:42
发布于:上海
12阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int a[200005];
int b[5];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
b[a[i]] ++;
}
int x = b[1] + 1, y = n;
int cnt = 0;
for (int i = 1; i <= b[1]; i ++) {
if (a[i] == 2) {
while (a[x] != 1) {
x ++;
}
swap(a[i], a[x]);
cnt ++;
}
else if (a[i] == 3) {
while (a[y] != 1) {
y --;
}
swap(a[i], a[y]);
cnt ++;
}
}
for (int i = b[1] + 1; i <= b[1] + b[2]; i ++) {
if (a[i] == 3) {
cnt ++;
}
}
cout << cnt << endl;
return 0;
}
这里空空如也
有帮助,赞一个