문제에서 주어지는
order, sort, complete를 각각 구현하였다.
뭔가 효율적으로 할 수 있을거 같은데, 방법이 떠오르지 않아 무식하게 풀었다.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> #include <queue> #include <string> #include <vector> #include <algorithm> #define P pair<int,int> using namespace std; //큐에 들어있는 내용을 순서대로 출력 void printAll(queue<P> q) { if (q.empty()) { cout << "sleep\n"; return; } while (true) { if (!q.empty()) { cout << q.front().second << " "; q.pop(); } else { break; } } cout << "\n"; } // 완성된 테이블 제거 queue<P> compQ(queue<P> &q,int n) { vector<P> v; while (true) { if (!q.empty()) { if (q.front().second != n) { v.push_back(q.front()); } q.pop(); } else { break; } } for(int i = 0; i < v.size(); i++) { q.push(v[i]); } return q; } // 시간순으로 정렬 queue<P> sortQ(queue<P> q) { vector<P> v; while (true) { if (!q.empty()) { v.push_back(q.front()); q.pop(); } else { break; } } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { q.push(v[i]); } return q; } int main() { int n, m; cin >> n >> m; queue<P> q; string str; int x, y; for (int i = 0; i < n; i++) { cin >> str; if (str == "order") { cin >> x >> y; q.push(P(y, x)); printAll(q); } else if (str == "sort") { q = sortQ(q); printAll(q); } else if(str=="complete"){ cin >> x; compQ(q, x); printAll(q); } } } | cs |