๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“• STUDY/Algorithm

[C / C++] ๋ฐฑ์ค€ 10845๋ฒˆ - ํ(Queue)

๋ฐฑ์ค€ 10828๋ฒˆ : https://www.acmicpc.net/problem/10845

 

10845๋ฒˆ: ํ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

โœ… ๋ฌธ์ œ ์„ค๋ช…

๋ง ๊ทธ๋Œ€๋กœ ํ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

โšก ํ๋ž€?

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ์„ ์ž…์„ ์ถœ ๊ตฌ์กฐ!

FIFO (First In First Out) ๋กœ,

์‹๋‹น์— ๊ฐ€์„œ ์ค„์„ ์„œ๋ฉด ๋จผ์ € ์ค„์„ ์„  ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ž…์žฅํ•˜๋“ฏ์ด

๊ฐ€์žฅ ๋จผ์ € ๋„ฃ์€ ์ •๋ณด๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๊บผ๋‚ด๋Š” ๊ตฌ์กฐ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด a -> b -> c ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์—ˆ๋‹ค๋ฉด, ๋บ„ ๋•Œ๋Š” a -> b -> c ์ˆœ์„œ๋กœ ๊บผ๋‚ด๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์—ฌ๊ธฐ์„œ ๋„ฃ๋Š” ๊ฒƒ์€ ‘push’, ๋นผ๋Š” ๊ฒƒ์€ ‘pop’์ด๋ผ๊ณ  ์นญํ•œ๋‹ค.

 

1. ์ดˆ๊ธฐ ์ƒํƒœ (์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ๊ณต๋ฐฑ ์ƒํƒœ)

2. push(a) -> push(b) -> push(c)

3. pop() -> pop() -> pop()

1. push X : ์ •์ˆ˜ X๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.

2. pop : ํ์—์„œ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๋นผ์„œ ์ถœ๋ ฅ (๋น„์–ด์žˆ๋‹ค๋ฉด –1 ์ถœ๋ ฅ)

3. size : ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

4. empty : ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0 ์ถœ๋ ฅ

5. front : ํ์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š”(๋จผ์ € ๋„ฃ์€) ์ •์ˆ˜ ์ถœ๋ ฅ (๋น„์–ด์žˆ์œผ๋ฉด –1์ถœ๋ ฅ)

6. back : ํ์˜ ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š”(๋งˆ์ง€๋ง‰์— ๋„ฃ์€) ์ •์ˆ˜ ์ถœ๋ ฅ (๋น„์–ด์žˆ์œผ๋ฉด –1 ์ถœ๋ ฅ)


โœ… ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๋ช…

1. ์ „์—ญ๋ณ€์ˆ˜ idx_f, idx_b์„ ์‚ฌ์šฉ

-1) idx_b์€ ํ˜„์žฌ ํ์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋œปํ•˜๋ฉฐ, idx_f๋Š” ๋งจ ์•ž ์ธ๋ฑ์Šค๋ฅผ ๋œปํ•จ

-2) ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— idx_b์„ 0์ด ์•„๋‹ˆ๋ผ –1๋กœ ์‹œ์ž‘ํ•จ

 

2. front()

-1) empty() == 1์ด๋ฉด –1 ๋ฐ˜ํ™˜

-2) ์•„๋‹ˆ๋ฉด queue[idx_f] (๋งจ ์•ž์— ์žˆ๋Š” ๊ฐ’) ๋ฐ˜ํ™˜

 

3. back()

-1) empty() == 1์ด๋ฉด –1 ๋ฐ˜ํ™˜

-2) ์•„๋‹ˆ๋ฉด queue[idx_b] (๋งจ ๋’ค์— ์žˆ๋Š” ๊ฐ’) ๋ฐ˜ํ™˜

 

4. push(int x)

-1) idx_b๊ฐ€ -1๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— +1์„ ํ•œ ํ›„ ๊ฐ’ x ๋„ฃ๊ธฐ

 

5. pop()

-1) empty() == 1์ด๋ฉด ๊ณต๋ฐฑ์ด๋ฏ€๋กœ –1 ๋ฐ˜ํ™˜

-2) pop()์„ ํ•˜๋ฉด ๋งจ ์•ž ์ •์ˆ˜๋ฅผ ๊บผ๋‚ด๊ธฐ ๋•Œ๋ฌธ์— idx_f++

-3) ๊บผ๋‚ด๊ธฐ ์ „ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ queue[idx_f-1] ๋ฐ˜ํ™˜

 

6. size()

