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 , the integer may not start with the digit . 

For example, if you are given the digits , , , ,  and , you can write the pair of integers  and . Of course, there are many ways to form such pairs of integers:  and ,  and , etc. The absolute value of the difference between the integers in the last pair is , 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  decimal digits. (The decimal digits are , , ..., .) 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

     

Sample Output


Source

给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小。

思路:

有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生的差值比当前所搜索到的结果还要大,那么就直接返回。这个剪枝就是超时与几十MS的差距

注意一点就是可能有0 与一个数字存在的情况,比如0 3,0 5等等。

其他的就比较简单了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 1<<29
#define N 16
int a[N];
int vis_a[N],vis_b[N];
int ans;
int Anum,Bnum;
int w;
int n;
int nums[]={,,,,,};
void dfs_B(int num,int vals,int sum){ if( num> && sum*nums[Bnum-num]-vals>=ans ) return; if(num==Bnum){
ans=min(ans,abs(sum-vals));
return;
} for(int i=;i<n;i++){
if(!vis_a[i] && !vis_b[i]){
if(num== && a[i]==)
continue;
vis_b[i]=;
dfs_B(num+,vals,sum*+a[i]);
vis_b[i]=;
}
} }
void dfs_A(int num,int vals){ if(num==Anum){
w=vals;
memset(vis_b,,sizeof(vis_b));
dfs_B(,vals,);
return;
} for(int i=;i<n;i++){
if(!vis_a[i]){
if(num== && a[i]==)
continue;
vis_a[i]=;
dfs_A(num+,vals*+a[i]);
vis_a[i]=;
}
}
}
int main()
{
int t; while(scanf("%d",&t)!=EOF)
{ getchar();
while(t--){
n=;
char ch;
while((ch=getchar())!='\n'){
if(ch>='' && ch<='')
a[n++]=ch-'';
} //for(int i=0;i<n;i++)
//printf("---%d\n",a[i]);
ans=inf; Anum=n>>;
Bnum=n-Anum; dfs_A(,); if(ans!=inf)
printf("%d\n",ans);
else
printf("%d\n",w);
} }
return ;
}
 

poj 2718 Smallest Difference(穷竭搜索dfs)的更多相关文章

  1. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  2. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  3. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

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

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  5. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  6. POJ 2718 Smallest Difference【DFS】

    题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...

  7. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  8. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  9. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

随机推荐

  1. 【架构师之路】依赖注入原理---IoC框架

    1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.  图1:软件系统中耦合的对象 如果我们 ...

  2. 魅蓝Note2跑分 MT6753性能究竟如何

    MT6753实力究竟如何? 采用LP工艺的MT6753实际上在性能和功耗方面并不比MT6752高,相反,同频下功耗要高1/3左右.并且其内存带宽是5.3G/s,小于MT 6752的6.4G/s 而且没 ...

  3. 常见算法:C语言求最小公倍数和最大公约数三种算法

    最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...

  4. ios 绘制wav波形图

    最近研究了如何在iOS上绘制wav波形图.查了很多资料,都没能找到一个很完整的介绍,我这里总结一下一些经验. 首先需要了解wav的这3个重要指标:采样率.采样位数.声道数.下面以16KHz, 16Bi ...

  5. hdu 4940 无源汇有上下界最大流

    /* <img src="http://img.blog.csdn.net/20140823174212937?watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  6. 常用分组函数count-avg-sum-max-min

    分组函数也称多行函数,用于对一组数据进行运算,针对一组数据(取自于多行记录的相同字段)只返回一个结果,例如计算公司全体员工的工资总和.最高工资.最低工资.各部门的员工平均工资(按部门分组)等.由于分组 ...

  7. javascript 识别移动端设备

    看到一种比较简单的方法,于是就把它记录下来备用吧.最近离职了,房子换了,还有...真是一把心酸,我知道谁活着都不容易,自己也资格把自己的苦水吐给别人,因为别人也过得不容易,所以大多不快都只能闷着,大家 ...

  8. 浅述Oracle分布式事务概念

    着系统的复杂性不断增加,我们所面对的分布式系统渐渐增加.分布式文件系统.分布式消息队列系统等等层出不穷,在一些行业特别是互联网行业应用广泛.分布式数据库也是目前使用比较常用的分布式系统之一. 简单来说 ...

  9. java中IO递归写入

    package cn.stat.p1.file; import java.io.BufferedWriter; import java.io.File; import java.io.FileWrit ...

  10. 常用mysql笔记

    1.insert into ... values insert into tables (col1,col2) values (1,2),(2,3); 2.insert into ... select ...