枚举两个排列以及有那些数字,用dfs比较灵活。

dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大,

后面最优全是0,如果全是0都没有当前ans小的话就剪掉。

(第1个dfs完了,忘了加return。。。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std; //string line;
int line[];
bool vis[];
int len1,len2, n;
const int wei[] = {,,,,,,};
int ans;
int num1; void dfs2(int d,int cur)
{
if(abs(num1 - cur*wei[len2-d]) >= ans) return;
if(d == len2){
ans = min(ans,abs(num1-cur));
return;
}
for(int j = ; j < n; j++){
if(!vis[j]){
vis[j] = true;
dfs2(d+,cur*+line[j]);
vis[j] = false;
}
}
} //permutation
void dfs1(int d,int cur)
{
if(d == len1){
num1 = cur;
for(int fi = ; fi < n; fi++){
if(!vis[fi] && line[fi]){
vis[fi] = true;
dfs2(,line[fi]);
vis[fi] = false;
}
}
return;
}
for(int j = ; j < n; j++){
if(!vis[j]){
vis[j] = true;
dfs1(d+,cur*+line[j]);
vis[j] = false;
}
}
} //0 2 , 0 3 //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T; scanf("%d",&T);
scanf("\n");
while(T--){
char ch;
n = ;
while((ch = getchar())!= '\n'){
if(isdigit(ch)){
line[n++] = ch-'';
}
}
if(n == ){ printf("%d\n",abs(line[]-line[])); continue; }
len1 = n>>; len2 = n-len1;
ans = <<;
memset(vis,,sizeof(vis));
for(int fi = ; fi < n; fi++){
if(line[fi]){
vis[fi] = true;
dfs1(,line[fi]);
vis[fi] = false;
}
}
printf("%d\n",ans);
}
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(最小差)

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

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

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

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

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

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

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

  6. POJ 2718 Smallest Difference【DFS】

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

  7. 穷竭搜索: POJ 2718 Smallest Difference

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

  8. POJ 2718 Smallest Difference 枚举

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

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

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

随机推荐

  1. [WIP]php入門

    创建: 2019/06/19 安装  MAMP   变量与运算符  php标签  <?php ... ?> <?php ... ?> ● 在文件最后的 ?> 通常省略, ...

  2. The Largest Generation (25)(BFS)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;int n,m,l,t;int a[1307][137][67];int vis[1307][137] ...

  3. 【转】vs发布msi程序详解

    源地址:http://wenku.baidu.com/link?url=MV1Mf7IukCZ0cab8AzXQoQ3MAXeUAHGz5b2IuUL4Kw-hCI90ZyBKXwKeQA3t3-SV ...

  4. 洛谷P1065 作业调度方案

    P1065 作业调度方案 题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作 ...

  5. java线程池之一:创建线程池的方法

    在Java开发过程中经常需要用到线程,为了减少资源的开销,提高系统性能,Java提供了线程池,即事先创建好线程,如果需要使用从池中取即可,Java中创建线程池有以下的方式, 1.使用ThreadPoo ...

  6. php类内方法使用类外变量和类外函数

    如果要调用另一个类的属性和方法,直接实例化后调用就可以 但是要使用的变量和函数是独立的,不在本类或其他类里面,这时调用就要用下面的方法(本例是在同一个php文件里,如果不再同一个类文件可以requir ...

  7. HelloSpock

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. Java中常用的数据源

    几种常用的Java数据源解决方案 Java中的数据源就是javax.sql.DataSource.DataSource的创建可以有不同的实现. JNDI方式创建DataSource 以JNDI方式创建 ...

  9. F. Gourmet and Banquet(贪心加二分求值)

    题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...

  10. python3+Appium自动化05-xpath定位

    概念 xpath定位是一种路径定位方式,主要是依赖于元素绝对路径或者相关属性来定位,但是绝对路径xpath执行效率比较低(特别是元素路径比较深的时候),一般使用比较少.通常使用xpath相对路径和属性 ...