POJ 2718 Smallest Difference【DFS】
题意:
就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小。
思路:
我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生的差值比当前所搜索到的结果还要大,那么就直接返回。这个剪枝就是超时与几十MS的差距
注意一点就是可能有0 与一个数字存在的情况,比如0 3,0 5等等。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
const int inf=1<<29;
int num[20],cnt,cnta,cntb,val,ans;
int nums[10]={1,10,100,1000,10000,100000,1000000};
bool usea[20],useb[20];
char ch;
void DFS_B(int vals,int deep)
{
if(deep>0&&abs(val-vals*nums[cntb-deep])>=ans)
return;
if(deep==cntb)
{
ans=min(ans,max(val,vals)-min(val,vals));
return;
}
for(int i=0;i<cnt;i++)
if(!usea[i]&&!useb[i])
{
if(deep==0&&num[i]==0)
continue;
useb[i]=1;
DFS_B(vals*10+num[i],deep+1);
useb[i]=0;
}
}
void DFS_A(int vals,int deep)
{
if(deep==cnta)
{
val=vals;
memset(useb,0,sizeof(useb));
DFS_B(0,0);
return;
}
for(int i=0;i<cnt;i++)
if(!usea[i])
{
if(deep==0&&num[i]==0)
continue;
usea[i]=1;
DFS_A(vals*10+num[i],deep+1);
usea[i]=0;
}
}
int main()
{
int T;
while(scanf("%d",&T)!=EOF)
{
getchar();
while(T--)
{
cnt=0;
while((ch=getchar())!='\n')
{
if(ch>='0'&&ch<='9')
num[cnt++]=ch-'0';
}
cnta=cnt>>1;
cntb=cnt-cnta;
ans=inf;
DFS_A(0,0);
if(ans==inf)
printf("%d\n",val);
else
printf("%d\n",ans);
}
}
return 0;
}
思路二:
要想两个数的差最小,就是对半分,暴力比较求最小值。
关键就是用next_permutation()函数求这列数的全排列,排除前导零的情况。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std; int a[15];
int n; void solve()
{
while(a[0]==0)
next_permutation(a,a+n); int ans=INF;
int mid=(n+1)/2;
do
{
if(a[mid])
{
int x=a[0],y=a[mid];
for(int i=1;i<mid;i++)
x=x*10+a[i];
for(int i=mid+1;i<n;i++)
y=y*10+a[i];
if(ans>abs(x-y))
ans=abs(x-y);
} }while(next_permutation(a,a+n));
cout<<ans<<endl;
} int main()
{
int T;
char c; scanf("%d",&T);
getchar();
while(T--)
{
n=0;
memset(a,0,sizeof(a)); while((c=getchar())!='\n')
{
if(c!=' ')
a[n++]=c-'0';
} if(n==1)
printf("%d\n",a[0]);
else if(n==2)
printf("%d\n",abs(a[1]-a[0]));
else
solve();
}
return 0;
}
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(穷竭搜索dfs)
		Description Given a number of distinct , the integer may not start with the digit . For example, , , ... 
- POJ 2718 Smallest Difference(dfs,剪枝)
		枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ... 
- 穷竭搜索: POJ 2718 Smallest Difference
		题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ... 
- POJ - 1321 棋盘问题  【DFS】
		题目链接 http://poj.org/problem?id=1321 思路 和N皇后问题类似 但是有一点不同的是 这个是只需要摆放K个棋子就可以了 所以 我们要做好 两个出口 并且要持续往下一层找 ... 
- POJ 2718 Smallest Difference 枚举
		http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ... 
随机推荐
- mac os 如何加载 Java Native/Shared Library (.jnilib)
			1 . 问题描述 今天在开发 Java 解压.z 文件的时候 需要加载 .jnilib 文件. 总是提示 Native code library failed to load. java.lang.U ... 
- jquery load 和 iframe 比较
			如果要加载的东西比较简单,里面的没有复杂的数据和逻辑,可以使用load.如果要加载的页面自身有复杂的逻辑.操作,还是建议使用ifame,因为iframe里面可以引入自身的js和样式,而load引入的东 ... 
- php无限极分类以及递归(thinkphp)
			php无限极分类: 无限极分类重点在于表的设计: 1在model中: class CatModel extends Model{ protected $cat = array(); public fu ... 
- 汤姆大叔 javascript 系列 第20课 最后的5到javascript题目
			博客链接:http://www.cnblogs.com/TomXu/archive/2012/02/10/2342098.html 原题: 大叔注:这些题目也是来自出这5个题目的人,当然如果你能答对4 ... 
- 数组中的每一个对象执行一次方法:makeObjectsPerformSelector
			1, 为数组中的每一个button添加点击事件: [_buttonArray makeObjectsPerformSelector:@selector(addTarget:self action:( ... 
- grep 命令
			简单介绍:grep命令是用于分析一行信息,若当中有我们所需要的信息,就将该行取出来. 语法结构:grep [-acinv] [--color=auto] '查找关键字' #{filename} -a: ... 
- oracle 学习笔记(三)
			1. SQL(基础查询) 1.1. 基本查询语句 1.1.1. FROM子句 SQL查询语句的语法如下: SELECT <*, column [alias], -> FROM tabl ... 
- CloudSim4.0报错NoClassDefFoundError,Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.distribution.UniformRealDistribution
			今天下载了CloudSim 4.0的代码,运行其中自带的示例程序,结果有一部分运行错误: 原因是找不到org.apache.commons.math3.distribution.UniformReal ... 
- thinkpad W500S 如何换键盘?
			tHINKPAD的笔记本拆装有,123456789... 至少5种以上了,一般键盘去下都是边上撬就去下 来了.今天拆换W550S键盘就遇到劲敌了.拼了 老劲也去不下,冬天背上都冒汗(屋子热的吧). 终 ... 
- c++单链表基本功能
			head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ... 
