POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19528 | Accepted: 5329 |
Description
For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you
can write the pair of integers 10 and 2467. Of course, there are many
ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The
absolute value of the difference between the integers in the last pair
is 28, and it turns out that no other pair formed by the rules above can
achieve a smaller difference.
Input
first line of input contains the number of cases to follow. For each
case, there is one line of input containing at least two but no more
than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit
appears more than once in one line of the input. The digits will appear
in increasing order, separated by exactly one blank space.
Output
each test case, write on a single line the smallest absolute difference
of two integers that can be written from the given digits as described
by the rules above.
Sample Input
1
0 1 2 4 6 7
Sample Output
28 题意:n个数字分成两部分,构成两个整数,求这两个整数的最小差 题解:1、直接用函数next_pertumation()全排列
2、用dfs实现next_pertumation()功能
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int t,cnt,ans;
int a[];
string s; int main()
{
cin>>t;
getchar();
while(t--)
{
cnt=,ans=;
getline(cin,s);
for(int i=;s[i];i++)
{
if(isdigit(s[i]))
a[cnt++]=s[i]-'';
}
if(cnt==)//特例判断 0 1
{
cout<<abs(a[]-a[])<<endl;
continue;
}
while(a[]==)//如果首位数为0,在排一次序之后就不是了
next_permutation(a,a+cnt);
do
{
if(a[cnt/]!=)//首位不能为0
{
int num1=,num2=;
for(int i=;i<cnt/;i++)
num1=num1*+a[i];
for(int i=cnt/;i<cnt;i++)
num2=num2*+a[i];
ans=min(ans,abs(num1-num2));
} }while(next_permutation(a,a+cnt));
cout<<ans<<endl;
}
return ;
}
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int t,cnt,ans;
int a[],b[],vis[];
string s; void dfs(int dep)
{
if(dep==cnt)
{
int num1=,num2=;
for(int i=;i<cnt/;i++)
num1=num1*+b[i];
for(int i=cnt/;i<cnt;i++)
num2=num2*+b[i];
ans=min(ans,abs(num1-num2));
return ;
} for(int i=;i<cnt;i++)
{
if(vis[i]==)
continue;
if(dep==&&a[i]==)
continue;
if(dep==cnt/&&a[i]==)
continue;
vis[i]=;
b[dep]=a[i];
dfs(dep+);
vis[i]=;
}
}
int main()
{
cin>>t;
getchar();
while(t--)
{
memset(vis,,sizeof(vis));
cnt=,ans=;
getline(cin,s);
for(int i=;s[i];i++)
{
if(isdigit(s[i]))
a[cnt++]=s[i]-'';
}
if(cnt==)
{
cout<<abs(a[]-a[])<<endl;
continue;
}
if(cnt==)//防止tle
{
cout<<<<endl;
continue;
}
dfs();
cout<<ans<<endl;
}
return ;
}
POJ 2718 Smallest Difference dfs枚举两个数差最小的更多相关文章
- 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(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- 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
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
随机推荐
- leetcode 0218
目录 ✅ 1200. 最小绝对差 描述 解答 cpp py ✅ 897. 递增顺序查找树 描述 解答 cpp 指针问题? fuck ptr py ✅ 183. 从不订购的客户 描述 解答 sql to ...
- openjudge 和为给定数(二分答案)
嗯... 题目链接:http://noi.openjudge.cn/ch0111/07/ 这道题是一道不太明显,但很好二分的二分答案的一道题... 首先排序(二分要满足单调性),然后枚举每一个数,在[ ...
- System.Web.Compilation.BuildManager.CopyPrecompiledFile 並未將物件參考設定為物件的執行個體
使用MSBUild 的 aspnet_compiler.exe 发布网站, 过程中出现错误 [NullReferenceException]: 並未將物件參考設定為物件的執行個體 System.W ...
- 牛顿迭代法--求任意数的开n次方
牛顿迭代法是求开n次方近似解的一种方法,本文参考. 引言 假如\(x^n = m\),我们需要求x的近似值. 我们设\(f(x) = x^n - m\), 那么也就是求该函数f(x)=0时与x轴的交点 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:为所有表格的单元格添加边框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Python学习笔记009
不换行 print("Hello,world!",end='')print("Hello,world!",end='')print("Hello,wo ...
- Linux命令:ss命令
ss功能:用来显示套接字信息的,类似于netstat,可以显示更多的信息,用于替代netstat. ss常用选项 ss -t:tcp协议的连接 -u:udp协议的链接 -w:裸套接字相关 -x:uni ...
- 捣鼓FileZilla
今天突然对ftp服务器感兴趣,于是随意打了一个ftp词条,发现了FZ官网,好奇点进去下载了之后,捣鼓了一会.于是,也写一个小教程记录一下吧,害怕自己以后忘记怎么弄的了. 首先需要用到两个,一个是FZ ...
- Python学习 —— 实现简单的爬虫
为了加快学习python3.x,查了许多资料后写了这个脚本,这个脚本主要是爬取百度图片'东方幻想乡'的图片,但还是有很多问题存在. 下面给出代码: # 更新了一下代码 from urllib impo ...
- webpack配置文件里loader的执行顺序:从下到上,从右到左; css-loader开启css模块化modules: true,
注释: options:{ importLoaders: 2 } 解决样式文件里使用@import 'xxx.xxx' 的问题 module: { rules: [{ test: /\.scss$/, ...