작성일 :

문제 링크

8270번 - Tulips

설명

정원에 있는 튤립 품종 번호들이 주어질 때, 전체 15,000종 중 아직 없는 품종이 몇 개인지 구하는 문제입니다.


접근법

등장한 품종 번호를 표시하고, 중복을 제외한 개수를 센 뒤 15,000에서 빼면 부족한 품종 수가 됩니다.

배열로 등장 여부를 체크하면 O(n)으로 처리할 수 있습니다.



Code

C#

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

class Program {
  static void Main() {
    var parts = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
    var idx = 0;
    var n = int.Parse(parts[idx++]);

    var seen = new bool[15001];
    var distinct = 0;
    for (var i = 0; i < n; i++) {
      var v = int.Parse(parts[idx++]);
      if (!seen[v]) {
        seen[v] = true;
        distinct++;
      }
    }

    var missing = 15000 - distinct;
    Console.WriteLine(missing);
  }
}

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
25
#include <bits/stdc++.h>
using namespace std;

typedef vector<bool> vb;

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

  int n; cin >> n;
  vb seen(15001, false);
  int distinct = 0;
  for (int i = 0; i < n; i++) {
    int v; cin >> v;
    if (!seen[v]) {
      seen[v] = true;
      distinct++;
    }
  }

  int missing = 15000 - distinct;
  cout << missing << "\n";

  return 0;
}

Tags: 8270, BOJ, C#, C++, 구현, 백준, 알고리즘

Categories: ,