Anton has a positive integer nn, however, it quite looks like a mess, so he wants to make it beautiful after kk swaps of digits.
Let the decimal representation of nn as (x1x2⋯xm)10(x1x2⋯xm)10 satisfying that 1≤x1≤91≤x1≤9, 0≤xi≤90≤xi≤9 (2≤i≤m)(2≤i≤m), which means n=∑mi=1xi10m−in=∑i=1mxi10m−i. In each swap, Anton can select two digits xixi and xjxj (1≤i≤j≤m)(1≤i≤j≤m) and then swap them if the integer after this swap has no leading zero.
Could you please tell him the minimum integer and the maximum integer he can obtain after kk swaps?
Let the decimal representation of nn as (x1x2⋯xm)10(x1x2⋯xm)10 satisfying that 1≤x1≤91≤x1≤9, 0≤xi≤90≤xi≤9 (2≤i≤m)(2≤i≤m), which means n=∑mi=1xi10m−in=∑i=1mxi10m−i. In each swap, Anton can select two digits xixi and xjxj (1≤i≤j≤m)(1≤i≤j≤m) and then swap them if the integer after this swap has no leading zero.
Could you please tell him the minimum integer and the maximum integer he can obtain after kk swaps?
Input
The first line contains one integer T indicating the number of test cases.
Each of the following T lines describes a test case and contains two space-separated integers n and k.
1≤T≤1001≤T≤100, 1≤n,k≤1091≤n,k≤109.
Output
For each test case, print in one line the minimum integer and the maximum integer which are separated by one space.
Sample Input
5 12 1 213 2 998244353 1 998244353 2 998244353 3
Sample Output
21 is 12 is 123 321 298 944 353 998 544 323 238 944 359 998 544 332 233 944 859 998 544 332
meaning of problems
to exchange a number N every two digit positions
after the exchange of packets can be K times the maximum and minimum
solution
to be separated you honestly opening array for writing data structure,
but the mind ribs pumping method chosen to implement arithmetic operations exchange sites.
I next time do not blind the chicken scrawl.
Probably thinking
from the high to the low scan,
comparing the current position with the maximum / minimum,
if necessary exchange of words,
each time the number of legal (the first non-zero) maximum / minimum number mentioned in the current position.
Error Code (wa still do not know where, do not want changed)
1 #include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstring> 4 #include <stack> 5 #include <cstdlib> 6 #include <queue> 7 #include <cmath> 8 #include <cstdio> 9 #include <algorithm> 10 #include <string> 11 #include <vector> 12 #include <list> 13 #include <iterator> 14 #include <set> 15 #include <map> 16 #include <utility> 17 #include <iomanip> 18 #include <ctime> 19 #include <sstream> 20 #include <bitset> 21 #include <deque> 22 #include <limits> 23 #include <numeric> 24 #include <functional> 25 #include <ctime> 26 27 #define gc getchar() 28 #define mem(a) memset(a,0,sizeof(a)) 29 #define mod 1000000007 30 #define sort(a,n,int) sort(a,a+n,less<int>()) 31 #define fread() freopen("in.in","r",stdin) 32 #define fwrite() freopen("out.out","w",stdout) 33 using namespace std; 34 35 typedef long long ll; 36 typedef char ch; 37 typedef double db; 38 39 int gcd(int a,int b){ 40 if(a<b)swap(a,b); 41 if(a%b==0)return b; 42 else gcd(b,a%b); 43 } 44 45 double random(double start,double end) 46 { 47 return start + (end - start) * rand() / (RAND_MAX + 1.0); 48 } 49 const int maxn = 100+10; 50 const int INF = 0x3f3f3f3f; 51 double dis(double x1,double y1){ 52 return sqrt(x1 * x1 + y1 * y1); 53 } 54 55 int main() 56 { 57 ll t = 0; 58 cin >> t; 59 while(t--) 60 { 61 unsigned long long a = 0 , k = 0; 62 unsigned long long res = 0; 63 cin >> a >> k; 64 //cout<<t<<endl;a = t;k = 2; 65 res = a; 66 unsigned long long a1 = a; 67 int j = 0; 68 for(int i = 0 , counter = 0;counter<k;) 69 { 70 unsigned long long s = a; 71 int min = 9; 72 int p = -1; 73 for(j = 0;s/10 != 0;j++)s/=10; 74 //cout<<j<<endl; // 75 if(j==i)break; 76 if(j==1) 77 { 78 if(a%10<a/10 && a%10!=0) 79 res = (a%10) * 10 + a/10; 80 else res = a; 81 break; 82 } 83 s = a; 84 for(int l = 0;l<j-i;l++) 85 { 86 if(s % 10 < min) 87 { 88 if(i!=0 || s % 10!=0) 89 { 90 min = s % 10; 91 p = l; 92 } 93 } 94 s /= 10; 95 } 96 if(p==-1) 97 { 98 res = a; 99 break; 100 } 101 //cout<<i<<' '<<counter<<endl;// 102 //cout<<p<<endl; // 103 s = a; 104 int m = 0,n = 0; 105 int k = 0; 106 107 for(k = 0;k <=j-i;k++) 108 { 109 if(k == p) 110 { 111 m = s % 10; 112 } 113 if(k == j-i) 114 { 115 n = s % 10; 116 } 117 s/=10; 118 } 119 //cout<<m<<' '<<n<<endl; // 120 if(n <= m) 121 { 122 i++; 123 continue; 124 } 125 int mul = 1; 126 for(k = 0;k <=j-i;k++) 127 { 128 if(k == p) 129 { 130 a = a- m*mul +n*mul; 131 } 132 if(k == j-i) 133 { 134 a = a- n*mul +m*mul; 135 } 136 mul*=10; 137 } 138 res = a; 139 //cout<<a<<endl; // 140 i++; 141 counter++; 142 } 143 cout<<res<<' '; 144 a = a1; 145 res = a; 146 for(int i = 0 , counter = 0;counter<k;) 147 { 148 if(i>j) 149 { 150 res = a; 151 break; 152 } 153 ll s = a; 154 int max = 0; 155 int p = 0; 156 for(j = 0;s/10 != 0;j++)s/=10; 157 //cout<<j<<endl; // 158 if(j==i)break; 159 if(j==1) 160 { 161 if(a%10>a/10 && a%10!=0) 162 res = (a%10)*10+a/10; 163 else res = a; 164 break; 165 } 166 167 s = a; 168 for(int l = 0;l<j-i;l++) 169 { 170 if(s % 10 > max) 171 { 172 max = s % 10; 173 p = l; 174 } 175 s /= 10; 176 } 177 //cout<<p<<endl; // 178 s = a; 179 int m = 0,n = 0; 180 int k = 0; 181 //cout<<s<<endl; // 182 for(k = 0;k <=j-i;k++) 183 { 184 if(k == p) 185 { 186 m = s % 10; 187 } 188 if(k == j-i) 189 { 190 n = s % 10; 191 } 192 s/=10; 193 } 194 //cout<<m<<' '<<n<<endl; // 195 if(n >= m) 196 { 197 i++; 198 continue; 199 } 200 int mul = 1; 201 for(k = 0;k <=j-i;k++) 202 { 203 if(k == p) 204 { 205 a = a- m*mul +n*mul; 206 } 207 if(k == j-i) 208 { 209 a = a- n*mul +m*mul; 210 } 211 mul*=10; 212 } 213 res = a; 214 i++; 215 counter++; 216 } 217 cout<<res<<endl; 218 219 } 220 return 0; 221 }