2024-03-21 23:34:52

첫째 줄에 반복 횟수가, 둘째 줄부터 x좌표와 y좌표가 한줄로 주어진다.

문제에서 요구한는 출력은 x좌표를 기준으로 오름차순 정렬을 한 후, y좌표를 기준으로 오름차순 정렬을 해주면 된다.

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
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();
            List<Tuple<intint>> list = new List<Tuple<intint>>();
 
            int N = int.Parse(reader.ReadLine());
 
            for (int i = 0; i < N; i++)
            {
                string[] str = reader.ReadLine().Split();
                list.Add(new Tuple<intint>(int.Parse(str[0]), int.Parse(str[1])));
            }
            list.Sort();
 
            foreach (Tuple<intint> a in list)
                sb.Append((a.Item1) + " " + (a.Item2) + "\n");
 
            print.WriteLine(sb);
 
 
        }
    }
}
cs

리스트를 튜틀타입으로 x, y좌표를 입력받아, 내장 메서드인 sort를 사용해 정렬 해준 뒤 출력했습니다.

주어진 시간이 1초라는 짧은 시간이기 때문에, 일반적인 방법으로 입/출력을 했을 때 시간 초과로 틀렸다고 떴었네요.

그래서 StringBuilder를 사용했더니 아슬아슬 하지만 통과는 했습니다.

그리고 StreamReader / StreamWriter 까지 써주니 작동 시간이 거의 절반으로 줄어들며 드라마틱한 시간 단축 효과를 얻었네요.

이정도면 단순 입출력 문제가 아닌 이상 거의 항상 사용해야 할듯.

'하루 한 접시' 카테고리의 다른 글

[백준] 2587번: 대표값2 [C#]  (0) 2024.03.21
[백준] 10817번: 세 수[C#]  (0) 2024.03.21
[백준] 11399번: ATM [C#]  (0) 2024.03.21
[백준] 1181번: 단어 정렬하기 [C#]  (0) 2024.03.20
[백준]9012번: 괄호 [C#]  (0) 2024.03.20