Advertisement
Dmaxiya

封印宝石 参考代码

Mar 22nd, 2025
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100000 + 100;
  6. struct Node {
  7.     int a, idx;
  8. };
  9.  
  10. bool operator<(const Node &a, const Node &b) {
  11.     return a.a == b.a ? a.idx < b.idx : a.a > b.a;
  12. }
  13.  
  14. int n, k, idx;
  15. int ans[maxn], a[maxn];
  16. bool vis[maxn];
  17. set<Node> st;
  18. set<Node>::iterator it;
  19.  
  20. int main() {
  21. #ifdef ExRoc
  22.     freopen("test.txt", "r", stdin);
  23. #endif
  24.     ios::sync_with_stdio(false);
  25.  
  26.     cin >> n >> k;
  27.     for (int i = 1; i <= n; ++i) {
  28.         cin >> a[i];
  29.     }
  30.     memset(ans, -1, sizeof(ans));
  31.     for (int i = 1; i <= n; ++i) {
  32.         idx = max(idx, i);
  33.         while (idx <= n && idx - i <= k) {
  34.             if (!vis[idx]) {
  35.                 st.insert({a[idx], idx});
  36.             }
  37.             ++idx;
  38.         }
  39.         while (idx - 1 >= i && idx - 1 - i > k) {
  40.             st.erase({a[idx - 1], idx - 1});
  41.             --idx;
  42.         }
  43.         if (!st.empty()) {
  44.             if (ans[i - 1] == -1 || ans[i - 1] != st.begin()->a) {
  45.                 it = st.begin();
  46.             } else {
  47.                 it = st.upper_bound({ans[i - 1], n});
  48.             }
  49.             if (it != st.end()) {
  50.                 ans[i] = it->a;
  51.                 vis[it->idx] = true;
  52.                 k -= it->idx - i;
  53.                 st.erase(it);
  54.             }
  55.         }
  56.         st.erase({a[i], i});
  57.     }
  58.     for (int i = 1; i <= n; ++i) {
  59.         cout << ans[i] << " ";
  60.     }
  61.     cout << endl;
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement