[백준] 11723번 집합 / C++

#문제

백준 11723번 집합

#풀이

#include <iostream>
#include <string>

using namespace std;

bool arr[21] = { false };

void all()
{
	fill(begin(arr), end(arr), true);
}

void empty()
{
	fill(begin(arr), end(arr), false);
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);

	string oper;
	int m, x;
	cin >> m;

    while (m--)
    {
        cin >> oper;

        if (oper == "add")
        {
			cin >> x;
			arr[x] = true;
        }
		else if (oper == "remove")
		{
			cin >> x;
			arr[x] = false;
		}
		else if (oper == "check")
		{
			cin >> x;
			cout << arr[x] << '\n';
		}
		else if (oper == "toggle")
		{
			cin >> x;
			arr[x] = !arr[x];
		}
		else if (oper == "all")
		{
			all();
		}
		else
		{
			empty();
		}
    }

	return 0;
}

#정리

시간 제한이 1.5초로 굉장히 효율적인 연산을 필요로 하는 문제다. 집합 내의 숫자를 확인하는 연산이 있어서 해시를 쓰면 빠르게 풀 수 있을 줄 알았는데, 시간 초과로 실패. x의 입력이 1~20 사이라는 점에서 힌트를 얻어 21개 짜리(0빼고 20까지 써야하니까) bool 타입 집합을 만들고 입력받는 연산을 처리하는 코드를 작성하여 해결했다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 10828번 스택 / C++
  • [백준] 10866번 덱 / C++
  • [백준] 18258번 큐 2 / C++
  • [백준] 10845번 큐 / C++
  • [백준] 1244번 스위치 켜고 끄기 / C++