summaryrefslogtreecommitdiff
path: root/2.1/sort3.c
blob: fe709bb312f20307fc96fbd0dbc672fe3ae67c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}