http://poj.org/problem?id=2718
Select a subset from some number to form a number, the remaining number to form another number, (number cannot start with 0) Ask the absolute value of the difference of two numbers is the minimum!
Whether it is an odd or even number, to minimize the absolute value, then two number of digits to be as close as possible, so each number of bits are N/2, enumerate these numbers of the full array, and then go to find this minimum value.
Note that the direct enumeration will time out and need pruning.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 1010 - using namespacestd; the + intMain () A { the //Read (); + //Write () - intT; $ intf[ the],n,minn; $ strings; -scanf"%d",&t); - GetChar (); the while(t--) - {Wuyi getline (cin,s); then=0; - for(intI=0; I<s.length (); i++) Wu { - if(s[i]>='0'&&s[i]<='9') f[n++]=s[i]-'0'; About } $ //printf ("%d\n", n); - //for (int i=0;i<n;i++) printf ("%d", F[i]); - if(n==2) {printf ("%d\n", ABS (f[0]-f[1]));Continue;} -minn=MAXN; A while(f[0]==0) next_permutation (f,f+n);//Pruning + Do the { - if(f[n/2])//Pruning $ { the intx=0, y=0; the for(intI=0; i<n/2; i++) x=x*Ten+F[i]; the for(inti=n/2; i<n;i++) y=y*Ten+F[i]; theMinn=min (ABS (xy), Minn); - } in} while(Next_permutation (f,f+n)); theprintf"%d\n", Minn); the } About return 0; the}
POJ-2718 Smallest difference