'commit'
This commit is contained in:
36
TangDou/DanDiaoZhan/Right_Big.cpp
Normal file
36
TangDou/DanDiaoZhan/Right_Big.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
//https://www.ixigua.com/6925435220292436494
|
||||
|
||||
using namespace std;
|
||||
/**
|
||||
输入数据:
|
||||
7
|
||||
50 30 10 5 3 1 20
|
||||
|
||||
输出数据:
|
||||
-1 -1 20 20 20 20 -1
|
||||
*/
|
||||
int n;
|
||||
stack<int> stk; //装的是序号
|
||||
const int N = 1010;
|
||||
int a[N]; //a[stk.top()]是指栈顶元素的值
|
||||
int res[N]; //装的是序号 a[res[i]]是真正的数值
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++)cin >> a[i];
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
//a[i]右侧元素,栈顶元素找到了右侧比自己大的最近数据
|
||||
while (!stk.empty() && a[stk.top()] <= a[i]) {
|
||||
res[stk.top()] = i;
|
||||
stk.pop();
|
||||
}
|
||||
stk.push(i);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (res[i])cout << a[res[i]] << " ";
|
||||
else cout << "-1 ";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user