반응형
문제 출처 :
https://www.acmicpc.net/problem/1822
알고리즘 분석 :
문제 해결에 필요한 사항
1. Map STL :: http://www.crocus.co.kr/604
맵을 이용하여 풀면 간단하게 해결 할 수 있는 문제이다.
우선 A집합의 value를 받아 map[value] = true로 저장시키고 그다음 B집합의 value를 받아 map[value] = false로 저장시킨 후
map을 순회하며 true인 개수를 찾으면 정답이 된다.(true에서 false가 된 것 자체가 차집합을 수행하고 있음을 의미한다.)
소스 코드 :
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <cstdio> #include <map> using namespace std; map<int, bool> mp; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { int val; scanf("%d", &val); mp[val] = true; } for (int i = 0; i < m; i++) { int val; scanf("%d", &val); mp[val] = false; } int cnt = 0; for (auto it = mp.begin(); it != mp.end(); it++) if (it->second == true) cnt++; printf("%d\n", cnt); for (auto it = mp.begin(); it != mp.end(); it++) if (it->second == true) printf("%d ", it->first); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[2412번] 암벽 등반 (0) | 2017.07.02 |
---|---|
[2798번] 블랙잭 (0) | 2017.07.02 |
[Codeground 31번] 프리랜서 (0) | 2017.06.29 |
[Codeground 30번] Rectangles (0) | 2017.06.29 |
[12852번] 1로 만들기 2 (0) | 2017.06.29 |