标签:
/*
ID: awsd1231
PROG: sort3
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, a[1010];
int main () {
freopen("sort3.in", "r", stdin);
freopen("sort3.out", "w", stdout);
scanf("%d", &n);
int c1(0);
for (int i = 0; i != n; ++i) {
scanf("%d", &a[i]);
if(a[i] == 1) ++c1;
}
int cnt(0);
for (int m = 1; m != 3; ++m) {
for (int i = n - 1; i != -1; --i) {
bool c3 = false;
if (a[i] == m) {
int t = i;
if (m == 1) t = c1;
for (int j = 0; j != t; ++j) {
if (a[j] == 3) {
int t = a[i];
a[i] = a[j];
a[j] = t;
++cnt;
c3 = true;
break;
}
}
if (!c3 && m != 2) {
for (int j = 0; j != i; ++j) {
if (a[j] == 2) {
int t = a[i];
a[i] = a[j];
a[j] = t;
++cnt;
break;
}
}
}
}
}
}
cout << cnt << endl;
return 0;
}
2.1.3 Sorting a Three-Valued Sequence
标签:
原文地址:http://www.cnblogs.com/liangyongrui/p/4545475.html