如果将1和3都放到正确的位置,2自然也在正确的位置。那么统计1,2,3的数量num1,num2,num3。再看前num1个数有几个(设x个)不是1,那么x个1肯定要移。设前num1个数有y个3,最后num3个数有z个1,那么这个过程中,最多能将min(y,z)个3移到正确位置。剩下num3-min(y,z)个3也必须移到最后num3个位置
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF;
int arr[SZ]; int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//for(lon time=1;time<=casenum;++time)
{
int n;
cin>>n;
for(int i=;i<=n;++i)cin>>arr[i];
int num1=count(arr+,arr++n,);
int num2=count(arr+,arr++n,);
int num3=count(arr+,arr++n,);
int x=,y=,z=,w=;
for(int i=;i<=num1;++i)
{
if(arr[i]!=)++x;
if(arr[i]==)++z;
}
for(int i=num1+num2+;i<=n;++i)
{
if(arr[i]!=)++y;
if(arr[i]==)++w;
}
//cout<<num1<<" "<<num2<<" "<<num3<<endl;
//cout<<x<<" "<<y<<" "<<z<<endl;
cout<<(x+y-min(z,w))<<endl;
}
return ;
}