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


+ Recent posts