Wrong Answer
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