[BOJ] 10866번 : 덱

2024. 8. 7. 15:56Algorithm

1. problem :

https://www.acmicpc.net/problem/10866

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;

const int MX = 1000005;
int dat[2 * MX + 1];
int head = MX, tail = MX; 

void push_front(int x) {
	dat[--head] = x;
}
void push_back(int x) {
	dat[tail++] = x;
}
int pop_front() {
	if (head == tail) return -1;
	return dat[head++];
}
int pop_back() {
	if (head == tail) return -1;
	return dat[--tail];
}
int front() {
	if (head == tail) return -1;
	return dat[head];
}
int back() {
	if (head == tail) return -1;
	return dat[tail-1]; 
}
int size() {
	return (tail - head);
}
int empty() {
	return (head == tail) ? 1 : 0;
}


int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int N;
	cin >> N;

	while (N--) {
		string op;
		cin >> op;
		if (op == "push_front") {
			int x;
			cin >> x;
			push_front(x);
		}
		else if (op == "push_back") {
			int x;
			cin >> x;
			push_back(x);
		}
		else if (op == "front") cout << front() << '\n';
		else if (op == "back") cout << back() << '\n';
		else if (op == "size") cout << size() << '\n';
		else if (op == "empty") cout << empty() << '\n';
		else if (op == "pop_front") {
			cout << pop_front() << '\n';
		}
		else {
			cout << pop_back() << '\n';
		}
	}
	return 0;
}

'Algorithm' 카테고리의 다른 글

[BOJ] 5430번 : AC  (0) 2024.08.07
[BOJ] 1021번 : 회전하는 큐  (0) 2024.08.07
[BOJ] 2164번 : 카드2  (0) 2024.08.07
[BOJ] 10845 : 큐  (0) 2024.08.07
[BOJ] 2493: 탑  (0) 2024.08.07