2024-03-25 23:56:57

 

좌표의 개수와 그 좌표들이 주어진다.

주어진 좌표들을 압축해 0부터 시작하게 만들어야 한다

.

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();
 
            int N = Convert.ToInt32(reader.ReadLine());
            int[] arr = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
 
            List<Tuple<intint>> list = new List<Tuple<intint>>();
 
            for (int i = 0; i < N; i++)
            {
                list.Add(new Tuple<intint>(arr[i], i));
            }
            list.Sort();
 
            int[] res = new int[N];
            res[list[0].Item2] = 0;
 
            int count = 0;
            for (int i = 1; i < N; i++)
            {
                if (list[i - 1].Item1 == list[i].Item1)
                    res[list[i].Item2] = count;
                else
                    res[list[i].Item2] = ++count;
            }
            
            foreach (int i in res)
            {
                sb.Append(i + " ");
            }
 
            print.WriteLine(sb);
 
        }
    }
}
cs

 

 

이번에도 2개 이상의 자료를 한번에 관리하기 위해 리스트에 튜플을 넣어서 문제를 풀어봤다.

우선적으로 좌표를 압축하기 위해 좌표들을 정렬해줄 필요가 있었다.

이후 원래의 인덱스 번호 (list[i].Item2)를 이용해, 새로운 배열에 값을 넣어주었다.

값을 넣는 규칙은 0부터 시작하여, 중복되는 경우 대입하는 값의 크기를 증가시키지 않고, 다를 경우에만 값을 증가시켰다.