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.枚举 ...
随机推荐
- 管理时间TED语录
When people find out I write about time management, They assume two things. One is that I'm always o ...
- Go:坑之for range
go只提供了一种循环方式,即for循环,在使用时可以像c那样使用,也可以通过for range方式遍历容器类型如数组.切片和映射.但是在使用for range时,如果使用不当,就会出现一些问题,导致程 ...
- MCP|MZL|Accurate Estimation of Context- Dependent False Discovery Rates in Top- Down Proteomics 在自顶向下蛋白组学中精确设定评估条件估计假阳性
一. 概述: 自顶向下的蛋白质组学技术近年来也发展成为高通量蛋白定性定量手段.该技术可以在一次的实验中定性上千种蛋白,然而缺乏一个可靠的假阳性控制方法阻碍了该技术的发展.在大规模流程化的假阳性控制手段 ...
- C# Stack堆栈的使用方法
堆栈(Stack)代表了一个后进先出的对象集合.当您需要对各项进行后进先出的访问时,则使用堆栈.当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素. Stack 类的方法和属性 ...
- jenkins+maven+Tomcat+shell构建自动化部署
https://yq.aliyun.com/articles/685931 1.官网下载war包:jenkins本质上就是一个web应用,直接下载jenkins的war包通过tomcat运行即可.ht ...
- 洛谷P2826 LJJ的数学课
题目背景 题目描述(本题是提高组第二题难度+) 题目描述 \(LJJ\)又要开始上数学课啦!(\(T1\),永恒不变的数学) \(LJJ\)的\(Teacher\)对上次的考试很不满意(其实是出题人对 ...
- Angular学习笔记 ——input 标签上的【name属性】和【ngModelOptions属性】
利用“@angular/forms" 创建<form>表单的时候,系统默认会创建一个”FormGroup"的对象. 使用带有“ngModel"的”<in ...
- javascript 返回上一页面:onclick="javascript:history.back(-1);"
<a href="<a href="javascript :history.back(-1)">返回上一页</a>或<a href=& ...
- Spark操作
### scala源码 /* SimpleApp.scala */ import org.apache.spark.SparkContext import org.apache.spark.Spark ...
- 【手撸一个ORM】MyOrm的使用说明
[手撸一个ORM]第一步.约定和实体描述 [手撸一个ORM]第二步.封装实体描述和实体属性描述 [手撸一个ORM]第三步.SQL语句构造器和SqlParameter封装 [手撸一个ORM]第四步.Ex ...