Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6537 | Accepted: 1780 |
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
题意:给你若干个0-9之间的数字(无重复),你可以任意组合成两个整数a和b(不能带有前导0且所有数字都要用上),要求abs(a-b)最小。
思路:DFS标记组成a的数字,然后next_permutation()一下剩余数字的全排列就可以了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (1000000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int num[20];
char str[100];
int top, c[20];
bool vis[20];
int ans;
void DFS(int cnt, int val)
{
if(cnt == top / 2)
{
int k = 0;
for(int i = 1; i <= top; i++)
{
if(vis[i]) continue;
c[k++] = num[i];
}
do
{
int v = 0;
if(c[0] == 0 && k > 1) continue;
for(int i = 0; i < k; i++)
v = v * 10 + c[i];
//printf("%d %d\n", v, val);
ans = min(ans, abs(v - val));
}while(next_permutation(c, c+k));
}
else
{
for(int i = 1; i <= top; i++)
{
if(vis[i]) continue;
if(num[i] == 0 && cnt == 0) continue;
vis[i] = true;
DFS(cnt + 1, val * 10 + num[i]);
vis[i] = false;
}
}
}
int main()
{
int t; Ri(t);
getchar();
W(t)
{
gets(str);
int len = strlen(str);
int sum = 0;
top = 0;
for(int i = 0; i < len; i++)
{
if(str[i] == ' ' || i == len-1)
{
if(i == len-1)
sum = sum * 10 + str[i] - '0';
num[++top] = sum;
sum = 0;
}
else
sum = sum * 10 + str[i] - '0';
}
ans = INF;
CLR(vis, false); DFS(0, 0);
Pi(ans);
}
return 0;
}