Baekjoon 10816.숫자 카드 2 c++ [Silver IV]

2024. 12. 30. 17:39·Baekjoon/Silver
728x90
반응형

unordered_map

메모리 시간
43828KB 488ms
#include <iostream>
#include <unordered_map>
#include <algorithm>

using namespace std;

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

    int N;
    cin >> N;
    unordered_map<int, int> numbers;

    while (N--)
    {
        int i;
        cin >> i;
        numbers[i]++;
    }
    int M;
    cin >> M;
    while (M--)
    {
        int check;
        cin >> check;
        cout << numbers[check] << " ";
    }
    return 0;
}

 

이진탐색

메모리 시간
5936KB 288ms

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

    int N, M;
    cin >> N;

    vector<int> cards(N);
    for (int i = 0; i < N; ++i)
    {
        cin >> cards[i];
    }

    sort(cards.begin(), cards.end());

    cin >> M;
    vector<int> queries(M);
    for (int i = 0; i < M; ++i)
    {
        cin >> queries[i];
    }

    for (int query : queries)
    {
        auto lower = lower_bound(cards.begin(), cards.end(), query);
        auto upper = upper_bound(cards.begin(), cards.end(), query);

        cout << (upper - lower) << " ";
    }

    return 0;
}

 

lower_bound는 10이 처음 등장하는 위치 찾기 -> 정렬된 배열에서 10은 인덱스 7에 처음 등장 -> 즉, lower_bound는 cards[7]을 가리킨다. -> upper_bound는 10보다 큰 값이 처음 등장하는 위치를 찾고 -> 정렬된 배열에서 10보다 큰 값은 없음 -> 따라서 upper_bound는 인덱스 10 (배열의 끝)을 가리킨다. upper - lower = 10 - 7 = 3

728x90
반응형
저작자표시 (새창열림)

'Baekjoon > Silver' 카테고리의 다른 글

Baekjoon 15650.N과 M (2) c++ [Silver III]  (1) 2025.01.09
Baekjoon 10845.큐 c++ [Silver IV]  (0) 2024.12.30
Baekjoon 1436.영화감독 슘 c++ [Silver V]  (0) 2024.12.28
Baekjoon 17219.비밀번호 찾기 c++ [Silver IV]  (1) 2024.12.23
Baekjoon 11399.ATM c++[Silver IV]  (0) 2024.12.22
'Baekjoon/Silver' 카테고리의 다른 글
  • Baekjoon 15650.N과 M (2) c++ [Silver III]
  • Baekjoon 10845.큐 c++ [Silver IV]
  • Baekjoon 1436.영화감독 슘 c++ [Silver V]
  • Baekjoon 17219.비밀번호 찾기 c++ [Silver IV]
Hun-bot
Hun-bot
IT를 중심으로 다양한 것
  • Hun-bot
    로봇이 만드는 눈사람
    Hun-bot
  • 전체
    오늘
    어제
    • All Article (128)
      • Programmers (6)
        • TIP (1)
        • SQL (2)
        • LV1 (1)
        • LV2 (2)
        • LV3 (0)
      • Baekjoon (31)
        • Bronze (10)
        • Silver (19)
        • Gold (2)
        • Platinum (0)
        • Diamond (0)
      • Leetcode (0)
        • Easy (0)
        • Medium (0)
        • Hard (0)
        • SQL (0)
      • 알고리즘(Algorithm) (42)
      • JavaScript (40)
      • Linux (7)
      • JSP (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      자바스크립트 #연습문제
      자바스크립트
      JS #JavaScript #프로그래머스 #카카오
      Algorithm
      오블완
      JS #프로그래머스 #숫자의표현 #알고리즘
      리눅스
      JS #JavaScript #프로그래머스 #알고리즘
      BaekJoon
      async await #js #문법 #자바스크립트 #비동기
      JavaScript #Set #Collection
      Javascript
      고득점 Kit
      LeetCode #JS #Javascript #Algorithm
      c++
      프로그래머스 #자바스크립트 #JS
      리눅스 #입문
      파이썬 #입력 #python #input
      SQL
      Python #알고리즘
      알고리즘 #Algorithm
      Programmers
      JS #클래스
      티스토리챌린지
      Vue #Vue.js #정리
      알고리즘
      JSP #Vscode #톰켓 #Tomcat #Java #Web #jdk
      JS #javascript #객체 #Object
      프로그래머스
      JS #정규표현식
    • 최근 댓글

    • hELLO· Designed By정상우.v4.10.3
    Hun-bot
    Baekjoon 10816.숫자 카드 2 c++ [Silver IV]
    상단으로

    티스토리툴바

    티스토리툴바