Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10453 | Accepted: 2855 |
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
当n为奇数的时候,很明显,让最常的从高位到最低位递增,最短的从高位到低位递减。
当n为偶数的时候,要使得两个数的最高位的差最小,同时后面的每一位的差值最大,我们可以找到差值最小的两个数,并且使得这两个数尽量在中间,这样才会使得后面的差值最大。
我看到好多全排列的题,找了两个对拍发现了对方代码都有bug,这道题的测试数据有点水。
#include <map> #include <set> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <iostream> #include <stack> #include <cmath> #include <string> #include <vector> #include <cstdlib> //#include <bits/stdc++.h> //#define LOACL #define space " " using namespace std; //typedef long long LL; //typedef __int64 Int; typedef pair<int, int> PAI; const int INF = 0x3f3f3f3f; const double ESP = 1e-5; const double PI = acos(-1.0); const int MOD = 1e9 + 7; const int MAXN = 500 + 10; int num[MAXN]; char c; struct node { int a, b, c; } data[MAXN]; int cnt; bool cmp(node x, node y) { if (x.c == y.c) return (abs(x.a - cnt/2) < abs(y.a - cnt/2)); return x.c < y.c; } int main() { int T; scanf("%d", &T); getchar(); while (T--) { cnt = 0; while (c = getchar()) { if (c == '\n') break; if (c >= '0' && c <= '9') num[cnt++] = c - '0'; } sort(num, num + cnt); int a = 0, b = 0; int ans = INF; if (cnt == 2) { ans = num[1] - num[0]; } else if (cnt & 1) { for (int i = 0; i < (cnt + 1)/2; i++) { if (num[i] == 0) { a = a*10 + num[++i]; a = a*10; } else a = a*10 + num[i]; } // cout << a << endl; for (int i = cnt - 1; i >= (cnt + 1)/2; i--) { b = b*10 + num[i]; } ans = a - b; // cout << a << space << b << endl; } else { int cut = 0; for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt; j++) { if (num[i] == 0) continue; data[cut].a = j; data[cut].b = i; data[cut++].c = num[j] - num[i]; } } sort(data, data + cut, cmp); //for (int i = 0; i < cut; i++) cout << data[i].a << space << data[i].b << space << data[i].c << endl; int k = abs(data[0].a - cnt/2) == abs(data[1].a - cnt/2); k++; //cout << "k= " << k << endl; while (k--) { int ca = cnt/2 - 1; int cb = cnt/2 - 1; int ta = data[k].a, tb = data[k].b; a = num[ta]; b = num[tb]; for (int i = 0; i < cnt; i++) { if (ca == 0) {ca = i; break;} if (i != ta && i != tb) { ca--; a = a*10 + num[i]; } } for (int i = cnt - 1; i >= ca; i--) { if (i != ta && i != tb) { b = b*10 + num[i]; } } ans = min(ans, a - b); } } printf("%d\n", ans); } return 0; }