A 数列下标

题目地址:

https://ac.nowcoder.com/acm/contest/6290/A

基本思路:

找离一个位置最近的比他大的数的位置,比较明显的一个单调栈,
不懂单调栈的同学可以去学习一下,就是在入栈之前保证栈的单调性就可以了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF (int)1e18

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn = 10100;
int n,a[maxn],b[maxn];
signed main() {
  IO;
  vector<int> v;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  per(i, n, 1) {
    while (!v.empty() && a[v.back()] <= a[i]) v.pop_back();
    if (!v.empty()) b[i] = v.back();
    else b[i] = 0;
    v.push_back(i);
  }
  rep(i, 1, n) cout << b[i] << ' ';
  cout << '\n';
  return 0;
}