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.枚举 ...
随机推荐
- Spring MVC 基于Method的映射规则(注解版)
在Restful风格的web开发中,根据不同的请求方法使用相应的控制器处理逻辑成为核心需求,下面就看看如何在Spring MVC中识别不同的请求方法. 请求方法 在Http中,请求的方法有很多种,最常 ...
- MCP|ZWT|Precision de novo peptide sequencing using mirror proteases of Ac-LysargiNase and trypsin for large-scale proteomics(基于Ac-LysargiNase和胰蛋白酶的蛋白组镜像de novo测序)
一.概述 由于难以获得100%的蛋白氨基酸序列覆盖率,蛋白组de novo测序成为了蛋白测序的难点,由Ac-LysargiNase(N端蛋白酶)和胰蛋白酶构成的镜像酶组合可以解决这个问题并具有稳定性, ...
- Gradle用户使用指南
转载请事先沟通,未经允许,谢绝转载. 1. 新工具介绍(Introduction) 能够复用代码和资源能够构建几种不同版本参数的应用能够配置.扩展.自定义构建过程1.1 为什么选择Gradle(Why ...
- k8s 更新应用程序
参考:https://kubernetes.io/docs/tutorials/kubernetes-basics/ 更新应用程序 用户希望应用程序始终可用,开发人员每天需要部署几次新版本的应用程序. ...
- array类型的方法
var arr1 = [12,454,'dafda','feagfag',23]; var arr2 = [46468,'ffwfe','dafs','dfsfs']; arr1.indexOf('d ...
- Helvetic Coding Contest 2016 online mirror F1
Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...
- 匿名内部类(new类时覆盖类中方法)
public class Person { private String name ; protected String getName() { return name; } public void ...
- weblogic.xml
<?xml version="1.0" encoding="UTF-8"?> <weblogic-web-app xmlns="ht ...
- LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- SpringBoot | 第十九章:web应用开发之WebSocket
前言 web开发也讲解了三章了,这章节开始讲解关于与前端通信相关知识.实现一个在线聊天室类似的功能或者后端推送消息到前端,在没有WebSocket时,读大学那伙还有接触过DWR(Direct Web ...