#P13039. [GCJ 2021 #3] Build-A-Pair

    ID: 12840 Type: RemoteJudge 5000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>贪心2021枚举分类讨论Google Code Jam

[GCJ 2021 #3] Build-A-Pair

题目描述

You want to build a pair of positive integers. To do that, you are given a list of decimal digits to use. You must use every digit in the list exactly once, but you get to choose which ones to use for the first integer and which ones to use for the second integer. You also get to choose the order of the digits within each integer, except you cannot put a zero as the most significant (leftmost) digit in either integer. Note that you cannot choose just a zero for one integer either, because it would not be positive.

For example, you could be given the list [1,0,2,0,4,3][1, 0, 2, 0, 4, 3]. Two of the valid pairs you can build are (200,143)(200, 143) and (3,12400)(3, 12400). The following pairs, on the other hand, are not valid:

  • (0102,34)(0102, 34): has a leading zero.
  • (0,12340)(0, 12340): has a non-positive integer.
  • (10,243)(10, 243) and (12300,47)(12300, 47): the list of digits in each of these pairs is not exactly equal to the given list of digits.

Given the list of digits to use, what is the minimum absolute difference between the two built integers that can be achieved?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} lines follow. Each line describes a test case with a single string of digits D\mathbf{D}. Each character of D\mathbf{D} is a digit you must use.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum possible absolute difference between the two integers built from D\mathbf{D} according to the rules above.

4
1234
0011
07080
0899
Case #1: 7
Case #2: 0
Case #3: 620
Case #4: 1

提示

Sample Explanation

The optimal pair of integers to build are 3131 and 2424 for Sample Case #1, 1010 and 1010 for Sample Case #2, 700700 and 8080 for Sample Case #3, and 8989 and 9090 for Sample Case #4.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • Each character of D\mathbf{D} is a decimal digit.
  • At least two characters of D\mathbf{D} are not \emptyset.

Test Set 1 (3 Pts, Visible Verdict)

  • 22 \leq the length of D8\mathbf{D} \leq 8.

Test Set 2 (12 Pts, Visible Verdict)

  • 22 \leq the length of D36\mathbf{D} \leq 36.