/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 392.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 4ms 888.0 KiB
#6 Accepted 4ms 880.0 KiB
#7 Wrong Answer 5ms 932.0 KiB
#8 Accepted 7ms 928.0 KiB
#9 Accepted 7ms 976.0 KiB
#10 Wrong Answer 7ms 884.0 KiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2025-08-29 11:00:07
**/

#include<bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) 42
#endif
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = 2e9;

#define int long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define clg(x) (32 - __builtin_clz(x))
#define Testcase_Handler int tts, tc = 0; cin >> tts; hell: while(tc++ < tts)
#define uniq(v) sort(all(v)), v.resize(distance(v.begin(), unique(v.begin(), v.end())))
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {bool first = true;for(auto &x : a) {if(!first) out << ' ';first = false;out << x;}return out;};

namespace Dark_Lord_Binoy {
inline void init() {
    ios::sync_with_stdio(false); 
    cin.tie(nullptr);

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
}

int32_t main() {
    Dark_Lord_Binoy::init();

    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;

    vector<int> f(26, 0);
    int i = 0, j = 0, last = -1;
    while(i < n) {
        int r = i + k - 1;
        if(r >= n - 1) {
            int gap = n - last;
            if(gap >= k) {
                r = n - 1;
                sort(s.begin() + i, s.begin() + r + 1);
            }
            break;
        }

        // build frequency for substring [i .. r]
        while(j < r) {
            f[s[j] - 'a']++;
            j++;
        }
        f[s[j] - 'a']++;
        
        // check if there exists a smaller character than s[i]
        int cur = s[i] - 'a';
        bool exists = false;
        for (int id = 0; id < cur; id++) {
            exists |= (f[id] > 0);
        }

        if(exists) {
            // sorting this window is optimal
            last = r;
            sort(s.begin() + i, s.begin() + r + 1);
            while(i <= r) {
                f[s[i] - 'a']--;
                i++;
            }
        }
        else {
            f[cur]--;
            i++;
        }
    }
    cout << s << '\n';

    dbg(_Time_);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1230 Lexicographically Smallest Rearrangement
Contest
Testing - Intra LU Programming contest 25
Language
C++17 (G++ 13.2.0)
Submit At
2025-08-30 20:21:17
Judged At
2025-08-30 20:21:17
Judged By
Score
8
Total Time
7ms
Peak Memory
976.0 KiB