[BOJ]11723-집합

yoon_H·2024년 9월 9일

BOJ

목록 보기
89/110

11723

처음에는 set 컨테이너랑 함수 호출 형식으로 작성했는데 시간 초과가 났다.
find를 계속 돌리는 것과 함수 호출에서 느려지는 것 같았다.
문제를 보니 x의 범위값이 1에서 20뿐이어서 bool 배열로 값을 저장하도록 진행했다.
그리고 성공!

성공코드

#include <iostream>
using namespace std;

int N;

bool arr[21];

void Set(bool flag)
{
	for (int i = 1; i < 21; i++)
	{
		arr[i] = flag;
	}
}

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

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		string temp;

		cin >> temp;

		if (temp == "all")
		{
			Set(true);
			continue;
		}
		else if (temp == "empty")
		{
			Set(false);
			continue;
		}
		else
		{
			int num;
			cin >> num;

			if (temp == "add")
			{
				arr[num] = true;
			}
			else if (temp == "remove")
			{
				arr[num] = false;
			}
			else if (temp == "check")
			{
				if (arr[num])
				{
					cout << 1 << '\n';
				}
				else
				{
					cout << 0 << '\n';
				}
			}
			else if (temp == "toggle")
			{
				arr[num] = !arr[num];
			}
		}
	}
	
}

0개의 댓글