Problem
https://www.acmicpc.net/problem/10816
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,0
www.acmicpc.net
상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 몇 개 가지고 있는지 구하시오.
Solution
- upper_bound와 lower_bound()를 이용한 풀이: 이진탐색, O(logN)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, tmp;
cin >> n;
vector<int> v(n);
for(int i=0;i<n;i++)
cin >> v[i];
sort(v.begin(), v.end());
cin >> n;
vector<int> answer(n);
for(int i=0;i<n;i++){
cin >> tmp;
auto upper = upper_bound(v.begin(), v.end(), tmp);
auto lower = lower_bound(v.begin(), v.end(), tmp);
answer[i] = upper - lower;
}
for(auto m : answer)
cout << m << " ";
return 0;
}
cpp의 upper_bound와 lower_bound를 이용하면 쉽게 풀 수 있다. upper_bound()는 이진탐색으로 해당 숫자가 끝나는 위치를 반환하고 lower_bound()는 이진탐색으로 해당 숫자가 시작되는 위치를 반환한다.
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
위 코드는 C와 C++의 표준 stream의 동기화를 끊는 역할을 한다. cin과 cout의 속도가 C의 입출력 속도에 비해 떨어지기 때문에 저 코드를 사용해 속도를 높일 수 있다. 하지만 동기화를 끊게되면 C의 입출력 함수(scanf, printf, getchar...)를 더 이상 사용하지 못 한다. 위 코드를 쓸 때, c의 입출력 함수를 쓰지 않도록 조심하자.
- HashMap을 이용한 풀이: O(1)
#include<iostream>
#include<unordered_map>
#pragma warning(disable: 4996) // scanf 사용시 보안 경고 무시
using namespace std;
unordered_map<int, int> cards;
int n, m, tmp;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for(int i=0;i<n;i++){
cin >> tmp;
cards[tmp]++;
}
cin >> m;
for(int i=0;i<m;i++){
cin >> tmp;
cout << cards[tmp] << ' ';
}
return 0;
}
Reference
https://hwan-shell.tistory.com/300
백준 10816] C++ 숫자 카드 2
해당 문제는 백준 사이트에서 풀 수 있습니다. www.acmicpc.net/problem/10816 1. 문제 숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이
hwan-shell.tistory.com
https://puffinknight.tistory.com/69
#pragma warning(disable : 4996)
* 경고 C4996은 권장되지 않는 함수 사용에 관한 경고입니다. scanf는, MS가 지정한 잘못 쓰면 안전하지 않은 일이 발생할 수도 있는 함수들 중 하나입니다.그러나, 문제에서처럼 %d %f 같은 고정폭 변
puffinknight.tistory.com
sync_with_stdio(false) 쓸 때 주의할 사항
주의할 점 백준 문제를 풀다가 아래 코드가 틀렸었다. 왜 틀렸나 봤는데 ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);이 부분 때문이었다. ```c++ #include #include using namespace std; int arr[53]; int ans[53]; s
ip99202.github.io
https://bbeomgeun.tistory.com/18
[baekjoon 10816] 숫자 카드2 - Hashmap, 이분탐색 (C++)
www.acmicpc.net/problem/10816 10816번: 숫자 카드 2 첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적
bbeomgeun.tistory.com
C++ 자료구조 - unordered map(hash map)
unordered map(hash map) map과 비슷하지만 정렬이 되어있지 않다. insert, erase, find 모두가 O(1)O(1) 으로 수행된다 셋이나 맵의 경우 O(log n)O(logn) 이었지만, unordered_set 과 unordered_map 의 경우 상수 시간에 원
dev-su.tistory.com
'코딩 테스트 > 백준 (C++, Python)' 카테고리의 다른 글
cpp) 백준 13305: 주유소 (0) | 2023.06.27 |
---|---|
cpp) 백준 1764: 듣보잡 (0) | 2023.06.25 |
cpp) 백준 1920: 수 찾기 (0) | 2023.06.15 |
cpp) 백준 10866: 덱 (0) | 2023.06.14 |
cpp) 백준 1676: 팩토리얼 0의 개수 (1) | 2023.06.13 |