POJ 2718 Smallest Difference 【DFS 穷竭搜索】

Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6519 Accepted: 1774

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

恩,题目大意就是说,给出几个数字,求由这几个数字所组成的两个数的最小差,0不可为一个非1位数的前导。首先这两个数的位数差不能超过1,然后看人家题解是得到当前的一个值,然后由STL里的next_permutation()函数求的排列出不同的另一值,比较得最小差。next_permutation ()得到的是当前排列的下一个排列。


#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,len,ans,r[11],t[11];
bool vis[11];
void dif(int a)
{
    int k=0,b=0;
    for(int i=0;i<n;++i)
    {
        if(!vis[i])
        {
            t[k++]=r[i];
            b=b*10+r[i];
        }
    }
    if(k==1||t[0]!=0)//这里是排除位数大于1而0为前导的情况
        ans=min(ans,abs(a-b));
    while(next_permutation(t,t+k))
    {
        b=0;
        int i=0;
        while(i<k)
        {
            b=b*10+t[i];
            i++;
        }
        if(k==1||t[0]!=0)
            ans=min(ans,abs(a-b));
    }
}
void dfs(int k,int val)
{
    if(k==len)
    {
        dif(val);
        return ;
    }
    for(int i=0;i<n;++i)
    {
        if(!vis[i])
        {
            if(r[i]==0&&k==0&&len>1)
                continue;
            vis[i]=true;
            dfs(k+1,val*10+r[i]);
            vis[i]=false;
        }
    }
}
int main()
{
    int t;
    char c;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        n=0;
        ans=0x3f3f3f;
        memset(vis,false,sizeof(vis));
        while((c=getchar())!='\n')
        {
            if(c!=' ')
                r[n++]=c-'0';
        }
        len=n/2;
        dfs(0,0);
        printf("%d\n",ans);
    }
    return 0;
}


恩,这个是双DFS 在 a 已定的情况下找 b 找到最小值,要剪枝,就是当a 一定时,若此时未求出完整的b 但此时b 之后的位都补0 与a 的差大于等于此时的ans 则不用再找下去

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int rec[12],a[12],b[12];
int n,ans,cnta,cntb,val;
bool visa[12],visb[12];
int num[10]={1,10,100,1000,10000,100000,1000000};
void dfs_b(int lenb,int bb)
{
    if(lenb>0&&abs(val-bb*num[cntb-lenb])>=ans)
        return ;
    if(lenb==cntb)
    {
        ans=min(ans,abs(val-bb));
        return ;
    }
    for(int i=0;i<n;++i)
    {
        if(!visa[i])
        {
            if(rec[i]==0&&lenb==0)
                continue;
            visa[i]=true;
            dfs_b(lenb+1,bb*10+rec[i]);
            visa[i]=false;
        }
    }
}
void dfs_a(int lena,int aa)
{
    if(lena==cnta)
    {
        val=aa;
        dfs_b(0,0);
        return ;
    }
    for(int i=0;i<n;++i)
    {
        if(!visa[i])
        {
            if(rec[i]==0&&lena==0)
                continue;
            visa[i]=true;
            dfs_a(lena+1,aa*10+rec[i]);
            visa[i]=false;
        }
    }
}
int main()
{
    char c;
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        n=0;
        ans=inf;
        memset(visa,false,sizeof(visa));
        while((c=getchar())!='\n')
        {
            if(c!=' ')
                rec[n++]=c-'0';
        }
        cnta=n/2;
        cntb=n-n/2;
        dfs_a(0,0);
        if(ans==inf)
            printf("%d\n",val);
        else
            printf("%d\n",ans);
    }
    return 0;
}


 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值