D問題 Building
「後ろから考える」が肝になる問題でしたね、本番中に正解したかった。。
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <atcoder/all>
#include <stack>
#define INF (1LL<<30)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; }
else return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; }
return false;
}
long long popcount(long long n) {
long long ans = 0;
for (int x = 0; x <= 60; x++) if (((n >> x) & 1) == 1) ans++;
return ans;
}
struct TreeNode {
std::vector<int> Children;
bool IsVisited = false;
};
int main() {
int N;
std::cin >> N;
std::vector<int> H(N);
for (int n = 0; n < N; n++) std::cin >> H[n];
std::stack<int> st;
std::stack<int> ans;
for (int n = (N - 1); 0 <= n; n--) {
ans.push(st.size());
while (0 < st.size()) {
int tmp = st.top();
if (tmp < H[n]) st.pop();
else break;
}
st.push(H[n]);
}
while (0 < ans.size()) {
std::cout << ans.top() << " ";
ans.pop();
}
std::cout << std::endl;
}


コメント