알고리즘 풀이

[C#] 백준 18870번 문제풀이

bimtaeur30 2026. 3. 25. 13:01

문제번호: 1002

문제명: 터렛

문제링크: https://www.acmicpc.net/problem/18870

문제내용과 예제 입/출력은 위 문제 링크에서 확인해 주시기 바랍니다

 

오늘은 좌표 압축 문제를 풀었습니다. 1차원 배열에서 값들의 우선순위를 지정하고 순서대로 출력하는 문제입니다.
문제를 해결하기 위해 다음과 같이 풀이과정을 세웠습니다.

 

1. 배열 중복제거, 정렬

2. 딕셔너리 생성

3. 반복문으로 딕셔너리에 [sorted[i]] = i 를 삽입

다음은 정답코드입니다.

static void Main()
{
    using StreamReader sr = new StreamReader(Console.OpenStandardInput());
    using StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

    int[] result;

    int n = int.Parse(sr.ReadLine());
    List<int> numbers = Array.ConvertAll(sr.ReadLine().Split(), int.Parse).ToList();
    int[] sorted = numbers.Distinct().ToArray();
    Array.Sort(sorted);

    Dictionary<int, int> compress = new Dictionary<int, int>();

    for (int i = 0; i < sorted.Length; i++)
    {
        compress[sorted[i]] = i;
    }

    foreach (int i in numbers)
        sw.Write(compress[i] + " ");
}

'알고리즘 풀이' 카테고리의 다른 글

[C#] 백준 1912번 문제풀이  (0) 2026.04.02
[C#] 백준 14911번 문제풀이  (0) 2026.04.01
[C#] 백준 1002번 문제풀이  (0) 2026.03.24
[C#] 백준 9465번 문제풀이  (0) 2026.03.22
[C#] 백준 15311번 문제풀이  (0) 2026.03.21