Description
Input
Output
Sample Input
2
1432 2
4321 2
Sample Output
4312
4321
Data Constraint
算法讨论
不难发现,交换的位置越靠前,贡献也就越大,所以越靠前就越优先,对于第i位,与所剩的k的次数内能交换的最大的数交换,剩下的留给后面。
var
x:array[1..10000] of string;
n,k,i,j,l,e,w:longint;
max:string;
ch:char;
begin
assign(input,'swap.in');reset(input);
assign(output,'swap.out');rewrite(output);
readln(n);
for i:=1 to n do
begin
read(ch);l:=1;
fillchar(x,sizeof(x),0);
while ch<>' ' do
begin
x[l]:=ch;
inc(l);
read(ch);
end;
readln(k);
for j:=1 to l-1 do
begin
max:='';
for e:=j+1 to j+k do
begin
if x[e]>max then
begin max:=x[e];w:=e;end;
if e>l-1 then break;
end;
if max<=x[j] then continue;
k:=k-w+j;
for e:=w downto j+1 do
x[e]:=x[e-1];
x[j]:=max;
if k=0 then break;
end;
for j:=1 to l-1 do
write(x[j]);
writeln;
end;
close(input);close(output);
end.