728x90
반응형
https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int m;
cin >> m;
set<int> s;
set<int> tmp;
for(int i = 1; i <= 20; i++) tmp.insert(i);
string str;
int k;
for(int i = 0; i < m; i++){
cin >> str;
if(str == "add"){
cin >> k;
s.insert(k);
} else if(str == "remove"){
cin >> k;
s.erase(k);
} else if(str == "check"){
cin >> k;
if(s.find(k) == s.end()){
cout << "0\n";
} else{
cout << "1\n";
}
} else if(str == "toggle"){
cin >> k;
if(s.find(k) == s.end()){
s.insert(k);
} else{
s.erase(k);
}
} else if(str == "all"){
s = tmp;
} else if(str == "empty"){
s.clear();
}
}
return 0;
}
all일 때마다 for문을 돌리니까 시간 초과가 발생했다.
{1, 2, ..., 20}인 집합 tmp를 미리 만들어두고 all일 때 s에 tmp를 대입하여 시간 초과를 해결했다.
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[BOJ] 9461번 파도반 수열 (C++) (0) | 2022.01.09 |
---|---|
[BOJ] 1316번 그룹 단어 체커 (C++) (0) | 2022.01.08 |
[BOJ] 1620번 나는야 포켓몬 마스터 이다솜 (C++) (0) | 2022.01.05 |
[BOJ] 1753번 최단경로 (C++) (0) | 2022.01.04 |
[BOJ] 1764번 듣보잡 (C++) (0) | 2022.01.03 |