/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define bug(a) cout<< #a << " : " << a <<endl;
#define bug2(a, b) cout<< #a << " : " << a  << " " << #b << " : " << b << endl;

void solve(int cs){
    int n,m;cin>>n>>m;
    string s;cin>>s;
    map<char,set<int>>mp;
    for(int i=0;i<n;i++){
        mp[s[i]].insert(i);
    }
    // for(auto i:mp){
    //     for(auto j:i.second)cout<<j<<' ';
    //         cout<<endl;
    // }
    string tmp=s;

    sort(tmp.begin(),tmp.end());
    // cout<<tmp<<endl;
    int i=0;
    while(m and i<n){
        if(s[i]!=tmp[i]){
            auto it = --mp[tmp[i]].end();
            // bug(x)
            int x=*it;
            int a=i;
            mp[s[i]].erase(i);
            mp[tmp[i]].erase(x);
            swap(s[i],s[x]);
            m--;
            // mp[s[i]]
            mp[s[i]].insert(i);
            mp[s[x]].insert(x);
        }
        i++;
    }
    cout<<s<<endl;
}


int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    cout.tie(0);

    int tc = 1;
    cin >> tc;

    for(int cs = 1; cs <= tc; cs++){
        solve(cs);
    }
}

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:37:00
Judged At
2024-11-11 02:35:16
Judged By
Score
5
Total Time
2ms
Peak Memory
324.0 KiB