Advertisement
BaoJIaoPisu

Untitled

Oct 30th, 2022
869
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 1 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33.     bool minimize(X &x, const Y &y) {
  34.         if (x > y)
  35.         {
  36.             x = y;
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. template<class X, class Y>
  42.     bool maximize(X &x, const Y &y) {
  43.         if (x < y)
  44.         {
  45.             x = y;
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54.     void add(X &x, const Y &y) {
  55.         x = (x + y);
  56.         if(x >= MOD) x -= MOD;
  57.     }
  58.  
  59. template<class X, class Y>
  60.     void sub(X &x, const Y &y) {
  61.         x = (x - y);
  62.         if(x < 0) x += MOD;
  63.     }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e9;
  68. const int N = 1e5 + 10;
  69. const int S = 330;
  70.  
  71. int a[N], best[N], cnt[N], ans[N];
  72. bool c[N];
  73. int f[S][N];
  74.  
  75. void solve() {
  76.     int n, k; cin >> n >> k;
  77.    
  78.     int num = 330;
  79.     int block = (n + num - 1) / num;
  80.     for(int i = 0; i < n; i++) cin >> a[i];
  81.  
  82.     a[n] = -INF;
  83.     for(int i = 1; i <= block; i++) {
  84.         int l = (i - 1) * num, r = min(l + num - 1, n - 1);
  85.         for(int j = 0; j < k; j++) f[i][j] = n;
  86.         for(int j = l; j <= r; j++) {
  87.             if(a[f[i][j % k]] < a[j]) f[i][j % k] = j;
  88.         }
  89.  
  90.         best[i] = f[i][0];
  91.     }
  92.  
  93.     for(int rep = 1; rep <= n; rep++) {
  94.         int pos = 1;
  95.         for(int i = 1; i <= block; i++) {
  96.             if(a[best[i]] > a[best[pos]]) pos = i;
  97.         }
  98.  
  99.         ans[rep] = a[best[pos]];
  100.         for(int i = pos + 1; i <= block; i++) {
  101.             ++cnt[i]; cnt[i] %= k;
  102.             best[i] = f[i][cnt[i]];
  103.         }
  104.  
  105.         int l = (pos - 1) * num, r = min(l + num - 1, n - 1);
  106.         int d = 0;
  107.         for(int i = l; i <= r; i++) {
  108.             if(!c[i]) {
  109.                 f[pos][(i - d) % k] = n;
  110.             } else ++d;
  111.         }
  112.         d = 0;
  113.         c[best[pos]] = true;
  114.         for(int i = l; i <= r; i++) {
  115.             if(!c[i]) {
  116.                 if(a[f[pos][(i - d) % k]] < a[i]) f[pos][(i - d) % k] = i;
  117.             } else ++d;
  118.         }
  119.         best[pos] = f[pos][cnt[pos]];
  120.     }
  121.  
  122.     for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  123. }
  124.  
  125. int main()
  126. {
  127.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  128.     #ifndef ONLINE_JUDGE
  129.     freopen("input.txt", "r", stdin);
  130.     freopen("output.txt", "w", stdout);
  131.     #else
  132.     //online
  133.     #endif
  134.  
  135.     int tc = 1, ddd = 0;
  136.     // cin >> tc;
  137.     while(tc--) {
  138.         //ddd++;
  139.         //cout << "Case #" << ddd << ": ";
  140.         solve();
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement