/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Wrong Answer 1ms 320.0 KiB
#3 Wrong Answer 2ms 324.0 KiB

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;
        for(int i=0;i<n;++i) mp[s[i]].insert(i);
        for(int i=0;i<n;++i) {
            if(!k){
                ans[i] = s[i];
                continue;
            }
            if(s[i] == t[i]){
                mp[s[i]].erase(i);
                ans[i] = s[i];
                continue;
            }
            k--;
            int lst = *mp[t[i]].rbegin();
            mp[t[i]].erase(lst);
            mp[s[i]].insert(lst);
            swap(s[i], s[lst]);
            ans[i] = s[i];
        }
        cout << ans << 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 09:33:15
Judged At
2024-11-11 02:35:19
Judged By
Score
5
Total Time
2ms
Peak Memory
324.0 KiB