작성일 :

문제 링크

5176번 - 대회 자리

설명

각 참가자가 앉으려는 자리가 이미 다른 사람이 앉아 있는지 여부를 체크하여,

자리를 겹쳐 앉는 시도를 몇 번 했는지 구하는 문제입니다.

  • 입력으로는 참가자 수좌석 수, 그리고 각 참가자가 원하는 좌석 번호가 주어집니다.
  • 좌석 번호는 1번부터 시작하며, 이미 누군가가 앉아 있다면 해당 참가자는 앉지 못합니다.
  • 이때 앉지 못한 참가자의 수를 출력하면 됩니다.

접근법

  • 좌석 수만큼의 크기를 가지는 bool 배열을 생성하여 해당 좌석이 사용 중인지 여부를 기록합니다.
  • 참가자의 좌석 요청을 순서대로 처리하면서 이미 사용된 좌석이라면 카운트를 증가시킵니다.
  • 최종적으로 겹쳐 앉으려 한 횟수를 출력합니다.

Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;

class Program {
  static void Main() {
    int t = int.Parse(Console.ReadLine());
    while (t-- > 0) {
      var parts = Console.ReadLine().Split();
      int cntP = int.Parse(parts[0]);
      int cntS = int.Parse(parts[1]);

      var isDup = new bool[cntS + 1];
      int cntN = 0;

      for (int i = 0; i < cntP; i++) {
        int idx = int.Parse(Console.ReadLine());
        if (isDup[idx]) cntN++;
        else isDup[idx] = true;
      }

      Console.WriteLine(cntN);
    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>

using namespace std;
typedef vector<bool> vb;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    int cntP, cntS; cin >> cntP >> cntS;
    vb isDup(cntS + 1, false);
    int cntN = 0;
    for (int i = 0; i < cntP; i++) {
      int idx; cin >> idx;
      if (isDup[idx]) cntN++;
      else isDup[idx] = true;
    }
    cout << cntN << "\n";
  }

  return 0;
}