POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用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,剪枝)的更多相关文章
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 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.枚举 ...
随机推荐
- D. Beautiful Array
题目:http://codeforces.com/contest/1155/problem/D 给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 竟然是很简单的d ...
- Java web错误汇总
环境: 电脑:win 10 IDE: Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 2 (4.4.2) B ...
- Python中的矩阵、多维数组:Numpy
Numpy 是Python中科学计算的核心库.它提供一个高性能多维数据对象,以及操作这个对象的工具.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对 ...
- CF 980D Perfect Groups(数论)
CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...
- 置换群(本蒟蒻瞎BB的)(未完)
置换群(本蒟蒻瞎BB的)(未完) 群的定义 给定一个集合\(G=\{a, b, c...\}\)和集合\(G\)上的二元运算*,并满足: 封闭性:\(\forall a, b \in G, \exis ...
- P2939 [USACO09FEB]改造路Revamping Trails(分层图最短路)
传送门 完了我好像连分层图最短路都不会了……果然还是太菜了…… 具体来说就是记录一个步数表示免费了几条边,在dijkstra的时候以步数为第一关键字,距离为第二关键字.枚举边的时候分别枚举免不免费下一 ...
- 关于在SSM框架下使用PageHelper
首先,如果各位在这块配置和代码有什么问题欢迎说出来,我也会尽自己最大的能力帮大家解答 这些代码我都是写在一个小项目里的,项目的github地址为:https://github.com/Albert-B ...
- MySQL 逻辑备份mysqldump&mysqlpump&mydumper原理解析
目录 准备 mysqldump备份 mysqlpump备份 mydumper备份 想弄清除逻辑备份的原理,最好的办法是开启general_log,一探究竟 准备 创建用户 CREATE USER IF ...
- FJOI2019全记录(福建省选)
Day 0 最后一个早上,早读完后就到机房里,复习了左偏树和计算几何,然后真的不知道还要做什么了(FFT和一些数论的结论昨天复习过了 也许还要去学一点新东西(?),像Krusk重构树什么的.(一直没有 ...
- Orthanc+OHIF DICOM Viewer最佳Dicom解析、在线浏览实践指南(解决方案)
概念 DICOM identifiers Very importantly, the DICOM standard specifies DICOM tags that allow to index e ...