Advertisement
Dmaxiya

P8686 [蓝桥杯 2019 省 A] 修改数组 参考代码

Mar 5th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 2000000 + 100;
  6. int n;
  7. int fa[maxn], num[maxn];
  8.  
  9. void init() {
  10.     for (int i = 1; i < maxn; ++i) {
  11.         fa[i] = i;
  12.     }
  13. }
  14.  
  15. int findF(int x) {
  16.     return fa[x] == x ? x : fa[x] = findF(fa[x]);
  17. }
  18.  
  19. void union_(int x, int y) {
  20.     fa[findF(x)] = findF(y);
  21. }
  22.  
  23. void del(int x) {
  24.     union_(x, x + 1);
  25. }
  26.  
  27. int main() {
  28. #ifdef ExRoc
  29.     freopen("test.txt", "r", stdin);
  30. #endif // ExRoc
  31.     ios::sync_with_stdio(false);
  32.  
  33.     init();
  34.     cin >> n;
  35.     for (int i = 1; i <= n; ++i) {
  36.         cin >> num[i];
  37.         num[i] = findF(num[i]);
  38.         del(num[i]);
  39.     }
  40.     for (int i = 1; i <= n; ++i) {
  41.         cout << num[i] << " ";
  42.     }
  43.     cout << endl;
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement