穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718
题意:
就是输入N组数据,一组数据为,类似 【1 4 5 6 8 9】这样在0~9之间升序输入的数据,然后从这些数据中切一刀,比如 n1:【1 4 5】,n2:【6 8 9】这样,然后 abs(n1- n2),对n1 和 n2的所有可能的排列 n1: 【1 4 5】【1 5 4】...这样,要算出来的最小的差,显然从中间切一刀才会出现这种解。
题解:
这里可以用来练习 STL,算法不会也没有关系,可以在这题学到 bitset 的用法,类模板 bitset 表示一个 N 位的固定大小序列。可以用标准逻辑运算符操作位集,并将它与字符串和整数相互转换。如:bitset<10> used = static_cast<bitset<10>>(permute),10个二进制位, permute表示这个数的二进制表示形式;比如:permute=3; used就是 0000000011; used[0] = used[1] = 1; 这个低位在右边
我们这样,设输入字符串长度 len, 我们遍历 2 ^ len种可能, used[i] == 1,则 s1 += line[i] ;否则 s2 += line[i];这样避免了 s1 = "1 4 5", s2 = "6 8 9" 或 s1 = "6 8 9", s2 = "1 4 5" 这种重复的情况。这样 s1, s2就被切割出来了。再对 s1, s2字符串的位置进行搜索, s1 = "123", "132"....这样
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <bitset>
using namespace std; int T;
const int INF = ; void solve()
{
scanf("%d", &T);
getchar(); //下面输入 带空格的字符串, 需要吃掉输入T的回车,再可以
while (T--)
{
string line;
getline(cin, line);
line.erase(remove(line.begin(), line.end(), ' '), line.end()); //擦除' '
int ans = INF;
int half = line.size() / ;
int len = line.size();
int permute = << len; // 2 ^ len ; do {
//10个二进制位, permute表示这个数的二进制表示形式; permute=3; used就是 0000000011;
bitset<> used = static_cast<bitset<>>(permute); // used[0] = used[1] = 1; 这个低位在右边
// cout << "Debug: " << used << endl; //可以输出used看看
string s1, s2;
for (int i = ; i < len; i++)
{
if (used[i])
{
s1 += line[i];
}
else
{
s2 += line[i];
}
}
if ((s1.size() != half) || (s1[] == '' && s1.size() > ))
{
continue;
}
//s1 s2已经被切割出来了
//s1, s2字符串的位置进行搜索, s1 = "123", "132"....这样
do
{
int n1 = atoi(s1.c_str());
do {
if (s2[] == '' && s2.size() > ) {
continue;
}
int n2 = atoi(s2.c_str());
int diff = abs(n1 - n2);
if (diff < ans) {
ans = diff;
}
} while (next_permutation(s2.begin(), s2.end()));
} while(next_permutation(s1.begin(), s1.end()));
} while (--permute); printf("%d\n", ans);
} } int main()
{
solve(); return ;
}
穷竭搜索: POJ 2718 Smallest Difference的更多相关文章
- 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枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 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 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
随机推荐
- Hibernate left join
6.4.5 左外连接 左外连接(Left Outer Join)查询出左表对应的复合条件的所有记录,如查询李晓梅同学的选课信息.下面是类HQLLeftOuterJoinQuery的源代码. 其实关联 ...
- 推荐一个Markdown数学公式编辑器——Haroopad & Mathjax
要在Markdown里插入数学公式,如果没有好用的的引擎or编辑器,那么只能插入图片了,十分麻烦.这里推荐一个十分强大的数学公式引擎--Mathjax. 配置 有道云笔记目前不支持浏览MathJax公 ...
- HDU 2123 An easy problem
http://acm.hdu.edu.cn/showproblem.php?pid=2123 Problem Description In this problem you need to make ...
- TADOConnection.Close - connection still active on MS-SQL server
I have several Delphi programs (XE3), that use a TADOConnection to connect to a MS-SQL Server. I rec ...
- Error: Unable to access jarfile D:\Apache\apache-jmeter-3.0\bin\ApacheJMete.jar
双击jmeter.bat后,在cmd窗口显示Error: Unable to access jarfile D:\Apache\apache-jmeter-3.0\bin\ApacheJMete.ja ...
- 性能分析_linux服务器CPU_中断
中断 1. 指标范围 1.1 Interrupt rate 应该与cpu利用率结合分析,如果cpu利用率在合理范围内,大量的中断也是可以接受的.一个巨大的中断值,同时伴随着缓慢的系统性能表现,指示 ...
- Mxnet Windows配置
MXNET Windows 编译安装(Python) 本文只记录Mxnet在windows下的编译安装,更多环境配置请移步官方文档:http://mxnet.readthedocs.io/en/lat ...
- PGM学习之六 从有向无环图(DAG)到贝叶斯网络(Bayesian Networks)
本文的目的是记录一些在学习贝叶斯网络(Bayesian Networks)过程中遇到的基本问题.主要包括有向无环图(DAG),I-Maps,分解(Factorization),有向分割(d-Separ ...
- [BZOJ3230] 相似字串 后缀数组+RMQ
3230: 相似子串 Time Limit: 20 Sec Memory Limit: 128 MB Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数.第2行是字符 ...
- MT【148】凸数列
(2018浙江省赛13题) 设实数$x_1,x_2,\cdots,x_{2018}$满足$x_{n+1}^2\le x_nx_{n+2},(n=1,2,\cdots,2016)$和$\prod\lim ...