[c++][정올][Intermediate_Coder] 1141 : 불쾌한 날(Bad Hair Day)

[c++][정올][Intermediate_Coder] 1141 : 불쾌한 날(Bad Hair Day)

코드

#include <iostream>
#include <vector>
#include <deque>
  
using namespace std;
  
int main() {
    int N;
    cin>>N;
      
    vector<unsigned long> numbers(N);
    for (int i=0; i<N; i++){
        cin>>numbers[i];
    }
      
    vector<unsigned long> answer(N);
    deque<vector<unsigned long>> dq;
    for (int idx=0; idx<N; idx++){
        while (!dq.empty() and dq.back()[1]<=numbers[idx]){
            vector<unsigned long> ck = dq.back();
            dq.pop_back();
            answer[ck[0]]=idx-ck[0]-1;
        }
        dq.push_back({(unsigned long)idx,numbers[idx]});
    }
      
    while (!dq.empty()){
        vector<unsigned long> ck = dq.back();
        dq.pop_back();
        answer[ck[0]]=N-ck[0]-1;
    }
      
    unsigned long ans = 0;
    for (int i=0; i<N; i++){
        ans+=answer[i];
    }
    cout<<ans;
}

Discussion and feedback