POJ-2718 Smallest Difference---DFS
题目链接:
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的更多相关文章
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- 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(贪心 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】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
随机推荐
- Linux多线程及线程同步简单实例
一.多线程基本概念 1. 线程的基本概念 ① 线程就是轻量级的进程 ②线程和创建他的进程共享代码段.数据段 ③线程拥有自己的栈 2. 在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互 ...
- oracle merge into函数中插入clob字段
当使用Merge into 函数向ORACLE数据库中插入或更新数据时,报错“ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”,使用如下方法可以把String转换为clob. 需 ...
- pat06-图5. 旅游规划(25)
06-图5. 旅游规划(25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该 ...
- Hadoop学习笔记(3) Hadoop文件系统一
1. 分布式文件系统,即为管理网络中跨多台计算机存储的文件系统.HDFS以流式数据访问模式来存储超大文件,运行于商用硬件集群上.HDFS的构建思路为:一次写入.多次读取是最高效的访问模式.数据集通常由 ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- BNU 27847——Cellphone Typing——————【字典树】
Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...
- linux下常用命令备忘
转自:Linux 命令集锦 linux下查看监听端口对应的进程 # lsof -i:9000 # lsof -Pnl +M -i4 如果退格键变成了:"^h". 终端连接unix删 ...
- asp.net webForm也可以这样用Ajax -- My Ajax Framework [全屏看文]
对于asp.net WebForm项目,进行Ajax操作大概有三种方式:web服务(.asmx文件) , 一般处理程序(.ashx)和 一些Ajax控件. 对于.net提供的ajax控件,暂且不 ...
- 获得数据库image图片二进制
/// <summary> /// 获得图片二进制 /// </summary> /// <param name="u ...
- golang精华资源
转载自:http://blog.csdn.net/songbohr/article/details/13292261 1.Learning Go <学习Go语言> http://www.m ...