经典的题目
#include <stdio.h> #include <stack> using namespace std; int data[80000]; int N; long long sum; stack<int> s; void getMax() { s.push(data[0]); for(int i = 1; i <= N-1; i++) { // how many data can see data[i] (greater than data[i])? // they are all in the stack! while(!s.empty() && s.top() <= data[i]) { s.pop(); } sum += (long long)s.size(); s.push(data[i]); } printf("%lld/n", sum); } int main() { scanf("%d", &N); for(int i = 0; i < N; i++) { scanf("%d", &data[i]); } getMax(); }
本文介绍了一个使用栈解决的经典算法问题:如何计算每个元素能看到多少个比它大的元素。通过遍历数组并利用栈的数据结构特性,高效地解决了这一问题。
3850

被折叠的 条评论
为什么被折叠?



