This commit is contained in:
2024-04-18 18:38:55 +08:00
parent bfcbb1996a
commit 4f8f929242
20 changed files with 3408 additions and 0 deletions

36
ACMOJ-2163.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
typedef long double LDB;
const int kMaxN = 1e5 + 10;
int n, key[kMaxN], w[kMaxN];
int stk[kMaxN], ls[kMaxN], rs[kMaxN];
void dfs(int rt) {
printf("%d ", rt);
if (ls[rt]) dfs(ls[rt]);
if (rs[rt]) dfs(rs[rt]);
}
int main() {
#ifdef local
freopen("pro.in", "r", stdin);
#endif
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &key[i]);
w[key[i]] = i;
}
int top = 0;
for (int i = 1; i <= n; i++) {
int k = top;
while (k > 0 && w[stk[k]] > w[i]) k--;
if (k) rs[stk[k]] = i; // rs代表笛卡尔树每个节点的右儿子
if (k < top) ls[i] = stk[k + 1]; // ls代表笛卡尔树每个节点的左儿子
stk[++k] = i;
top = k;
}
dfs(stk[1]);
return 0;
}