/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 1.27 MiB
#2 Accepted 2ms 1.27 MiB
#3 Accepted 2ms 1.203 MiB
#4 Wrong Answer 2ms 1.27 MiB
#5 Accepted 54ms 22.215 MiB
#6 Accepted 53ms 22.215 MiB
#7 Accepted 54ms 22.215 MiB
#8 Accepted 54ms 22.145 MiB
#9 Accepted 53ms 22.215 MiB
#10 Accepted 54ms 22.262 MiB
#11 Accepted 2ms 1.27 MiB
#12 Accepted 2ms 1.27 MiB
#13 Accepted 2ms 1.309 MiB
#14 Accepted 1ms 532.0 KiB
#15 Accepted 2ms 1.09 MiB
#16 Accepted 2ms 1.297 MiB
#17 Accepted 2ms 1.27 MiB
#18 Accepted 2ms 1.102 MiB
#19 Accepted 2ms 1.27 MiB
#20 Wrong Answer 3ms 1.062 MiB

Code

#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 9;
int lg[N];
void Preprocess() {
  for (int i = 2; i < N; ++i) {
    lg[i] = lg[i / 2] + 1;
  }
}
template <class T> struct RMQ {
  int n = 1, LOG = 1;
  vector<vector<T>> st;
  T Merge(T& a, T& b) {
    return min(a, b);
  }
  RMQ() {}
  RMQ(vector<T>& a) {
    if (lg[2] == 0) Preprocess();
    n = (int)a.size(), LOG = __lg(n) + 1;
    st.assign(n, vector<T> (LOG));
    for (int j = 0; j < LOG; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        if (j == 0) st[i][j] = a[i];
        else st[i][j] = Merge(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
      }
    }
  }
  T Query(int l, int r) {
    int k = lg[r - l + 1];
    return Merge(st[l][k], st[r - (1 << k) + 1][k]);
  }
};

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  string a;
  cin >> a;
  if (k == 1) {
    cout << a << '\n';
    return 0;
  }
  a = "#" + a;
  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    b[i] = a[i] - 'a';
  }
  RMQ rmq(b);
  for (int i = 1, j = k; j <= n; i++, j++) {
    if (b[i] > rmq.Query(i + 1, j)) {
      sort(a.begin() + i, a.begin() + j + 1);
      i += k - 1;
      j += k - 1;
    }
  }
  a.erase(0, 1);
  cout << a << '\n';
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1230 Lexicographically Smallest Rearrangement
Contest
LUCC Presents Intra LU Junior Programming Contest - Replay
Language
C++17 (G++ 13.2.0)
Submit At
2025-09-02 17:10:51
Judged At
2025-09-02 17:10:51
Judged By
Score
18
Total Time
54ms
Peak Memory
22.262 MiB