Smallest difference
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 8463 |
|
Accepted: 2324 |
Description
Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and WRI Ting them in some order. The remaining digits can is written down in some order to form a second integer. Unless the resulting integer is 0, the integer could not be start with the digit 0.
For example, if is given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers and 2467. Of course, there is many 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 a, and it turns out this no other pair form Ed 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 are one line of input containing at least and no more than decimal digits. (The decimal digits is 0, 1, ..., 9.) No Digit appears more than once on one line of the input. The digits would appear in increasing order, separated by exactly one blank space.
Output
For each test case, write to a single line the smallest absolute difference of both integers that can is written from the G Iven digits as described by the rules above.
Sample Input
10 1 2 4 6 7
Sample Output
28
Source
Rocky Mountain 2005 Note the conditions that are n>2 in line 46th of the code.
1#include <iostream>2#include <algorithm>3#include <map>4#include <vector>5#include <functional>6#include <string>7#include <cstring>8#include <queue>9#include <Set>Ten#include <cmath> One#include <cstdio> A using namespacestd; - #defineIOS Ios_base::sync_with_stdio (False) - #defineTIE Std::cin.tie (0) the #defineMIN2 (A, B) (A<B?A:B) - #defineMIN3 (A, B) (A<b? ( A<C?A:C):(b<c?b:c)) - #defineMAX2 (A, B) (A>B?A:B) - #defineMAX3 (a,b,c) (a>b? ( A>C?A:C):(b>c?b:c)) +typedefLong LongLL; -typedef unsignedLong LongULL; + Const intINF =0x3f3f3f3f; A Const DoublePI =4.0*atan (1.0); at - intCA, N, a[ the], ans, M1, M2, x; - intInput () - { - intn =0; - CharCh=' '; in while(ch!='\ n'){ - if('0'<= Ch&&ch <='9'){ toa[n++] = CH-'0'; + } -CH =GetChar (); the } * returnN; $ }Panax Notoginseng intMain () - { thescanf"%d", &CA); + GetChar (); A while(ca--){ then =Input (); +Sort (A, A +n); -Ans =INF; $ Do{ $ if(a[0] ==0|| A[n/2] ==0&& n>2)Continue; -M1 = m2 =0; - for(inti =0; I < n/2; i++){ theM1 *=Ten; M1 + =A[i]; - }Wuyi for(inti = n/2; I < n; i++){ theM2 *=Ten; M2 + =A[i]; - } Wux = ABS (M1-m2); - if(x < ans) ans =x; About} while(Next_permutation (A, A +n)); $printf"%d\n", ans); - } -}
POJ 2718 Smallest difference