/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

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

void solve()
{
    int n, k;   cin >> n >> k;

    string s;   cin >> s;

    deque<char> dq;
    map<char,vector<int>> mp;
    for(int i = 0; i < n; i++) {
        dq.push_back(s[i]);
        mp[s[i]].push_back(i);
    }

    if (k >= n){
        sort( s.begin(), s.end() );
        cout << s << '\n';
        return;
    }

    vector<char> ans;
    int l = 0;
    for(auto x : mp){
        char ch = x.first;
        int cnt = x.second.size();
        int mx = cnt;
        deque<pair<char,int>> tmp;
        while( l < n and k and cnt ) {
            if ( dq[l] == ch ) {
                l++;
                continue;
            }
            if ( l > x.second.back() ) break;
            tmp.push_back({dq[l], l});
            l++;
            k--;
            cnt--;
        }
        mx -= cnt;
        
        //return;
        sort( tmp.begin(), tmp.end() );
       // for(auto x : tmp) cout << x;
        //cout << '\n';
        auto idx = x.second;
        while( !idx.empty() ) {
            if ( tmp.empty() ) break;
            s[tmp.back().second] = ch;
            s[idx.back()] = tmp.back().first;
            dq[idx.back()] = tmp.back().first;
            tmp.pop_back();
            idx.pop_back();
        }
        if ( !k ) break;
        if ( l == n ) break;
    }

    for(int i = l; i < n; i++) s[i] = dq[i];
    cout << s << '\n';
}

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

    int t = 1, tc = 0;
    cin >> t;

    while(t--){
    	//cout << "Case " << ++tc << ": ";
    	solve();
    } 
}


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 12:25:01
Judged At
2024-11-11 02:34:38
Judged By
Score
5
Total Time
2ms
Peak Memory
384.0 KiB