时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 256M,其他语言512M
小Q在周末的时候和他的小伙伴来到大城市逛街,一条步行街上有很多高楼,共有n座高楼排成一行。
小Q从第一栋一直走到了最后一栋,小Q从来都没有见到这么多的楼,所以他想知道他在每栋楼的位置处能看到多少栋楼呢?(当前面的楼的高度大于等于后面的楼时,后面的楼将被挡住)
输入描述:
输入第一行将包含一个数字n,代表楼的栋数,接下来的一行将包含n个数字wi(1<=i<=n),代表每一栋楼的高度。
1<=n<=100000;
1<=wi<=100000;
输出描述:
输出一行,包含空格分割的n个数字vi,分别代表小Q在第i栋楼时能看到的楼的数量。
输入例子1:
6 5 3 8 3 2 5
输出例子1:
3 3 5 4 4 4
我的解答:
#include <iostream>
#include <string>
using namespace std;
#include <stack>
#include<vector>
int main()
{
int n;
cin >> n;
int x[100001];
for (int i = 0; i < n; i++)
{
cin >> x[i];
}
vector<int> a, b;
stack<int> s1, s2;
for (int i = 0, j = n - 1; i < n, j>=0; i++, j--)
{
a.push_back(s1.size());
b.push_back(s2.size());
while(!s1.empty() && s1.top() <= x[i])
{
s1.pop();
}
while(!s2.empty() && s2.top() <= x[j])
{
s2.pop();
}
s1.push(x[i]);
s2.push(x[j]);
}
for (int i = 0; i < n; i++)
{
cout << a[i] + b[n - 1 - i]+1 << " ";
}
system("pause");
return 0;
}