백준 문제풀이

백준 6198번 - C++

diligent_gideok 2022. 4. 30. 06:17
#include <bits/stdc++.h>
using namespace std;

// push만 사용해서 메모리가 오버된다.
int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	stack<long long> S;
	long long A[80002];
	int n,sum=0,p=0;
	cin >> n;
	int flag = n;
	while (flag--) {
		long long m;
		cin >> m;
		S.push(m);
	}
	flag = n;
	while (flag--) {
			if(p!=0){
				int i = p;
				while (i--) {
					if (A[i] < S.top()) {
						sum++;
					}
					else {
						break;
					}
				}
			}
			A[p++]=S.top();
			S.pop();
	}
	cout << sum;  
}

 

#include <bits/stdc++.h>
using namespace std;

#define ll long long

stack<int> s;
int n;
ll ans;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    ll h;
    while (n--) {
        cin >> h;
        while (!s.empty() && s.top() <= h)
            s.pop();
        ans += s.size();
        s.push(h);
    }
    cout << ans;
    return 0;
}