poj 2718 Smallest Difference(穷竭搜索dfs)
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)的更多相关文章
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
随机推荐
- Difference between <? super T> and <? extends T> in Java
stackoverflow 原文 [http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends- ...
- C#委托与事件之观察者Observer设计模式
前言 委托: 委托是一种在对象里保存方法引用的类型,同时也是一种类型安全的函数指针. 或委托可以看成一种表示函数的数据类型,类似函数指针. 事件是特殊的委托 观察者模式:两种角色:(1)Subj ...
- 【从零学习openCV】IOS7下的人脸检測
前言: 人脸检測与识别一直是计算机视觉领域一大热门研究方向,并且也从安全监控等工业级的应用扩展到了手机移动端的app,总之随着人脸识别技术获得突破,其应用前景和市场价值都是不可估量的,眼下在学习ope ...
- linux文件解-压缩
常用: 解压tar.gz包 使用命令:tar -zxvf file.tar.gz -z 指有gzip的属性 -x 解开一个压缩文件的参数 -v解压过程中显示文件 -f放最后接filena ...
- R语言——包的添加和使用
R是开源的软件工具,很多R语言用户和爱好者都会扩展R的功能模块,我们把这些模块称为包.我们可以通过下载安装这些已经写好的包来完成我们需要的任务工作. 包下载地址:https://cran.r-proj ...
- FeatureClass的"import"(转换)功能
/// <summary> /// FeatureClass的"import"功能. /// </summary> /// <param name=& ...
- AngularJs练习Demo19 Resource
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- Objective-C 类型判断
可以通过 isKindOfClass 判断对象的类型 @interface A : NSObject @end @implementation A @end //// @interface AA : ...
- php——会话控制
1.什么叫做会话控制 允许服务器根据客户端做出的连续请求. 2.为什么需要会话控制? 因为当你打开一个网站,并想访问该网站的其他页面的时候,如果没有会话控制,当跳转到其他页面的 时候,就需要再次输入账 ...
- Android再学习-20141023-Intent-Thread
20141023-Android再学习 Intent对象的基本概念 Intent是Android应用程序组件之一 Intent对象在Android系统中表示一种意图 Intent当中最重要的内容是ac ...