POJ--2718 Smallest Difference(暴搜/减枝)

记录
19:10 2023-3-3

http://poj.org/problem?id=2718

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

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.

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

暴搜,先选择好num1中的数字(这时候num2中的数字也确定了),然后可以用next_permutation(自己写也可以),得出的结果abs(num1 - num2),求出最小的差值。
减枝的思路:

  1. 任何一个数不能使用所有的数字。
  2. 使用过的数可以不进行判断(可以用set来记录使用过的数)。
  3. 要求差值最小值,那么这俩个数的位数差不大于等于2。
#include<cstdio>
#include<cmath>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#define MAX_N 10000
typedef long long ll;
const int INF = 0x3f3f3f3f;


ll arr[MAX_N];
ll N;
ll arr_1[MAX_N];
ll arr_2[MAX_N];
ll num_1[MAX_N];
ll num_2[MAX_N];
bool used[MAX_N];
ll count;
std::set<ll> iSet;
ll min_diff = INF;

// ll fun() {
//     ll arr[3] = {2, 0, 0};
//     do {
//         for(auto i: arr) {
//             std::cout << i;
//         }
//         std::cout << std::endl;
//     }while (std::next_permutation(arr, arr + 2));
//     return 0;
// }

void diff() {
    if(count == N || count == 0) return;
    ll total_1 = 0;
    ll N_1 = 0;
    for(ll j = 0; j < N; j++) {
        if(used[j]) {
            total_1 = total_1 * 10 + arr[j];
            arr_1[N_1++] = arr[j];
        }
    }

    ll total_2 = 0;
    ll N_2 = 0;
    for(ll j = 0; j < N; j++) {
        if(!used[j]) {
            total_2 = total_2 * 10 + arr[j];
            arr_2[N_2++] = arr[j];
        }
    }
    if (abs(N_1 - N_2) >= 2) return;
    // if(iSet.find(total_1) != iSet.end()) {
    //     return;
    // } else {
    //     // std::cout << total_1 << " " << total_2 << std::endl;
    // }
    // iSet.insert(total_1);
    // iSet.insert(total_2);

    ll num_size_1 = 0;
    do {
        total_1 = 0;
        for(ll i = 0 ; i < N_1; i++) {
            total_1 = total_1 * 10 + arr_1[i];
        }
        if(arr_1[0] == 0 && total_1 != 0) continue;
        num_1[num_size_1++] = total_1;
    }while (std::next_permutation(arr_1, arr_1 + N_1));

    ll num_size_2 = 0;
    do {
        total_2 = 0;
        for(ll i = 0 ; i < N_2; i++) {
            total_2 = total_2 * 10 + arr_2[i];
        }
        if(arr_2[0] == 0 && total_2 != 0) continue;
        num_2[num_size_2++] = total_2;
    }while (std::next_permutation(arr_2, arr_2 + N_2));

    for(ll i = 0; i < num_size_1; i++) {
        for(ll j = 0; j < num_size_2; j++) {
            if(min_diff >= abs(num_1[i] - num_2[j])) {
                min_diff = abs(num_1[i] - num_2[j]);
            }
        }
    }
}

void select(ll i) {
    diff();
    for(;i < N; i++) {
        used[i] = true;
        count += 1;
        select(i + 1);
        used[i] = false;
        count -= 1;
    }
}

void solve() {
    min_diff = INF;
    iSet.clear();
    select(0);
    std::cout << min_diff << std::endl;
}

int main() {
    // fun();
    ll M, v;
    char c[50] = {'\0'};
    std::cin >> M;
    std::cin.get();
    for(ll i = 0; i < M; i++) {
        N = 0;
        std::cin.getline(c, 50);
        for(ll j = 0; j < 50; j++) {
            if(c[j] >= 48 && c[j] <= 57) {
                arr[N++] = c[j] - 48;
                c[j] = '\0';
            }
        }
        solve();
    }
    return 0;
}
posted @ 2023-03-03 19:20  57one  阅读(2)  评论(0编辑  收藏  举报