题目链接:

https://vjudge.net/problem/POJ-2718

题目大意:

有一列数,对其任意分成两组,每组按一定顺序可以组成一个数。问得到的两个数的差最小是多少。

思路:

直接dfs构造就行,注意不能有前导0。而且有数据需要特判

只有一个数字的时候需要特判,还有只有两个数字的时候也需要特判,这是因为如果含有0和其他的数字,由于在构造中不允许前导0的出现,所以如果有这种情况的话更新不了minans,所以只有两个数字的时候也需要特判

 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m, minans;
const int maxn = 1e5 + ;
int dir[][] = {,,,,-,,,-};
int a[];
bool v[];
void dfs(int num, int d)
{
if(d == n / )//递归出口
{
int b[], tot = ;
for(int i = ; i < n; i++)
if(!v[i])b[tot++] = a[i];
sort(b, b + tot);
do
{
if(!b[])continue;
int x = ;
for(int i = ; i < tot; i++)x = x * + b[i];
minans = min(minans, abs(num - x));
}while(next_permutation(b, b + tot));
return;
}
for(int i = ; i < n; i++)
{
if(!v[i])
{
v[i] = ;
dfs(num * + a[i], d + );
v[i] = ;
}
}
}
int main()
{
cin >> T;
getchar();
while(T--)
{
string s;
n = ;
minans = INF;
getline(cin, s);
stringstream ss(s);
while(ss >> a[n++]);
n--;//此处需要自减一,因为n在ss流完的时候又加了一
if(n == )//特例特判
{
cout<<abs(a[])<<endl;
}
else if(n == )
{
cout<<(abs(a[] - a[]))<<endl;
}
else
{
for(int i = ; i < n; i++)
{
memset(v, , sizeof(v));
if(!a[i])continue;
v[i] = ;
dfs(a[i], );
}
cout<<minans<<endl;
}
}
return ;
}

POJ-2718 Smallest Difference---DFS的更多相关文章

  1. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

  2. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  3. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  4. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  5. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  6. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  7. POJ 2718 Smallest Difference【DFS】

    题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...

  8. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  9. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  10. POJ - 2718 Smallest Difference(全排列)

    题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...

随机推荐

  1. CROSS APPLY和 OUTER APPLY 区别详解

    SQL Server 2005 新增 cross apply 和 outer apply 联接语句,增加这两个东东有啥作用呢? 我们知道有个 SQL Server 2000 中有个 cross joi ...

  2. 数据结构---Java---ArrayList

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...

  3. LISP语言学习资源

    LISP的介绍:Paul Graham 的主页 http://paulgraham.com/index.html Lisp之根源 - 保罗·格雷厄姆 http://daiyuwen.freeshell ...

  4. Python collections

    #count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', ...

  5. idea编译golang插件总结

    由于使用习惯了Idea 和vim插件.于是下载了idea的go插件并安装,可惜不支持go1.4 ,官方的go插件版本太低 133.326 — 133.9999 .只能手动编译 按照这个教程就可以 ht ...

  6. Nginx主主负载均衡架构

    在和一些朋友交流Nginx+Keepalived技术时,我虽然已成功多次实Nginx+Keepaived项目方案,但这些都是用的单主Nginx在工作,从Nginx长期只是处于备份状态,所以我们想将二台 ...

  7. DEDE列表页和内容页调用顶级栏目ID的方法

    dede模板中添加顶级栏目id的方法总结,使用dede顶级栏目id可以实现很多功能.比如,在每个列表页调用不同的栏目图片(同一顶级栏目调用相同的图片),如果我们做N个栏目就意味着要做N个列表页模板,显 ...

  8. Oracle安装后忘记用户名或密码+创建新登陆用户

    新安装的Oracle11g,不料在使用的时候没记住安装时的用户名和密码. 不用担心,打开sqlplus. 按如下步骤,新建一个登陆用户: 第一步:以sys登陆  sys/密码 as sysdba  此 ...

  9. VS Code Plugins And Configuration

    VS Code插件 vscode-icons: 显示文件类型的图标 project manager: 管理项目, 项目的保存加载与切换 beautify: 控制缩进 code runner: 执行代码 ...

  10. log4net 基础

    log4net:日志输出工具. 新建工程Log4NetDemo App.config配置如下: <?xml version="1.0" encoding="utf- ...