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 ...
随机推荐
- 加上固件密码,Mac更安全
在Mac OS X中,管理员密码非常重要,在修改系统参数或安装软件时都要求输入密码,这避免了绝大部分的破坏性误操作和恶意程序(尽管Mac上恶意程序本来就少的可怜). 但是对Mac OS X比较熟悉的人 ...
- 【创业积累】如何快速开发出一个高质量的APP
[起] 今早,一个技术群里有人想快速做出一个app,然后询问技术方案,大概是这样, 拿到了200w投资,期望花20w两个月先做出一个app,包括ios,android, 先,呵呵,一下, 大概预估了一 ...
- Laravel资料
http://laravel-cn.com/http://www.golaravel.com/docs/4.1/quick/https://github.com/search?q=Laravel&am ...
- Cycling Label
Cycling Label 来源: github/BBCyclingLabel Licence: Apache 2.0 作者: Bruno de Carvalho 分类: 标签(Label) 平台: ...
- 小学生之KTV播放原理
第一步: 创建一个Song类 //歌曲名称 public string SongName { get; set; } //歌曲路劲 public string SongPath { get; set ...
- .net程序开发人员必看的变量的命名规则
(1)类名.属性名.方法名采用Pascal命名,如 class User { } interface IEditable { } bool ValidateInput() public int Age ...
- php 邮件发送代码-php邮件群发
php 邮件发送如何进行的呢? php邮件发送是通过smtp协议进行的. 下面是一个php邮件发送的类的一个函数. 文件下载 function sendmail($to, $from, $subjec ...
- 自定义函数标签(JSTL)
创建自定义函数标签步骤: 1.创建类,并且方法只能是静态 public static void operation(calculator cal) 2.书写tld <taglib xmlns=& ...
- (原)torch的训练过程
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221622.html 参考网址: http://ju.outofmemory.cn/entry/284 ...
- Hibernate插入数据效率测试
硬件配置: 4G内存.CPUi3-2.3 数据库SQL2008 package com.pan.test; import org.hibernate.Session; import org.hiber ...