Skip to content

Commit 8b1ba5a

Browse files
committed
Luogu: 11530
[THUPC2025 初赛] 峰回路转
1 parent 316086c commit 8b1ba5a

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Luogu/11530.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @file 11530.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-12-15
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 1000005
20+
21+
int a[maxn], pos[maxn];
22+
23+
vector<int> graph[maxn], son[maxn];
24+
string ans[maxn];
25+
int dep[maxn], conn[maxn];
26+
27+
void dfs(int p) {
28+
for (auto i : graph[p]) dfs(i);
29+
30+
vector<tuple<int, int, int>> rec;
31+
32+
int d = 0, lst = 0;
33+
for (vector<int>::iterator l = son[p].begin(), r, c = graph[p].begin(); l != son[p].end(); l = r + 1) {
34+
r = l;
35+
while (r + 1 != son[p].end() && *(r + 1) == *r - 1 && a[*(r + 1)] == a[*r] + 1) r++;
36+
while (c != graph[p].end() && *c > *r) d = max(d, dep[*(c++)]);
37+
if (l != r) {
38+
if (l != son[p].begin()) rec.emplace_back(lst, l - son[p].begin(), d);
39+
for (auto i = l + 1; i <= r; i++) ans[*i] = "*";
40+
lst = r - son[p].begin(), d = 0;
41+
}
42+
}
43+
rec.emplace_back(lst, son[p].size() - 1, d);
44+
45+
for (int x = 0; x < (int)rec.size(); x++) {
46+
int l = get<0>(rec[x]), r = get<1>(rec[x]), d = get<2>(rec[x]);
47+
if (l == r) continue;
48+
dep[p] = max(dep[p], ++d);
49+
for (int i = l; i <= r; i++)
50+
if (i > l) {
51+
ans[son[p][i]] = to_string(d) + "-" + to_string(i - l + 1) + ans[son[p][i]];
52+
for (int j = 0; j < conn[son[p][i]] - 1; j++) ans[son[p][i] + j].push_back('#');
53+
} else
54+
ans[son[p][i] + conn[son[p][i]] - 1] = to_string(d) + "-1" + ans[son[p][i]];
55+
}
56+
return;
57+
}
58+
59+
void solve(void) {
60+
int n;
61+
cin >> n;
62+
for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i, conn[i] = 1;
63+
vector<int> rec;
64+
65+
for (int i = n - 1; i; i--)
66+
if (a[i] + 1 == a[i + 1]) conn[i] += conn[i + 1];
67+
68+
for (int l = 1, r; l <= n; l = r + conn[pos[r]]) {
69+
r = l;
70+
son[pos[l]].push_back(pos[l]);
71+
while (r + conn[pos[r]] <= n && pos[r + conn[pos[r]]] < pos[r]) son[pos[l]].push_back(pos[r += conn[pos[r]]]);
72+
while (!rec.empty() && son[rec.back()].back() > pos[r]) graph[pos[l]].push_back(rec.back()), rec.pop_back();
73+
if (!rec.empty() && rec.back() > pos[r]) return cout << -1 << endl, void();
74+
rec.push_back(pos[l]);
75+
}
76+
77+
for (auto i : rec) dfs(i);
78+
79+
for (int i = 1; i <= n; i++) cout << '.' << ans[i];
80+
cout << endl;
81+
82+
return;
83+
}
84+
85+
bool mem2;
86+
87+
int main() {
88+
ios::sync_with_stdio(false), cin.tie(nullptr);
89+
#ifdef LOCAL
90+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
91+
#endif
92+
93+
int _ = 1;
94+
while (_--) solve();
95+
96+
#ifdef LOCAL
97+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
98+
#endif
99+
return 0;
100+
}

0 commit comments

Comments
 (0)