fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28. typedef pair<int,int> pii;
  29.  
  30. string a;
  31.  
  32. string consistency(int n, int k){
  33.  
  34. priority_queue<pii, vector<pii>, greater<pii>> digits;
  35. priority_queue<pii, vector<pii>, greater<pii>> zeroes;
  36.  
  37.  
  38. string ans = "";
  39.  
  40. int i = 0;
  41. while(i<=k){
  42. if(a[i] == '0') zeroes.push({0,i});
  43. else digits.push({a[i]-'0',i});
  44. i++;
  45. }
  46.  
  47. int lastI = -1;
  48.  
  49. while(i<=n){
  50.  
  51. while(!digits.empty() && digits.top().second <= lastI) digits.pop();
  52. while(!zeroes.empty() && zeroes.top().second <= lastI) zeroes.pop();
  53.  
  54. if(ans.empty()){
  55. auto top = digits.top();
  56. digits.pop();
  57.  
  58. int digit = top.first;
  59. lastI = top.second;
  60.  
  61. ans.push_back(char(digit+'0'));
  62. }
  63. else{
  64. if(!zeroes.empty()){
  65. auto top = zeroes.top();
  66. zeroes.pop();
  67.  
  68. lastI = top.second;
  69.  
  70. ans.push_back('0');
  71. }
  72. else{
  73. auto top = digits.top();
  74. digits.pop();
  75.  
  76. int digit = top.first;
  77. lastI = top.second;
  78.  
  79. ans.push_back(char(digit+'0'));
  80. }
  81. }
  82.  
  83. if(a[i] == '0') zeroes.push({0, i});
  84. else digits.push({a[i]-'0', i});
  85. i++;
  86. }
  87.  
  88. return ans;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. string practice(int n, int k){
  107.  
  108.  
  109. return 0;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. void solve() {
  117.  
  118. int k;
  119. cin >> a >> k;
  120. int n = a.size();
  121.  
  122. cout << consistency(n, k) << endl;
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. int32_t main() {
  132. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  133.  
  134. int t = 1;
  135. cin >> t;
  136. while (t--) {
  137. solve();
  138. }
  139.  
  140. return 0;
  141. }
Success #stdin #stdout 0s 5316KB
stdin
6
89021034561
5
10000
4
1337
0
987654321
6
66837494128
5
7808652
3
stdout
103451
1
1337
321
344128
7052