Recent Posts
Notice
No Rules Rules
큐 2 (feat. 백준, 18258번) 본문
728x90
반응형
큐 2
https://www.acmicpc.net/problem/18258
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <deque>
#include <cstring>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
register int N;
string str;
deque<int> ans;
cin >> N;
for (register int n = 0, v; n < N; ++n) {
cin >> str;
if (!str.compare("push"))
cin >> v, ans.push_back(v);
else if (!str.compare("pop"))
if (ans.empty())
cout << -1 << "\n";
else
cout << ans.front() << "\n", ans.pop_front();
else if (!str.compare("size"))
cout << ans.size() << "\n";
else if (!str.compare("empty"))
if (ans.empty())
cout << 1 << "\n";
else
cout << 0 << "\n";
else if (!str.compare("front"))
if (ans.empty())
cout << -1 << "\n";
else
cout << ans.front() << "\n";
else if (!str.compare("back"))
if (ans.empty())
cout << -1 << "\n";
else
cout << ans.back() << "\n";
}
return 0;
}
// *&)*@*
- 자료구조 deque를 이용하여 문제에서 주어진 front, back을 구현합니다.
- deque는 양방향으로 삽입 및 삭제가 가능한 자료구조 입니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
요세푸스 문제 0 (feat. 백준, 11866번) (0) | 2022.08.12 |
---|---|
카드2 (feat. 백준, 2164번) (0) | 2022.08.12 |
오큰수 (feat. 백준, 17298번) (0) | 2022.08.12 |
스택 수열 (feat. 백준, 1874번) (0) | 2022.08.12 |
균형잡힌 세상 (feat. 백준, 4949번) (0) | 2022.08.12 |
Comments