백준 알고리즘(C++)

백준 1269번 대칭 차집합 ( C++ )

coding232624 2024. 9. 10. 19:11

문제

https://www.acmicpc.net/problem/7795

 

해설

최대값이 너무 크기 때문에 map을 사용하면 되는 문제

 

코드

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
43
44
45
46
47
48
#include<iostream>
#include<algorithm>
#include<map>
 
using namespace std;
 
map<int,int> mp;
int ret,a,b,tmp;
 
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
 
  cin >> a >> b;
 
  for(int i=0;i<a;i++){
    cin >> tmp;
    if(mp[tmp]==-1){
      continue;
    }
    else if(mp[tmp] == 1){
      ret--;
      mp[tmp] = -1;
    }
    else if(mp[tmp] == 0){
      ret++;
      mp[tmp] = 1;
    }
  }
 
  for(int i=0;i<b;i++){
    cin >> tmp;
    if(mp[tmp]==-1){
      continue;
    }
    else if(mp[tmp] == 1){
      ret--;
      mp[tmp] = -1;
    }
    else if(mp[tmp] == 0){
      ret++;
      mp[tmp] = 1;
    }
  }
 
  cout << ret;
  return 0;
}
cs