We can know the result after the last sort, that is, the position of all the third-place;
So we can pre-count the number of three-way, and then for the 2,3 in the 1 position we first respectively to their respective positions, to find whether there are 1 and then exchange, if not to find another location, after processing 1, we will be 2 of the position of the number of 3, we can get the answer.
The code is as follows:
/*id:15674811lang:c++task:sort3*/#include <iostream>#include <cstdio>#include <cstring>#include <fstream>using namespace STD;voidSwpint&a,int&B) {intT=a; A=b; b=t;}intMain () {Ofstream fout ("Sort3.out"); Ifstream Fin ("Sort3.in");///ifstream fin ("lkl.txt"); intN while(fin>>n) {inta[1010],num[4];memset(Num,0,sizeof(num)); for(intI=1; i<=n;i++) {fin>>a[i]; num[a[i]]++; }intCnt=0; for(intI=1; i<=num[1];i++) {if(a[i]==2) {cnt++;intJ for(j=num[1]+1; j<=num[1]+num[2];j++)if(a[j]==1) {SWP (a[i],a[j]); Break; }if(j>num[1]+num[2]) for(intd=num[1]+num[2]+1;d <=n;d++)if(a[d]==1) {SWP (a[i],a[d]); Break; } }if(a[i]==3) {cnt++;intJ for(j=num[1]+num[2]+1; j<=n;j++)if(a[j]==1) {SWP (a[i],a[j]); Break; }if(j>n) for(intd=num[1]+1;d <=num[1]+num[2];d + +)if(a[d]==1) {SWP (a[d],a[i]); Break; } } } for(inti=num[1]+1; i<=num[1]+num[2];i++) {if(a[i]!=2) cnt++; } fout<<cnt<<endl; }return 0;}
Usaco--2.1sorting a three-valued Sequence