-1) (๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค - ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค) + 1์„ ํ•˜๋ฉด ์ „์ฒด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‚˜์˜ด

ex) ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค = 1 ๐Ÿ’› ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค = 3
(๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค – ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค = 3 - 1 = 2) โฉ 2+1 = 3
idx = 0 idx =1  idx = 2 idx = 3
  a b c

 

7. empty()

idx_f > idx_b์ด๋ฉด ๋น„์–ด์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•จ


โœ… ์ฝ”๋“œ [C์–ธ์–ด]

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 

int queue[10000];
int idx = -1; // idx๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋‹ˆ๊นŒ -1๋กœ ์„ค์ •
int first = 0; // front์šฉ

// true๊ฐ€ ํ˜„์žฌ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋Š” ์ƒํƒœ, false๊ฐ€ ๊ณต๋ฐฑ
typedef enum {false, true} bool;
bool emp = false;  // ์ดˆ๊ธฐ์ƒํƒœ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ์ฒ˜๋ฆฌ

void push(int x) {
	idx++; // -1๋กœ ์„ค์ •ํ•ด์„œ ++ํ•˜๊ณ  ์‹œ์ž‘
	queue[idx] = x;
	emp = true; // ๋ญ์žˆ๋‹ค๊ณ  ๋ฐ”๊พธ๊ธฐ
}

void pop() {
	if (emp == false)
		printf("-1\n");
	else {
		printf("%d\n", queue[first]);
		if (idx == first)
			emp = false;
		first++; // first ++
	}
}

void front() {
	if (emp == false) // ๋น„์–ด์žˆ๋‹ค๋ฉด
		printf("-1\n");
	else
		printf("%d\n", queue[first]);
}

void back() {
	if (emp == false) // ๋น„์–ด์žˆ๋‹ค๋ฉด
		printf("-1\n");
	else
		printf("%d\n", queue[idx]);
}

void size() {
	printf("%d\n", idx + 1 - first);
}

void empty() {
	if (emp == true)
		printf("0\n");
	else
		printf("1\n");
}

int main(void) {
	int n, x;
	scanf("%d", &n);

	char input[6];
	for (int i = 0; i < n; i++) {
		scanf("%s",input);
		if (!strcmp(input, "push")) {
			scanf("%d", &x);
			push(x);
		}
		else if (!strcmp(input, "pop")) {
			pop();
		}
		else if (!strcmp(input, "size"))
			size();
		else if (!strcmp(input, "empty")) {
			empty();
		}
		else if (!strcmp(input, "front")) {
			front();
		}
		else if (!strcmp(input, "back")) {
			back();
		}
	}
	return 0;
}

 

https://github.com/2hyunjinn/Baekjoon/blob/6e155f58b7fdf95083d84ad5e97a3f2798bcbb55/Baekjoon_10845.c

 

GitHub - 2hyunjinn/Baekjoon

Contribute to 2hyunjinn/Baekjoon development by creating an account on GitHub.

github.com

์œ„ ์ฝ”๋“œ๋Š” queue์˜ ์ •ํ™•ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ’€์–ด์„œ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

๋งŒ์•ฝ C++์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด๋ฏธ queue๋ฅผ ๊ตฌํ˜„ํ•ด๋†“์€ C++์˜ ๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.


โœ… ์ฝ”๋“œ [C++ ๋‚ด์žฅํ•จ์ˆ˜]

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
using namespace std;

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

	int n;
	cin >> n;

	queue<int> queue;
	for (int i = 0; i < n; i++) {
		string word;
		cin >> word;
		if (word == "push") {
			int x;
			cin >> x;
			queue.push(x);
		}
		else if (word == "pop") {
			if (queue.empty()) cout << -1 << '\n';
			else {
				cout << queue.front() << '\n';
				queue.pop();
			}
		}
		else if (word == "size") {
			cout << queue.size() << '\n';
		}
		else if (word == "empty") {
			cout << queue.empty() << '\n';
		}
		else if (word == "front") {
			if (queue.empty()) cout << -1 << '\n';
			else cout << queue.front() << '\n';
		}
		else if (word == "back") {
			if (queue.empty()) cout << -1 << '\n';
			else cout << queue.back() << '\n';
		}
	}
	return 0;
}

 

 

https://github.com/2hyunjinn/Baekjoon/blob/7601643deb85a3f83e69779bf15ddef03a9508e7/Baekjoon_10845.cpp

 

GitHub - 2hyunjinn/Baekjoon

Contribute to 2hyunjinn/Baekjoon development by creating an account on GitHub.

github.com