PS
BOJ 10845 : 큐
lickelon
2024. 2. 15. 22:27
- 문제 링크 : boj.kr/10845
- 난이도 : S4
- 태그 : 구현, 큐
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
queue<int> _q;
int last = -1;
while(n--) {
string s;
cin >> s;
if(s == "push") {
int x;
cin >> x;
_q.push(x);
last = x;
}
if(s == "pop") {
if(_q.empty()) {
cout << -1 << "\n";
}
else {
cout << _q.front() << "\n";
_q.pop();
}
}
if(s == "size") cout << _q.size() << "\n";
if(s == "empty") cout << (_q.empty() ? 1 : 0) << "\n";
if(s == "front") cout << (_q.empty() ? -1 : _q.front()) << "\n";
if(s == "back") cout << (_q.empty() ? -1 : last) << "\n";
}
return 0;
}
풀이
큐라는 자료구조를 사용할 줄 아는지 물어보는 단순한 구현문제이다.
728x90