-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC435B.java
More file actions
73 lines (55 loc) · 1.49 KB
/
Copy pathC435B.java
File metadata and controls
73 lines (55 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class C435B {
/***/
public static void main(String[] args) {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
try{
String[] s=reader.readLine().split(" ", 2);
int swaps=Integer.valueOf(s[1].trim()).intValue();
int[] n=new int[s[0].trim().length()];
for(int i=0;i!=n.length;i++){
n[i]=Integer.valueOf(s[0].substring(i, i+1)).intValue();
// System.err.print(n[i]);
}
// System.err.println();
int p=0;
while((swaps>0)&&(p<n.length)){
// System.err.println("p="+p);
if(n[p]<9){
int[] sup=new int[10];
for(int i=0;i!=sup.length;i++)
sup[i]=-1;
for(int i=(p+1);i<n.length;i++){
if(sup[n[i]]==-1){
sup[n[i]]=i;
// System.err.println(n[i]+" "+sup[n[i]]);
}
}
//
// System.err.println("Test");
boolean bDone=false;
int nSup=9;
while(!bDone&&(nSup>n[p])){
if((sup[nSup]>0)&&((sup[nSup]-p)<=swaps)){
// System.err.println("nSup="+nSup+" "+(sup[nSup]-p)+" "+swaps);
// Swaps
for(int j=sup[nSup];j>p;j--)
n[j]=n[j-1];
n[p]=nSup;
swaps-=(sup[nSup]-p);
bDone=true;
}
nSup--;
}
}
p++;
}
/**/
for(int i=0;i!=n.length;i++)
System.out.print(n[i]);
System.out.println();
reader.close();
}catch(Exception e){}
}
}