/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 5ms 652.0 KiB
#5 Accepted 160ms 10.285 MiB
#6 Accepted 54ms 1.543 MiB
#7 Accepted 140ms 10.309 MiB
#8 Accepted 127ms 10.305 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'
int main() {
    int t; cin >> t;
    while(t--){
        ll n,k; string s, t, ans; cin >> n >> k >> s;
        t = s;
        ans = s;
        sort(t.begin(), t.end());
        map<char, set<int>> mp, lagbe;
        for(int i=0;i<n;++i) mp[s[i]].insert(i);
        for(int i=0;i<n;++i) lagbe[t[i]].insert(i);
        for(auto [c, v]: lagbe){
            int mx = *v.rbegin();
            while(mp[c].size() and *mp[c].begin() <=mx){
                mp[c].erase(*mp[c].begin());
            }
            vector<int> sw;
            vector<char> chr;
            int cnt = 0;
            while(mp[c].size() and k){
                sw.push_back(*mp[c].rbegin());
                mp[c].erase(*mp[c].rbegin());
                --k;
                cnt++;
            }
            for(int i=*v.begin();i<=*v.rbegin() and cnt;++i)
                if(s[i]!=c)
                    chr.push_back(s[i]),mp[s[i]].erase(i),cnt--, s[i] = c;
            sort(sw.begin(), sw.end());
            sort(chr.begin(), chr.end());
            for(int i=0;i<sw.size();++i) {
                s[sw[i]] = chr[i];
                mp[s[sw[i]]].insert(sw[i]);
            }
            // cerr << s << nl;
        }
        cout << s << nl;
    }
}

Information

Submit By
Type
Submission
Problem
P1058 Lexicographically Smallest String
Contest
Sylhet ICPC 2024 Collaborative Challenge: Episode 2
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-30 11:28:48
Judged At
2024-11-11 02:34:54
Judged By
Score
100
Total Time
160ms
Peak Memory
10.309 MiB