백준 1874번

2022. 1. 30. 20:11·Study/Algorithm

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

 

1874번: 스택 수열

1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.

www.acmicpc.net

#include <iostream>
#include <stack>
#include <vector>

using namespace std;
int main()
{
	stack<int> s;
	vector<char> result;
    vector<int> input;


	int n;
	cin >> n;

	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
        input.push_back(x);
	}

    int count = 0;

    for (int i = 1; i <= n; i++) {
        s.push(i);
        result.push_back('+');

        while (!s.empty() && s.top() == input[count]) {
            s.pop();
            result.push_back('-');
            count++;
        }
    }

    if (!s.empty()) {
        cout << "NO";
    }
    else {
        for (int i = 0; i < result.size(); i++) {
            cout << result[i] << '\n';
        }
    }
}

설계

'Study > Algorithm' 카테고리의 다른 글

백준 1068번  (0) 2022.02.06
백준 14501번  (0) 2022.01.30
백준 9935번  (0) 2022.01.30
백준 1991번  (0) 2022.01.30
백준 11279번  (0) 2022.01.30
'Study/Algorithm' 카테고리의 다른 글
  • 백준 1068번
  • 백준 14501번
  • 백준 9935번
  • 백준 1991번
_WooHyun_
_WooHyun_
  • _WooHyun_
    Nerd
    _WooHyun_
  • 전체
    오늘
    어제
    • 분류 전체보기 (79)
      • Study (60)
        • Algorithm (24)
        • Unreal Engine (19)
        • C++ (1)
        • Maya (1)
        • GoLang (3)
        • Mysql (3)
        • Linux (7)
        • Server (2)
      • Projects (0)
        • Unreal Engine (0)
        • Server (0)
      • 개발일지 (8)
        • Unreal Engine (7)
        • Art (1)
        • Server (0)
      • 미래 (5)
      • 개발아이디어 (0)
      • 잡지식 (2)
  • 블로그 메뉴

    • 홈
    • 방명록
    • 글쓰기
    • 블로그설정
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
_WooHyun_
백준 1874번
상단으로

티스토리툴바