Skip to content

Commit 955f8b2

Browse files
committed
Nowcoder: 2025牛客五一集训派对day1
1 parent 2394140 commit 955f8b2

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

Nowcoder/108199A.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @file 108199A.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-05-06
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 100005
14+
#define maxl 105
15+
16+
using tiii = tuple<int, int, int>;
17+
18+
bool mem1;
19+
20+
pair<char*, int> s[maxn];
21+
int pos[maxn], tans[maxn];
22+
23+
bool mem2;
24+
25+
void solve(void) {
26+
int n;
27+
scanf("%d", &n);
28+
for (int i = 1; i <= n; i++) {
29+
s[i].first = (char*)malloc(sizeof(char) * maxl);
30+
scanf("%s", s[i].first);
31+
s[i].second = i;
32+
}
33+
sort(s + 1, s + n + 1, [&](const auto& x, const auto& y) {
34+
int p = 0;
35+
while (x.first[p] == y.first[p]) p++;
36+
return x.first[p] < y.first[p];
37+
});
38+
for (int i = 1; i <= n; i++) pos[s[i].second] = i;
39+
40+
set<tiii> S;
41+
int64_t ans = 0;
42+
for (int l = 1, r; l <= n; l = r + 1) {
43+
r = l;
44+
while (r + 1 <= n && s[l].first[0] == s[r + 1].first[0]) r++;
45+
ans++, S.emplace(l, r, 0);
46+
}
47+
48+
for (int i = n; i; i--) {
49+
tans[i] = ans;
50+
51+
int p = pos[i];
52+
53+
auto it = --S.lower_bound({p + 1, 0, 0});
54+
auto [l, r, d] = *it;
55+
ans--, S.erase(it);
56+
57+
for (int td = d + 1; l < p; td++)
58+
for (int r; s[l].first[td] != s[p].first[td]; l = r + 1) {
59+
r = l;
60+
while (r + 1 < p && s[l].first[td] == s[r + 1].first[td]) r++;
61+
ans++, S.emplace(l, r, td);
62+
}
63+
for (int td = d + 1; p < r; td++)
64+
for (int l; s[r].first[td] != s[p].first[td]; r = l - 1) {
65+
l = r;
66+
while (p < l - 1 && s[l - 1].first[td] == s[r].first[td]) l--;
67+
ans++, S.emplace(l, r, td);
68+
}
69+
}
70+
71+
for (int i = 1; i <= n; i++) printf("%d\n", tans[i]);
72+
73+
return;
74+
}
75+
76+
int main() {
77+
int _ = 1;
78+
while (_--) solve();
79+
80+
return 0;
81+
}

Nowcoder/108199C.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @file 108199C.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-05-06
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
#define maxn 100005
16+
17+
using pii = pair<int, int>;
18+
19+
unordered_set<int> graph[maxn];
20+
vector<int> ngraph[maxn];
21+
set<pii> avai[maxn];
22+
int ans[maxn], dist[maxn], pre[maxn];
23+
bool lck[maxn];
24+
25+
void solve(void) {
26+
int n;
27+
cin >> n;
28+
for (int i = 1; i <= n; i++) avai[n + i].emplace(1, n);
29+
for (int i = 1, m, x; i <= n; i++) {
30+
cin >> m;
31+
while (m--) {
32+
cin >> x;
33+
graph[i].insert(n + x);
34+
35+
auto p = --avai[n + x].lower_bound({i + 1, 0});
36+
auto [l, r] = *p;
37+
avai[n + x].erase(p);
38+
if (l < i) avai[n + x].emplace(l, i - 1);
39+
if (i < r) avai[n + x].emplace(i + 1, r);
40+
}
41+
}
42+
43+
auto avaiLef = [&](int y) -> int {
44+
for (auto [l, r] : avai[y])
45+
for (int x = l; x <= r; x++)
46+
if (ans[x] && !lck[ans[x]]) return x;
47+
return 0;
48+
};
49+
50+
unordered_set<int> rig;
51+
for (int i = 1; i <= n; i++) rig.insert(n + i);
52+
for (int x = 1; x <= n; x++) {
53+
for (auto y : rig)
54+
if (!graph[x].count(y)) {
55+
ans[x] = y, ans[y] = x, rig.erase(y);
56+
break;
57+
}
58+
if (ans[x]) continue;
59+
60+
for (auto y : graph[x]) lck[y] = true;
61+
62+
for (auto i : graph[x])
63+
for (auto j : graph[x])
64+
if (ans[i] && i != j && !graph[ans[i]].count(j)) ngraph[i].push_back(j);
65+
66+
for (auto y2 : graph[x]) {
67+
int x1 = avaiLef(y2);
68+
if (!x1) continue;
69+
int y1 = ans[x1];
70+
71+
queue<int> que;
72+
que.push(y2);
73+
for (auto y : graph[x]) dist[y] = INT_MAX;
74+
dist[y2] = pre[y2] = 0;
75+
while (!que.empty()) {
76+
int p = que.front();
77+
que.pop();
78+
for (auto q : ngraph[p])
79+
if (dist[q] > dist[p] + 1) dist[q] = dist[p] + 1, pre[q] = p, que.push(q);
80+
}
81+
82+
int y = 0;
83+
for (auto ty : graph[x])
84+
if (!ans[ty] && dist[ty] != INT_MAX) y = ty;
85+
if (!y) continue;
86+
87+
vector<int> lnk;
88+
while (y) lnk.push_back(y), y = pre[y];
89+
lnk.push_back(y1);
90+
reverse(lnk.begin(), lnk.end());
91+
for (int i = lnk.size() - 1; i; i--) {
92+
int x = ans[lnk[i - 1]], y = lnk[i];
93+
ans[x] = y, ans[y] = x;
94+
}
95+
ans[x] = lnk[0], ans[lnk[0]] = x;
96+
rig.erase(lnk.back());
97+
98+
break;
99+
}
100+
101+
if (!ans[x]) return cout << -1 << endl, void();
102+
103+
for (auto y : graph[x]) lck[y] = false, ngraph[y].clear();
104+
}
105+
106+
for (int i = 1; i <= n; i++) cout << ans[i] - n << ' ';
107+
cout << endl;
108+
109+
return;
110+
}
111+
112+
int main() {
113+
ios::sync_with_stdio(false), cin.tie(nullptr);
114+
115+
int _ = 1;
116+
while (_--) solve();
117+
118+
return 0;
119+
}

0 commit comments

Comments
 (0)