POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4639   Accepted: 1290

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

Source

Rocky Mountain 2005

/*
** Problem: POJ2718
** Status: Accepted
** Running Time: 16ms
** Author: Changmu
**
** 题意:给出最多10个单位数字,将这些数字分成两部分,求它们的
** 绝对值的最小值。
** 题解:开始总想着用搜索来枚举,,,后来才知道可以用贪心。具体的思路
** 是这样:如果N为2且有一个数为0时特判,如果N为偶数,那么找出最接近的
** 两个非零数,大的放到box1中,小的放在box2中,剩下的将最小的组合放到
** box1中,最大的组合放在box2,求出差值并更新ans;如果N为奇数,那么在
** 这N个数中,选择最小的一个非零数放到box1头边,然后剩下的数按照N为偶数的情况处理。
*/

#include <stdio.h>
#include <string.h>
#include <algorithm>

#define maxn 12
#define inf 0x3f3f3f3f

int box[maxn], box1[maxn], box2[maxn], vis[maxn], num, num1, num2;
int minSub, ans, sign; // sign 用来标记奇偶

void Select(int k) {
    for(int i = 0; k < num1 && i < num; ++i)
        if(!vis[i]) {
            vis[i] = 1;
            box1[k++] = box[i];
        }
    for(int i = num - 1; sign < num2 && i >= 0; --i)
        if(!vis[i]) {
            box2[sign++] = box[i];
        }
    int a = 0, b = 0;
    for(int i = 0; i < num1; ++i)
        a = a * 10 + box1[i];
    for(int i = 0; i < num2; ++i)
        b = b * 10 + box2[i];
    ans = std::min(ans, a - b);
}

int main() {
    int T, i;
    char ch;
    scanf("%d", &T);
    while(T--) {
        num = 0;
        while(scanf("%d%c", &box[num++], &ch)) {
            vis[num-1] = 0;
            if(ch == '\n') break;
        }

        std::sort(box, box + num);
        num2 = num >> 1;
        num1 = num - num2;
        ans = inf;

        if(num == 2 && box[0] == 0) { // 特判
            printf("%d\n", box[1]);
            continue;
        }

        minSub = 10;
        sign = 0;

        if(num & 1) {
            if(box[0]) {
                vis[0] = 1;
                box1[0] = box[0];
            } else {
                vis[1] = 1;
                box1[0] = box[1];
            }
            Select(1);
        } else {
            i = 1;
            if(!box[0]) i = 2;
            for( ; i < num; ++i)
                minSub = std::min(minSub, box[i] - box[i-1]);
            for(i = 1; i < num; ++i) {
                if(box[i-1] && box[i] - box[i-1] == minSub) {
                    vis[i] = 1; vis[i-1] = 1;
                    box1[0] = box[i];
                    box2[0] = box[i-1];
                    sign = 1;
                    Select(1);
                    memset(vis, 0, sizeof(vis));
                }
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-07 06:49:23

POJ2718 Smallest Difference 【贪心+枚举】的相关文章

POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

Smallest Difference 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

poj 2718 Smallest Difference(暴力枚举)(贪心待补充)

abs写成fabs导致WA了一发. 关于next_permutation()这个STL里面的函数的基本应用 http://www.cnblogs.com/luosuo10/p/5479188.html 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace s

(DFS、全排列)POJ-2718 Smallest Difference

题目地址 简要题意: 给若干组数字,每组数据是递增的在0--9之间的数,且每组数的个数不确定.对于每组数,输出由这些数组成的两个数的差的绝对值最小是多少(每个数出现且只出现一次). 思路分析: 对于n个数,必定为分成两个位数分别为n/2和n-n/2的数时才可能取得差的绝对值最小.两组数分别进行全排列比较大小,这样比较次数最大为(10A5)*(5A5)=10!,在可以接受的范围内. 参考代码: 1 #include<stdio.h> 2 #include<cstring> 3 #in

POJ2718 Smallest Difference (暴力搜索+全排列)

题目链接:传送门 题意: 给你一个长度为n的数列 ,0<=a[i]<=9;用他们组成两个数 然后使两个数的差最小,求这个数.不能有前导0. 分析: 要使差最小肯定要使两个数的长度尽量相等,一个为n/2,另 一个为n-n/2.然后dfs搜一下选的数,然后另一个数用剩下的 数做一个全排列就可以了. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorit

Smallest Difference (poj 2718 暴力枚举)

Language: Default Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5034   Accepted: 1382 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits an

【POJ - 2718】Smallest Difference(搜索 )

-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467.当然写法多样:210和764,204和176,等等.最后一对数差的绝对值为28,实际上没有其他对拥有更小的差. Input  输入第一行的数表示随后测试用例的数量.对于每组测试用例,有一行至少两个

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

lintcode-medium-The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j