일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- stack
- Gold
- 프로세스 상태
- PrefixSum
- BOJ
- Flyweight Pattern
- programmers
- effective C++
- level1
- Unity
- Modern C++
- knapsack Problem
- Project
- two pointer
- 프로그래머스
- SWEA
- dirtyflag pattern
- Bronze
- Euclidean
- LEVEL2
- algorithm
- 8-Puzzle
- Zenject
- 3D RPG
- Silver
- level3
- BFS
- solid 원칙
- trie
- binary search
Archives
- Today
- Total
Patrick's Devlog
[BOJ/C++] 집합(11723번) 본문
1. 개요
https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
1-1. 설명
비어있는 공집합 S가 주어졌을때, 원소 추가, 제거, 체크, 토글 등 여러 연산들을 수행하는 프로그램을 작성한다. 자세한 연산은 위의 링크를 참고한다.
1-2. 제한 사항
- 첫 줄에 수행해야 하는 연산 수 M이 주어지며, M은 1 이상 3,000,000 이하
- 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한줄에 하나씩 주어짐
2. 구현
2-1. 풀이
메모리가 매우 한정적이므, 비트마스킹을 이용해 풀었다.
2-2. 코드
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int M, S = 0, inputNum;
string inputStr;
cin >> M;
for (int i = 0; i < M; i++) {
cin >> inputStr;
if (inputStr == "all") {
S = (1 << 20) - 1;
continue;
}
else if (inputStr == "empty") {
S = 0;
continue;
}
cin >> inputNum;
inputNum -= 1;
if (inputStr == "add") S |= (1 << inputNum);
else if (inputStr == "remove") S &= ~(1 << inputNum);
else if (inputStr == "check") cout << ((S & (1 << inputNum)) ? '1' : '0') << "\n";
else if (inputStr == "toggle") S ^= (1 << inputNum);
}
return 0;
}
'Algorithm > Algorithms Practice' 카테고리의 다른 글
[BOJ/C++] 부분합(1806번) (1) | 2022.11.22 |
---|---|
[BOJ/C++] 영단어 암기는 괴로워(20920번) (0) | 2022.11.21 |
[BOJ/C++] A와 B 2(12919번) (0) | 2022.11.17 |
[BOJ/C++] 비밀번호 발음하기(4659번) (0) | 2022.11.16 |
[BOJ/C++] 파티(1238번) (0) | 2022.11.15 |