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.枚举 ...
随机推荐
- MessageFormat 格式化String
public static String buildFailureString(AtomicInteger count, String cause) { return MessageFormat.fo ...
- sqlserver 常用语法
sqlserver查找 table, view, column select * from information_schema.tables where table_schema='bk' sele ...
- git 的搭建与使用
公司之前用的是vpn,然后老大说让我搞一个git.于是,我开始了git的研究之路.... 概念:(说实话,看了还是有些不太理解) git 是一种版本控制系统,是一个命令,是一种工具 g ...
- c++ 封装线程库 2
1.2线程回收: 首先得知道线程的两个状态: Joinable Detached 简单理解,如果一个线程是joinable的状态,那么这样的线程,就必须使用pthread_join来回收,否则程序结束 ...
- leetcode 175 Combine Two Tables join用法
https://leetcode.com/problemset/database/ ACM退役,刚好要学sql,一定要刷题才行,leetcode吧. 这一题,说了两个表,一个左一个右吧,左边的pers ...
- 《Flink 源码解析》—— 源码编译运行
更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...
- lftp 快速使用
登录 lftp username:password@ip:port 设置字符集 set ftp:charset 'gbk' set ftp:charset 'utf-8' 下载文件 mget *.tx ...
- Windbg工具使用
https://www.cnblogs.com/startpoint/p/4194052.html https://www.cnblogs.com/lyl6796910/p/7613664.html ...
- CSS选择器备忘录
CSS选择器备忘录 基本选择器 Selector Meaning Example 通用选择器 匹配任何元素 * 标签选择器 CSS1中称之为元素选择器,匹配为指定标签的所有元素 div 伪元素选择器 ...
- vue-样式问题
问题: 今天在用vue开发单页面应用的时候,遇到一个问题,在A页面,直接刷新,页面的布局样式之类的是没有问题的,不过在B页面跳转到A页面,那么A页面有一些样式就不是预期的效果. 发现解决问题: 用调试 ...