본문 바로가기

algorithm

(69)
백준 10866번 덱 https://www.acmicpc.net/problem/10866 아이디어:1. STL deque의 사용법을 묻는 기본 문제2. 덱이 비어있을 때의 예외처리3. push 입력받고 int tmp; cin >> tmp; 처리하기 #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, tmp; deque DQ; string str; cin >> n; while (n--) { cin >> str; if (str == "push_front") { cin >> tmp; DQ.push_front(tmp); } else if (str == "push_back") { ..
덱, DequeDouble Ended Queue양쪽 끝에서 삽입과 삭제가 모두 가능한 자료구조 덱의 성질원소의 추가, 제거, 확인이 O(1)제일 앞/뒤가 아닌 나머지 원소들의 확인과 변경이 원칙적으로 불가단, STL deque에서는 인덱스로 원소에 접근 가능 구현head는 앞쪽 포인터, tail은 뒤쪽 + 1 빈공간 포인터덱은 가운데에서 앞/뒤로 모두 늘어나므로 크기를 2 * MX + 1로 지정하고 head와 tail의 초기값으로 MX를 넣어줘야 한다.#include 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..
백준 2164번 카드2 https://www.acmicpc.net/problem/2164 아이디어:1. 큐에 1부터 n까지 push()2. 큐 크기가 1이 될 때까지 pop(), push(), pop() #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; queue Q; for (int i = 1; i 1) { Q.pop(); Q.push(Q.front()); Q.pop(); } cout
백준 10845번 큐 https://www.acmicpc.net/problem/10845 아이디어:1. STL queue의 사용법을 묻는 기본 문제2. 큐가 비어있을 때의 예외처리3. push 입력받고 int tmp; cin >> tmp; 처리하기#include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string str; queue Q; cin >> n; while (n--) { cin >> str; if (str == "push") { int tmp; cin >> tmp; Q.push(tmp); } else if (str == "pop") { if (!Q.empty..
큐, Queue한쪽 끝에서 원소를 넣고 반대쪽 끝에서 원소를 빼는 자료구조FIFO: First Input First Output 큐의 성질원소의 추가, 제거, 확인이 O(1)제일 앞/뒤가 아닌 나머지 원소들의 확인과 변경이 원칙적으로 불가추가는 뒤쪽(rear, tail), 제거는 앞쪽(front, head)에서 이루어짐 원형 큐 Circular Queue선형 큐를 배열로 만들었을 때, pop으로 앞쪽에 빈 공간이 생겨도 원소를 더 채울 수 없다.큐를 원형으로 만들면 앞쪽 빈 공간을 활용할 수 있다. 구현head는 제거하는 앞쪽, tail는 추가하는 뒤쪽#include using namespace std;const int MX = 1000005;int dat[MX];int head = 0, tail = 0..