POJ2718 Smallest Difference [greedy + enumeration]

Source: Internet
Author: User

Smallest Difference
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:4639   Accepted:1290

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. the remaining digits can be written down in some order to form a second integer. unless the resulting integer is 0, the integer may not start with the digit 0.

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. of course, there are always ways to form such pairs of integers: 210 and 764,204 and 176, etc. the absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. for each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1 ,..., 9 .) no digit appears more than once in one line of the input. the digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

10 1 2 4 6 7

Sample Output

28

Source

Rocky Mountain 2005

/*** Problem: POJ2718 *** Status: Accepted ** Running Time: 16 ms *** Author: Changmu *** question: a maximum of 10 unit numbers are given, divide these numbers into two parts and calculate the minimum value of their ** absolute values. ** Question: I always wanted to use search for enumeration. Later I learned that I could use greed. The specific train of thought ** is like this: if N is 2 and a number is 0, if N is an even number, then find the closest ** two non-zero numbers, place the largest value in box1 and the smallest value in box2. Place the smallest combination in ** box1, and place the largest combination in box2 to obtain the difference value and update ans. If N is an odd number, in ** The N number, select the smallest non-zero number and place it on the box1 header edge. Then, the remaining number is processed based on the case where N is an even number. */# Include <stdio. h> # include <string. h >#include <algorithm> # define maxn 12 # define inf 0x3f3f3fint box [maxn], box1 [maxn], box2 [maxn], vis [maxn], num, num1, num2; int minSub, ans, sign; // sign is used to mark the parity void Select (int k) {for (int I = 0; k <num1 & I <num; ++ I) if (! Vis [I]) {vis [I] = 1; box1 [k ++] = box [I];} for (int I = num-1; sign <num2 & I> = 0; -- I) if (! Vis [I]) {box2 [sign ++] = box [I];} int a = 0, B = 0; for (int I = 0; I <num1; ++ I) a = a * 10 + box1 [I]; for (int I = 0; I <num2; ++ I) B = B * 10 + box2 [I]; ans = std: min (ans, a-B);} int main () {int T, I; char ch; scanf ("% d", & T); while (T --) {num = 0; while (scanf ("% d % c", & box [num ++], & ch) {vis [num-1] = 0; if (ch = '\ n') break;} std: sort (box, box + num ); num2 = num> 1; num1 = num-num2; Ns = inf; if (num = 2 & box [0] = 0) {// specifies printf ("% d \ n", box [1]); continue;} minSub = 10; sign = 0; if (num & 1) {if (box [0]) {vis [0] = 1; box1 [0] = box [0];} else {vis [1] = 1; box1 [0] = box [1];} Select (1 );} else {I = 1; if (! Box [0]) I = 2; for (; I <num; ++ I) minSub = std: min (minSub, box [I]-box [i-1]); for (I = 1; I <num; ++ I) {if (box [i-1] & box [I]-box [i-1] = minSub) {vis [I] = 1; vis [i-1] = 1; box1 [0] = box [I]; box2 [0] = box [i-1]; sign = 1; select (1); memset (vis, 0, sizeof (vis) ;}} printf ("% d \ n", ans) ;}return 0 ;}



POJ2718 Smallest Difference [greedy + enumeration]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.