Usaco--2.1sorting a three-valued Sequence

Source: Internet
Author: User

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&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.