/* ID: lucien23prog: sort3lang: C ++ */# include <iostream> # include <fstream> # include <vector> # include <algorithm> using namespace STD; void Exchange (INT Nums [], int begin, int end, int N, int X); int sum = 0; int main () {ifstream infile ("sort3.in "); ofstream OUTFILE ("sort3.out"); If (! Infile |! OUTFILE) {cout <"file operation failure! "<Endl; Return-1;} int N; infile> N; int * Nums = new int [N]; int count1, count2, count3; count1 = count2 = count3 = 0; For (INT I = 0; I <n; I ++) {infile> Nums [I]; Switch (Nums [I]) {Case 1: count1 ++; break; Case 2: count2 ++; break; Case 3: count3 ++; break; default: Break ;}} Exchange (Nums, 0, count1, N, 1); Exchange (Nums, count1, count1 + count2, N, 2); OUTFILE <sum <Endl; return 0 ;} /** swap the large one to the back and the small one to the front */void ex Change (INT Nums [], int begin, int end, int N, int X) {int count0 = 0; vector <int> vecnum; For (INT I = begin; I <end; I ++) {If (Nums [I]! = X) {count0 ++; vecnum. push_back (Nums [I]); Nums [I] = x ;}} sum + = count0; sort (vecnum. begin (), vecnum. end (); For (INT I = end, j = 0; I <n & count0> 0; I ++) {If (Nums [I] = 1) {Nums [I] = vecnum [J]; j ++; count0 --;}}}
Usaco section 2.1 sorting a three-Valued Sequence