NYOJ 1057 Max search (3) (Greedy)
Description
Given an integer N, you can move the numbers on two adjacent digits at most K times to get a new integer.
Evaluate the maximum value of the new integer.
-
Enter multiple groups of test data.
Each group of test data occupies one row, with two numbers of N and K (1 ≤ limit N ≤ limit 10 ^ 18; 0 ≤ limit K ≤ limit 100 ). the output of each group of test data occupies one row, and the output moves to the maximum value of the new integer. Sample Input
1990 1100 09090000078001234 6
Sample output
91901009907000008001234
Question Analysis:
Greedy Algorithm, but greedy for each step, it should be in the range of k steps to find the maximum value, exchange forward in turn, if found the maximum value, but not to k step, then we will continue to look for the next kk step from the current optimum.
AC code:
# Include
# Include
# Include
# Include
Using namespace std; int main () {long n; int k; while (scanf (% lld % d, & n, & k )! = EOF) {char s [20]; sprintf (s, % lld, n); int len = strlen (s); int k1, k2, I, j; k1 = k2 = 0; while (k) {// cout <
S [k2]) {k2 = I ;}// cout <
K1) {for (j = k2; j> k1; j --) {// the largest value found, swap (s [j], s [J-1]);} k-= (k2-k1); // number of remaining transformations} k1 ++; // modify the pointer to k2 = k1;} printf (% s, s);} return 0 ;}