Choose pretty number - to fight a lot of pen questions (+ greedy violence)

The meaning of problems

Links: https://www.nowcoder.com/questionTerminal/005af31a10834b3688911463065ab47d
Source: Cattle-off network

A mobile phone number by the State and by the N decimal digits (0-9) only. A phone number at least the same K-digit number is defined as pretty. A country's mobile phone number can have leading zeros, such as 000 123 456 is a valid phone number.
Much smaller want to spend to modify their phone number is a pretty number. The amount of a number of digital modification takes for the difference between the new and old digital numbers. For example, the 1 changed to 6 or 6 to 1 to modify need to spend 5 dollars.
Much smaller now given phone number, ask to modify it into a pretty number, at least how much money?
Enter a description:
The first line contains two integers N, K, respectively, and the number of phone number digits Liang K, at least the same numbers. 
The second line contains N characters, each character is a digit ( '0' - '9'), no other whitespace between the numbers. Represent a small number of mobile phone number.
Data range:
2 <= K <= N <= 10000


Output Description:
The first row contains an integer indicating a number Liang modified, requires a minimum amount. 
The second line contains N-numeric characters, represent the least cost to modify a new phone number. If there are a plurality of minimum cost Liang number, the output lexicographically smallest Liang number.
Example 1

Entry

6 5
787585

Export

4
777577

Explanation

Spent four programs in two ways: 777 577 and 777 775, the former lexicographically smaller.

Thinking

Enumeration Each number (0 to 9) the same as the target number i, gradually widen the gap GAP, greedy strategy, to process i + gap, i.e., greater than i, and along the replacement, as it makes smaller lexicographically ; i-gap reprocessing, i.e., less than i, where i-gap exists when the need is large than needs to traverse backwards Alternatively, since the positive will lexicographically larger.

Specifically look at the code, it is still very simple.

Code

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(), k = in.nextInt();
		StringBuilder ss = new StringBuilder(in.next());
		int a[] = new int[10];
		for (int i = 0; i < n; i++) {
			a[ss.charAt(i) - '0']++;
		}
		int years = 0x3f3f3f3f;
		StringBuilder res = new StringBuilder();
		for (int i = 0; i <= 9; i++) {
			int need = k - a[i], gap = 1, sum = 0;
			StringBuilder s = new StringBuilder(ss);
			// System.out.println(s);
			while (need > 0) {
				// System.out.println("gg");
				if (i + gap <= 9) {
					if (a[i + gap] < need) {
						for (int j = 0; j < n; j++) {
							if (s.charAt(j) - '0' == i + gap) {
								s.setCharAt(j, (char) (i+'0'));
							}
						}
						sum += gap * a[i + gap];
						need -= a[i + gap];
					} else {
						sum += gap * need;
						for (int j = 0; j < n; j++) {
							if (s.charAt(j) - '0' == i + gap && need > 0) {
								s.setCharAt(j, (char) (i+'0'));
								need--;
							}
						}

						break;
					}
				}
				if (i - gap >= 0) {
					if (a[i - gap] < need) {
						for (int j = 0; j < n; j++) {
							if (s.charAt(j) - '0' == i - gap) {
								s.setCharAt(j, (char) (i+'0'));
							}
						}
						sum += gap * a[i - gap];
						need -= a[i - gap];
					} else {
						sum += gap * need;
						for (int j = n - 1; j > 0; j--) {
							if (s.charAt(j) - '0' == i - gap && need > 0) {
								s.setCharAt(j, (char) (i+'0'));
								need--;
							}
						}

						break;
					}
				}
				gap++;
			}
			String st = s.toString();
			String rt = res.toString();
//			System.out.println(i+" "+sum+" "+s);
			if (sum < ans) {
				ans = I;
				res = s;

			} else if (sum == ans && st.compareTo(rt) < 0) {
				res = s;
			}
		}
		System.out.println(ans);
		System.out.println(res);
	}
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/12094430.html
Recommended