![[백준 / BOJ] 10816번 숫자 카드 2 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FsUWS0%2FbtrJql7IAfM%2FPumKMmTfbPktkmkiMjzOek%2Fimg.png)
[백준 / BOJ] 10816번 숫자 카드 2 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 1. 19:43
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/10816
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
문제

문제 풀이
숫자 카드와 유사한 문제이지만 이번에는 1, 0만 출력하는 게 아니라 몇 개를 가지고 있는지 출력해야 한다.
upper_bound와 lower_bound 반복자의 차이를 구하면 그것이 해당 숫자 카드의 갯수다.
파이썬은 언제나 그랬듯이 간단하게 풀린다. 인터넷에 다른 풀이들도 많겠지만 Collections 모듈을 활용해도 된다.
C++ 코드 전문
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int N, M; | |
std::cin >> N; | |
std::vector<int> v1(N); | |
for (int i = 0; i < N; ++i) | |
std::cin >> v1[i]; | |
std::cin >> M; | |
std::vector<int> v2(M); | |
for (int i = 0; i < M; ++i) | |
std::cin >> v2[i]; | |
std::sort(v1.begin(), v1.end()); | |
for (int i = 0; i < M; ++i) { | |
if (std::binary_search(v1.begin(), v1.end(), v2[i])) { | |
std::cout << std::upper_bound(v1.begin(), v1.end(), v2[i]) - std::lower_bound(v1.begin(), v1.end(), v2[i]) << " "; | |
} | |
else | |
std::cout << "0 "; | |
} | |
return 0; | |
} |
Python 코드 전문
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import Counter | |
input = sys.stdin.readline | |
N = input() | |
arr = input().split() | |
M = input() | |
ans = input().split() | |
arr = Counter(arr) | |
print(' '.join(f'{arr[i]}' if i in arr else '0' for i in ans)) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1269번 대칭 차집합 (C++, Python) (0) | 2022.06.01 |
---|---|
[백준 / BOJ] 1764번 듣보잡 (C++, Python) (0) | 2022.06.01 |
[백준 / BOJ] 1620번 나는야 포켓몬 마스터 이다솜 (C++, Python) (0) | 2022.06.01 |
[백준 / BOJ] 14425번 문자열 집합 (C++, Python) (0) | 2022.06.01 |
[백준 / BOJ] 10815번 숫자 카드 (C++, Python) (0) | 2022.06.01 |
@Reo :: 코드 아카이브
자기계발 블로그