Untitled
unknown
plain_text
a year ago
2.5 kB
2
Indexable
Never
package Earning_Biggest_Prize_Money_2; import java.io.FileInputStream; import java.util.Scanner; class Solution { static int result; static int[] a, b; static int n, k; public static boolean like() { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) if (a[i] == a[j]) { return true; } } return false; } public static boolean sorted() { for (int i = 0; i < n - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } public static int findMa(int i) { int vt = i, max = a[i], sw = a[i]; for (int j = i + 1; j < n; j++) { if (a[j] >= max ) { max = a[j]; vt = j; sw++; } } return vt; } public static int findMb(int i) { int vt = i, max = a[i]; for (int j = i + 1; j < n; j++) { if (a[j] > max && b[j] == 1) { max = a[j]; vt = j; } } return vt; } public static void bonus() { for (int i = 0; i < n - 1; i++) { int vt = findMa(i); if (vt != i && a[i] != a[vt]) { int x = a[vt]; a[vt] = a[i]; a[i] = x; b[vt] = 1; k--; // System.out.println("swap " + i + " " + vt + " : " + a[i] + " " + a[vt]); // for(int m = 0; m < n; m++){ // System.out.print(a[m]); // } // System.out.println(); } if (sorted()) { // System.out.println("sorteddddddddddd"); break; } if (k == 0) { // System.out.println("kkkkkkkkkkk"); break; } } if (!sorted()) { for (int i = 0; i < n - 1; i++) { if (b[i] == 1) { int vt = findMb(i); if (vt != i) { int x = a[i]; a[i] = a[vt]; a[vt] = x; b[i] = 0; } } } } if (k % 2 == 1 && !like()) { int x = a[n - 2]; a[n - 2] = a[n - 1]; a[n - 1] = x; } for (int i = 0; i < n; i++) { result = result * 10 + a[i]; } } public static void main(String args[]) throws Exception { System.setIn(new FileInputStream("input.txt")); Scanner sc = new Scanner(System.in); int T; T = Integer.parseInt(sc.nextLine()); for (int test_case = 1; test_case <= T; test_case++) { String[] s = sc.nextLine().split(" "); n = s[0].length(); a = new int[n]; b = new int[n]; for (int i = 0; i < n; i++) { a[i] = s[0].charAt(i) - '0'; } k = Integer.parseInt(s[1]); result = 0; bonus(); System.out.println("Case #" + test_case + "\n" + result); } sc.close(); } }