๐Ÿ• Baaaaaarking/0x06๊ฐ• - ํ

[BOJ S4][C++] ๋ฐฑ์ค€ 10845๋ฒˆ : ํ

์„ ๋‹ฌ 2022. 2. 17. 18:32
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/10845

 

10845๋ฒˆ: ํ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

 

๋ฌธ์ œ

์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋ช…๋ น์€ ์ด ์—ฌ์„ฏ ๊ฐ€์ง€์ด๋‹ค.

  • push X: ์ •์ˆ˜ X๋ฅผ ํ์— ๋„ฃ๋Š” ์—ฐ์‚ฐ์ด๋‹ค.
  • pop: ํ์—์„œ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๋นผ๊ณ , ๊ทธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • size: ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • empty: ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • front: ํ์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • back: ํ์˜ ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€ ์•Š์€ ๋ช…๋ น์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ๋ช…๋ น์ด ์ฃผ์–ด์งˆ ๋•Œ๋งˆ๋‹ค, ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

c++์—์„œ ํ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

์‹œ๊ฐ„ ๋ณต์žก๋„ : ์›์†Œ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ์•ž ๋’ค ์›์†Œ ํ™•์ธ O(1)

 

- ํ—ค๋”ํŒŒ์ผ

#include <queue>

 

- ํ ์„ ์–ธํ•˜๊ธฐ

queue<ํƒ€์ž…> ์ด๋ฆ„;
queue<int> q;

 

- ํ์™€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋“ค

// void
q.push(๊ฐ’);
q.pop();

// bool
q.empty();

// int
q.size()

// ์š”์†Œ
q.front()
q.back()

 


์œ„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ ๋‹นํžˆ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ  ํ’€๋ฉด ๋œ๋‹ค

// Authored by : seondal
// Co-authored by : -

//#include <bits/stdc++.h>
#include <iostream>
#include <queue>

using namespace std;

queue<int> q;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    cin >> n;
    while(n--){
        string command;
        cin >> command;
        
        if(command == "push"){
            int num;
            cin >> num;
            q.push(num);
        }
        else if(command == "pop"){
            if(!q.empty()){
                cout << q.front()<<"\n";
                q.pop();
            }
            else cout << -1 << "\n";
        }
        else if(command == "size"){
            cout << q.size() << "\n";
        }
        else if(command == "empty"){
            if(q.empty()) cout << 1 << "\n";
            else cout << 0 << "\n";
        }
        else if(command == "front"){
            if(!q.empty()) cout << q.front() << "\n";
            else cout << -1 << "\n";
        }
        else if(command == "back"){
            if(!q.empty()) cout << q.back() << "\n";
            else cout << -1 << "\n";
        }
    }
    
    return 0;
}

/*
 */
๋ฐ˜์‘ํ˜•

'๐Ÿ• Baaaaaarking > 0x06๊ฐ• - ํ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ S4][C++] ๋ฐฑ์ค€ 2164๋ฒˆ: ์นด๋“œ2  (0) 2022.02.18