summaryrefslogtreecommitdiff
path: root/2.1/sort3.c
diff options
context:
space:
mode:
Diffstat (limited to '2.1/sort3.c')
-rw-r--r--2.1/sort3.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/2.1/sort3.c b/2.1/sort3.c
new file mode 100644
index 0000000..fe709bb
--- /dev/null
+++ b/2.1/sort3.c
@@ -0,0 +1,55 @@
+/*
+ID: mytbk921
+LANG: C
+TASK: sort3
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
+
+static inline int min(int a, int b) { return a<b?a:b; }
+
+int main()
+{
+ FILE *fin = fopen("sort3.in", "r");
+ FILE *fout = fopen("sort3.out", "w");
+ int N;
+ int a[1000];
+ int count[3];
+ int mat[3][3];
+ int i;
+ int nswaps;
+
+ memset(count, 0, sizeof(count));
+ memset(mat, 0, sizeof(mat));
+
+ fscanf(fin, "%d", &N);
+ for (i=0; i<N; i++)
+ fscanf(fin, "%d", &a[i]);
+ fclose(fin);
+
+ for (i=0; i<N; i++) {
+ a[i]--;
+ count[a[i]]++;
+ }
+
+ for (i=0; i<count[0]; i++)
+ mat[a[i]][0]++;
+
+ for (; i<count[0]+count[1]; i++)
+ mat[a[i]][1]++;
+
+ for (; i<N; i++)
+ mat[a[i]][2]++;
+
+ nswaps = min(mat[0][1], mat[1][0]) + min(mat[0][2], mat[2][0]) +
+ min(mat[1][2], mat[2][1]) +
+ (abs(mat[0][1]-mat[1][0]) +
+ abs(mat[0][2]-mat[2][0]) +
+ abs(mat[1][2]-mat[2][1])) / 3 * 2;
+ fprintf(fout, "%d\n", nswaps);
+ fclose(fout);
+ return 0;
+}