https://www.acmicpc.net/problem/6198
O(2n)
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 | #include <stack> #include <stdio.h> #include <iostream> using namespace std; int main() { stack<int> stack1; int N; unsigned long long count = 0; cin >> N; for (int i = 0; i < N; i++) { int input; cin >> input; if (stack1.empty()) stack1.push(input); else { while (stack1.top() <= input) { stack1.pop(); if (stack1.empty()) { stack1.push(input); break; } } if (stack1.top() > input) stack1.push(input); } count += stack1.size() - 1; } cout << count; return 0; } | cs |
'Algorithm Problems > BOJ' 카테고리의 다른 글
[11718번] Output as is 그대로 출력하기 - In/Output[C++] (0) | 2016.12.27 |
---|---|
[4673번] Self Number 셀프 넘버 - Method[Java] (0) | 2016.12.22 |
[11718번] Output as is 그대로 출력하기 - In/Output[Java] (0) | 2016.12.22 |
[9012번] Parenthesis 괄호 - Stack[C++] (0) | 2016.12.19 |
[3986번] ESEJ(Nice Word) - Stack [C++] ☆ (0) | 2016.12.19 |