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 ...
随机推荐
- 1034 - Navigation
Global Positioning System (GPS) is a navigation system based on a set of satellites orbiting approxi ...
- Dynamics CRM记录页面上隐藏子网格“+”标识
前段时间微软发布了Dynamics 365,这是Dynamics产品的又一次大的变动,期待新的版本能够更好的满足客户的需求,同时提供更多的可定制化的内容. 近期做Dynamics CRM项目遇到很多审 ...
- 内部类之.this&&.new
一..this 我们都知道this是指当前类中的对象本身,但是在内部类中需要指明外部类时,this不再起作用,那应该怎么做呢?下面,让我们看看: public class DotThis { void ...
- 锚点链接和hash属性
相信大家挺经常见过这样一个效果.有一个很长很长的页面,分成好几部分,目录中一点击,就能定位到页面某个位置. 例如:有这样一个目录,例如你点击一下“HTML”,就会直接跳转到“HTML”的页面位置 这就 ...
- Map Resource Definition 中type为 'ArcGIS Server Local'.
在使用 MapResourceManager, Map进行 地图显示 时, 在 Map Resource Definition 的 type 为 ArcGIS Server Local时概述. 1,打 ...
- java线层的使用
class Do3 { public static void main(String[] args) { XC d1=new XC("小王"); XC d2=new XC(&quo ...
- Linux 常用命令使用方法大搜刮
Linux 常用命令使用方法大搜刮 1.# 表示权限用户(如:root),$ 表示普通用户 开机提示:Login:输入用户名 password:输入口令 用户是系统注册用户成功登陆后,可以进入 ...
- jQuery异步加载数据添加事件
几个月前在一个项目中涉及到树形栏,然后看了很多插件,觉得有点麻烦,于是自己写了一个,写着写着就出问题了. 当时项目是通过树形栏进行权限控制的,管理员可以对从数据库去的数据动态生成树形栏进行增删改查操作 ...
- js ||与&&
||:找到结果为true的分项就停止,并返回该分项的值,否则继续执行,如果都没有为true的分项则返回最后分项的值(注意每个分项先转成bool与true进行比较). //例如下面的例子: // &qu ...
- Linux下关闭node应用
今天在折腾用node接入微信公众号时,碰到了node应用启动后卡死退出,需要找出该进程关闭的问题,由于对shell脚本不是很熟悉,记录如下: 我们在用npm start启动应用后,通常要关闭时,ctr ...