F 同一字符的字典序怎么看?
想了一个小时,硬是没想出来,心态崩了 qwq
我的 F 的代码如下:
#include <bits/stdc++.h> #define L(i, a, b) for(int i = (a); i <= (b); i++) #define R(i, a, b) for(int i = (a); i >= (b); i--) #define ll long long using namespace std; const int N = 1e6 + 10; char s[N]; map<char, int> cnt; int n, k, pos; void solve() { cnt.clear(); cin >> n >> k >> s+1; pos = 0; for(int i = 1; i <= n; i++) cnt[s[i]]++; for(auto x : cnt) { if(k > x.second) k -= x.second; else { priority_queue<pair<int, int>> q; int lst = 0; L(j, 1, n) if(s[j] == x.first) { if(lst) { q.push({-s[lst+1], lst}); } lst = j; } q.push({-x.first, lst}); while(--k) q.pop(); pos = q.top().second; break; } } cout << s[pos]; for(int i = 1; i <= n; i++) if(i ^ pos) cout << s[i]; cout << '\n'; } int main() { // freopen("f.in", "r", stdin); // freopen("f.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while(T--) solve(); return 0; }