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 ...
随机推荐
- webSphere内存溢出
有一个做了很长时间的项目,是用websphere做生产环境的,可是一旦加载的项目过多,webSphere就很傲娇的内存溢出,这是一个折腾了公司里某个前辈很久很久的问题,因为是测试版,所以各种官方文档说 ...
- [原创]Matlab2016b打包为C++的lib文件
这几天在研究如何将Matlab的程序导入到C++进行调用. 由于需要使用到不少Matlab函数,所以之前就有些担心这些函数在导出后是否能够继续使用.不过之后觉得既然已经导出成了一个单独文件,相关运算应 ...
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- FTP下载文件失败
这几天的定时任务下载文件的脚本失败了. 于是手工执行测试,发现报550 Permission denied. Passive mode refused. 意思就是被动模式下,没有权限获取文件. 解决方 ...
- synchronized使用说明
好久没有更新博客了,今天试着用简单的语言把synchronized的使用说清楚. synchronized是什么? synchronized是用来保证在多线程环境下代码同步执行的可重入的互斥锁.所谓互 ...
- C#调用百度地图 api
转 http://blog.csdn.net/kkkkkxiaofei/article/details/8663377 这一篇,记录一下我调用的地图API实现的功能.下面介绍的都是一些片段的节选,不 ...
- Sicily 1153: 马的周游问题(DFS+剪枝)
这道题没有找到一条回路,所以不能跟1152一样用数组储存后输出.我采用的方法是DFS加剪枝,直接DFS搜索会超时,优化的方法是在搜索是优先走出度小的路径,比如move1和move2都可以走,但是如走了 ...
- xampp 下安装mysql-python
pip install mysql-python修改路径PATH="$PATH":(/mysql/bin 路径)brew install mysql-connector-c
- zepto返回顶部动画
点击返回顶部 function goTop(acceleration, time) { acceleration = acceleration || 0.1; time = time || 16; v ...
- Mac 编写oracle 连接脚本
首先需要本地存有sqlplus命令, 如果没有则需要到官网下载 也可点击我进行下载 (解压 readme.txt 有安装配置说明): 在Oracle官网下载instant client for os ...