프로그래밍 알고리즘

[정올 2501] 모양 정돈

꾸준한사람 2023. 1. 8. 23:35
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=1762&sca=99&sfl=wr_hit&stx=2501 

 

JUNGOL

 

www.jungol.co.kr

#include <stdio.h>

int N, ShapeN[4];
int Arr[100010], SArr[6][100010], Matrix[4][4];

void SetSArr(int i, int a, int b, int c)
{
	int* pArr = SArr[i], j = 0;
	for (; j < ShapeN[a]; j++)	pArr[j] = a;
	for (; j < ShapeN[a] + ShapeN[b]; j++)	pArr[j] = b;
	for (; j < N; j++)	pArr[j] = c;
}

int AccumulateCnt(int i, int j)
{
	int a, b, cnt;
	(Matrix[i][j] > Matrix[j][i]) ? (a = i, b = j) : (a = j, b = i);
	cnt = Matrix[b][a]; Matrix[a][b] -= Matrix[b][a]; Matrix[b][a] = 0;
	return cnt;
}

int GetCnt(int Sidx)
{
	int a, b, cnt = 0, restsum = 0;

	for (int i = 1; i < 4; i++)
		for (int j = 1; j < 4; j++)		Matrix[i][j] = 0;

	for (int i = 0; i < N; i++)
	{
		a = Arr[i]; b = SArr[Sidx][i];
		Matrix[a][b]++;
	}
	
	cnt = AccumulateCnt(1, 2);
	cnt += AccumulateCnt(1, 3);
	cnt += AccumulateCnt(2, 3);

	for (int i = 1; i < 4; i++)
		for (int j = 1; j < 4; j++)
			if (i != j) restsum += Matrix[i][j];

	cnt += (restsum / 3) * 2;

	return cnt;
}

int main(void)
{
	int Cnt = 100000000;
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
	{
		scanf("%d", Arr + i);
		if (Arr[i] == 1) ShapeN[1]++;
		else if (Arr[i] == 2) ShapeN[2]++;
		else if (Arr[i] == 3) ShapeN[3]++;
	}

	SetSArr(0, 1, 2, 3);
	SetSArr(1, 1, 3, 2);
	SetSArr(2, 2, 1, 3);
	SetSArr(3, 2, 3, 1);
	SetSArr(4, 3, 1, 2);
	SetSArr(5, 3, 2, 1);

	for (int i = 0; i < 6; i++)
	{
		int mycnt = GetCnt(i);
		if (mycnt < Cnt)	Cnt = mycnt;
	}
	
	printf("%d", Cnt);
	return 0;
}
반응형

'프로그래밍 알고리즘' 카테고리의 다른 글

[정올 2518] 문자열변환  (0) 2023.01.09
[정올 2514] 문자열 찾기  (0) 2023.01.09
[정올 2499] 저울  (0) 2023.01.08
[정올 2498] 공약수  (0) 2023.01.08
[정올 2497] 수열  (0) 2023.01.08