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 ...
随机推荐
- NC凭证接口(Java发送流和处理返回结果)
问题描述: 金融行业在系统模块分为财务和业务两个系统,我公司是负责业务模块系统,NC公司负责财务系统.但是财务有时候需要生成凭证,这时候就涉及业务模块了,我方就需要写NC凭证接口.这时候就需要三方交互 ...
- Java Abstract Class & Interface
一. 抽象类 1. 抽象类:包含了一个抽象方法的类就是抽象类 2. 抽象方法:声明而未被实现的方法,用关键字abstract声明 3. 抽象类被子类继承,子类(如果不是抽象类)必须重写(overrid ...
- 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation
1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ...
- 安卓ApiDemos最简单的使用方法
http://download.csdn.net/detail/ffwmxr/7401067#comment 正确使用方法:开新工程, 名字API Demos,将下载文件里的 src,res, And ...
- iOS 25个性能优化/内存优化常用方法
1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...
- 2. 上传Android代码到github
1. 建立git仓库 cd到本地项目根目录,执行git命令 git init 同时会在项目根目录下生成一个.git的隐藏文件 windows下先禁用 ...
- 设计模式--原型模式Prototype(创建型)
一.原型模式 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象.原型模式实现的关键就是实现Clone函数,还需要实现深拷贝. 二.UML类图 三.例子 //父类 class Resume ...
- Feature Access
在ArcGIS Server中发布支持Feature Access地图服务,你需要知道的几点: 所绘制的mxd地图文件中包含的数据,必须来自企业级数据库链接: mxd中包含的所有图层的数据,必须来自同 ...
- C# 反射
//转载:http://blog.csdn.net/educast/article/details/2894892 反射(Reflection) 在.NET中的反射可以实现从对象的外部来了解对象(或程 ...
- Java后台发送邮件
一.实现思路: 1.设置连接参数 2.设置邮件相关属性 3.发送邮件 二.相关需求: 1.导入jar包: 2.设置email.properties mail.smtp.host=smtp.163.co ...