new file: P5788.cpp
This commit is contained in:
1 parent
c622586277
commit
06fa6530a0
2 files changed
+71
No files matched your search
@@ -0,0 +1,19 @@
|
|||||||
|
#include<bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
int n;
|
||||||
|
int a[3000005];
|
||||||
|
int stk[3000005];
|
||||||
|
int top = 0;
|
||||||
|
int ans[3000005];
|
||||||
|
int main() {
|
||||||
|
scanf("%d", &n);
|
||||||
|
for(int i = 1; i <= n; ++i) {
|
||||||
|
scanf("%d", &a[i]);
|
||||||
|
}
|
||||||
|
for(int i = n; i >= 1; --i) {
|
||||||
|
while(top > 0 && a[stk[top]] <= a[i]) top--;
|
||||||
|
stk[++top] = i;
|
||||||
|
ans[i] = stk[top - 1];
|
||||||
|
}
|
||||||
|
for(int i = 1; i <= n; ++i) printf("%d ", ans[i]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
# P5788 【模板】单调栈
|
||||||
|
|
||||||
|
## 题目背景
|
||||||
|
|
||||||
|
模板题,无背景。
|
||||||
|
|
||||||
|
2019.12.12 更新数据,放宽时限,现在不再卡常了。
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
给出项数为 $n$ 的整数数列 $a_{1 \dots n}$。
|
||||||
|
|
||||||
|
定义函数 $f(i)$ 代表数列中第 $i$ 个元素之后第一个大于 $a_i$ 的元素的**下标**,即 $f(i)=\min_{i<j\leq n, a_j > a_i} \{j\}$。若不存在,则 $f(i)=0$。
|
||||||
|
|
||||||
|
试求出 $f(1\dots n)$。
|
||||||
|
|
||||||
|
## 输入格式
|
||||||
|
|
||||||
|
第一行一个正整数 $n$。
|
||||||
|
|
||||||
|
第二行 $n$ 个正整数 $a_{1\dots n}$。
|
||||||
|
|
||||||
|
## 输出格式
|
||||||
|
|
||||||
|
一行 $n$ 个整数表示 $f(1), f(2), \dots, f(n)$ 的值。
|
||||||
|
|
||||||
|
## 输入输出样例 #1
|
||||||
|
|
||||||
|
### 输入 #1
|
||||||
|
|
||||||
|
```
|
||||||
|
5
|
||||||
|
1 4 2 3 5
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### 输出 #1
|
||||||
|
|
||||||
|
```
|
||||||
|
2 5 4 5 0
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## 说明/提示
|
||||||
|
|
||||||
|
【数据规模与约定】
|
||||||
|
|
||||||
|
对于 $30\%$ 的数据,$n\leq 100$;
|
||||||
|
|
||||||
|
对于 $60\%$ 的数据,$n\leq 5 \times 10^3$ ;
|
||||||
|
|
||||||
|
对于 $100\%$ 的数据,$1 \le n\leq 3\times 10^6$,$1\leq a_i\leq 10^9$。
|
||||||
Reference in new issue
Block a user