Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:13974 | Accepted: 3797 |
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 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 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.
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 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 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
1 0 1 2 4 6 7
Sample Output
28
这题情况有点多,没有剪枝,860ms,也还可以。 首先和最小要让他们两个位数最接近,假设有n个数 则一个n/2位,另一个n-n/2位。 dfs选出n/2个数,dfs2将这n/2个数组合,dfs3将剩下的数组合。 代码: #include<cstdio> #include<iostream> #include<queue> #include<string.h> #include<algorithm> int a[11],m,b1[7],b2[7],ans; bool use[11],use2[11],use3[11]; using namespace std; int pow1(int a,int b) { int r=1; for(int i=1;i<=b;i++) r=r*a; return r; } void dfs3(int num,int d,int sum,int s) { if(num==d) { ans=min(ans,abs(s-sum)); return; } for(int i=1;i<=num;i++) { if(!use3[i]) { if(b2[i]==0&&d+1==num&&num!=1) continue; use3[i]=1; dfs3(num,d+1,sum+b2[i]*pow1(10,d),s); use3[i]=0; } } } void dfs2(int num,int d,int sum) { if(num==d) { memset(use3,0,sizeof(use3)); dfs3(m-num,0,0,sum); return; } for(int i=1;i<=num;i++) { if(!use2[i]) { if(b1[i]==0&&d+1==num&&num!=1) continue; use2[i]=1; dfs2(num,d+1,sum+b1[i]*pow1(10,d)); use2[i]=0; } } } void dfs(int num,int d,int k) { if(num==d) { int i,j=0,k=0; for(i=1; i<=m; i++) if(use[i]) b1[++j]=a[i]; else b2[++k]=a[i]; memset(use2,0,sizeof(use2)); dfs2(num,0,0); return; } for(int i=k; i<=m; i++) { if(!use[i]) { use[i]=1; dfs(num,d+1,i+1); use[i]=0; } } } int main() { int T; scanf("%d",&T); getchar(); while(T--) { char t[33]; gets(t); int i,j; m=0; for(i=0; i<strlen(t); i++) { if(t[i]>='0'&&t[i]<='9') a[++m]=t[i]-'0'; } memset(use,0,sizeof(use)); ans=1000000000; dfs(m/2,0,1); printf("%d\n",ans); } return 0; }