
#define endl "\n"을 통해서 시간을 줄였다.
원래 endl은 줄바꿈 + 버퍼 비우기까지 하므로, 출력 호출이 많을 수록 느려진다.
#include <iostream>
#include <queue>
#define endl "\n"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
queue<int> q;
for (int i = 0; i < n; i++) {
int num;
string s;
cin >> s;
if (s == "push") {
cin >> num;
q.push(num);
} else if (s == "pop") {
if (q.size() > 0) {
cout << q.front() << endl;
q.pop();
continue;
}
cout << -1 << endl;
} else if (s == "size") {
cout << (q.size() > 0 ? q.size() : 0) << endl;
} else if (s == "empty") {
cout << (q.size() == 0 ? 1 : 0) << endl;
} else if (s == "front") {
cout << (q.size() > 0 ? q.front() : -1) << endl;
} else if (s == "back") {
cout << (q.size() > 0 ? q.back() : -1) << endl;
}
}
}'Algorithm > BOJ' 카테고리의 다른 글
| [백준] 10799번 SIlver 2 쇠막대기, C++ 코드 (0) | 2025.12.12 |
|---|---|
| [백준] 11003번 Gold 1 최솟값 찾기, C++ 코드 (0) | 2025.12.11 |
| [백준] 1021번 Silver3 회전하는 큐, C++ 코드 (0) | 2025.12.10 |
| [백준] 1969번 Silver 4 DNA, C++ 코드 (0) | 2025.12.10 |
| [백준] 17413번 Silver 3 단어 뒤집기 2, C++ 코드 (0) | 2025.12.08 |