Smallest Difference
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.
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
Source
题目类型:DFS搜索
算法分析:要想保证得到的是最小距离,需要满足两个数的位数尽量接近。所以dfs枚举得到lena / 2位的数vala,然后再构造valb,然后对于组成valb的数字枚举序列并更新最小值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
/************************************************* Author :supermaker Created Time :2016/4/15 19:37:46 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 10 + 66; int aa[maxn], bb[maxn], lena, lenb, tt, res; bool vis[maxn]; void SS (int val) { lenb = tt = 0; for (int i = 1; i <= lena; i++) if (!vis[i]) bb[++lenb] = aa[i], tt = tt * 10 + aa[i]; if (bb[1] > 0 || lenb == 1) res = min (res, abs (val - tt)); while (next_permutation (bb + 1, bb + 1 + lenb)) { if (bb[1] > 0 || lenb == 1) { tt = 0; for (int i = 1; i <= lenb; i++) tt = tt * 10 + bb[i]; res = min (res, abs (val - tt)); } } } void dfs (int cur, int val) { if (cur == lena / 2) { SS (val); return ; } for (int i = 1; i <= lena; i++) if (!vis[i]) { if (cur == 0 && aa[i] == 0 && lena >= 4) continue; vis[i] = true; dfs (cur + 1, val * 10 + aa[i]); vis[i] = false; } } int main() { //CFF; //CPPFF; int t; cin >> t; string s; getline (cin, s); while (t--) { getline (cin, s); stringstream ss (s); lena = 0; while (ss >> aa[++lena]) ; lena--; memset (vis, false, sizeof (vis)); res = INF; dfs (0, 0); cout << res << endl; } return 0; } |
- « 上一篇:poj3262
- poj3280:下一篇 »