Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- DFS
- Python
- 알고리즘
- Flutter
- 안드로이드
- cos pro 1급
- 분할정복
- DART
- AndroidStudio
- 안드로이드스튜디오
- 코딩테스트
- 코드품앗이
- 백준
- android
- Vue
- vuejs
- DFS와BFS
- 코테
- 동적계획법
- cos
- cos pro
- C++
- BAEKJOON
- codingtest
- issue
- django
- 파이썬
- 개발
- Algorithm
- 동적계획법과최단거리역추적
Archives
- Today
- Total
Development Artist
[Baekjoon, C++] 10866번 : 덱 본문
728x90
반응형
백준 단계별 풀기에서 큐,덱 다섯번째 문제이다.
링크는 아래와 같다.
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
1. Deque 라이브러리를 사용한다. 기본적으로 deque 라이브러리에서 제공하는 함수들이 지금 문제의 전부를 해결할 수 있다. en.cppreference.com/w/cpp/container/deque 해당 링크는 deque 라이브러리를 정리한 곳이다.
#include<iostream>
#include<deque>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int numberOfCommand;
deque<int> deque;
string str_command;
cin >> numberOfCommand;
for (int i = 0; i < numberOfCommand; i++) {
cin >> str_command;
if (str_command == "push_front") {
int num;
cin >> num;
deque.push_front(num);
}
else if (str_command == "push_back") {
int num;
cin >> num;
deque.push_back(num);
}
else if (str_command == "pop_front") {
if (!deque.empty()) {
cout << deque.front() << "\n";
deque.pop_front();
}
else {
cout << -1 << "\n";
}
}
else if (str_command == "pop_back") {
if (!deque.empty()) {
cout << deque.back() << "\n";
deque.pop_back();
}
else {
cout << -1 << "\n";
}
}
else if (str_command == "size") {
cout << deque.size() << "\n";
}
else if (str_command == "empty") {
if (deque.empty()) {
cout << 1 << "\n";
}
else {
cout << 0 << "\n";
}
}
else if (str_command == "front") {
if (!deque.empty()) {
cout << deque.front() << "\n";
}
else {
cout << -1 << "\n";
}
}
else if (str_command == "back") {
if (!deque.empty()) {
cout << deque.back() << "\n";
}
else {
cout << -1 << "\n";
}
}
}
return 0;
}
이상으로 Deque 문제를 마치겠다.
728x90
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
[Beakjoon, C++] 5430번 : AC (0) | 2021.01.19 |
---|---|
[Baekjoon, C++] 1021번 : 회전하는 큐 (0) | 2021.01.17 |
[Baekjoon, C++] 1966번 : 프린터 큐 (0) | 2021.01.15 |
[Baekjoon, C++] 11866번 : 요세푸스 문제 0 (0) | 2021.01.14 |
[Baekjoon, C++] 2164번 : 카드2 (0) | 2021.01.13 |
Comments