Smallest Difference

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19528   Accepted: 5329

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

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

The
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

For
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枚举两个数差最小的更多相关文章

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

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

  2. POJ 2718 Smallest Difference(最小差)

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

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

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

  4. POJ 2718 Smallest Difference 枚举

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

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

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

  6. POJ 2718 Smallest Difference【DFS】

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

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

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

  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(全排列)

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

随机推荐

  1. Python笔记3

    类格式示例: class Student(): name = '' age = 0 def print_file(self): print('name:' + self.name) print('ag ...

  2. 第十六节:Linq用法大全(四)

    1. OfType 获取集合中中指定类型元素. , , , , , "aaa", "bbb" }; int max = obj.OfType<int> ...

  3. 使用IDEA导入一个Maven风格的SSM项目

    转自: 方法一: (我用的这种,导入的方法 File->New->Project from existing sources)(同理,important也是一样的) https://how ...

  4. 「JSOI2010」挖宝藏

    「JSOI2010」挖宝藏 传送门 由于题目中说道挖一个位置的前提是挖掉它上面的三个,以此类推可以发现,挖掉一个点就需要挖掉这个点往上的整个倒三角,那么也就会映射到 \(x\) 轴上的一段区间(可以发 ...

  5. Lesson 16 The modern city

    What is the author's main argument about the modern city? In the organization of industrial life the ...

  6. 十五 OGNL的入门

    一.访问对象的方法

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:列表

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Excel使用小技巧

    1.Excel随机设置单元格的内容为整数0或1: 在单元格中写公式:  =ROUND(RAND(),0) 2.设置某个单元格的值为1或0,根据其他3个单元格的值为0或1来确定: 在该单元格中写公式: ...

  9. RGB 和 YUV 的转换公式

  10. SpringMVC 接收表单数据、数据绑定、解决请求参数中文乱码

    接收表单数据有3种方式. 1.使用简单类型接收表单数据(绑定简单数据类型) 表单: <form action="${pageContext.request.contextPath}/u ...