POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Description
For example, if you are given the digits 0, 1, 2, 4, 6
and 7, you can write the pair of integers 10 and 2467. Of course, there are many
ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute
value of the difference between the integers in the last pair is 28, and it
turns out that no other pair formed by the rules above can achieve a smaller
difference.
Input
to follow. For each case, there is one line of input containing at least two but
no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit
appears more than once in one line of the input. The digits will appear in
increasing order, separated by exactly one blank space.
Output
smallest absolute difference of two integers that can be written from the given
digits as described by the rules above.
Sample Input
1
0 1 2 4 6 7
Sample Output
28
Source
题意:给一升序集合 集合中元素范围为1~9 从中寻找两个不相交子集(每个数只能用一次) 求这两个子集组成两个整数的差最小值
这题要注意一个情况,除了组成的数只有0,否则都不能以0为开头,如01是不存在的, 它并不等于1。 如果输入012,那么答案是 10-2 = 8,并不是2-1(01) = 1。
分析:这道题数据量很小 可以用next_permutation 来枚举所有的情况 由于只是要找差最小 所以枚举完只需要把枚举的情况从前到后组成两个位数相等(偶数情况) 或者 位数差1(奇数情况)的数 然后相减取绝对值(避免先后问题),然后取这些情况的最小值即可。
next_permutation函数将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为减序为止,使用方法是next_permutation(begin(),end())。
代码中以一个地方写的很妙 可以排除所有首元素为0的情况:
while(a[0] ==0)
next_permutation(a,a+cnt);//第一个数不能为0
因为next_permutation函数不会重复生成已经生成过的序列
这样就先排除了所有首数字为0的情况
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
char aa[];
int a[];
int cnt, mid, ans;
int main()
{
int t;
scanf("%d", &t);
getchar();
while(t--)
{
cnt = ;
int t;
while((t = getchar()) != '\n')
{
if(t != ' ')
{
aa[cnt] = t;
cnt++;
}
} int mid = cnt/;
ans = 1e9;
if(cnt == )
{
printf("%d\n",aa[]-aa[]);
continue;
}
for(int i = ; i < cnt; i++)
{
a[i] = aa[i] - '';
}
while(a[] ==)
next_permutation(a,a+cnt);//第一个数不能为0
do
{
if(a[mid])
{
int t1=,t2=;
for(int i=;i<mid;++i)
t1=t1*+a[i];
for(int i=mid;i<cnt;++i)
t2=t2*+a[i];
ans=min(ans,abs(t1-t2));
} }while(next_permutation(a,a+cnt));
printf("%d\n",ans); }
}
Next_permutation
前面说过 这题数据量很小 可以用枚举来实现 复杂度为O(n²) 但如果数据量增大 枚举就行不通了 这时候可以使用贪心的思想来做这道题。
设t1为大数 t2为小数
如果是奇数的情况 那么因为t1的位数比t2多一位 那么t1最高位挑选最小的元素 t2最高位挑选次小的元素 然后t1依次挑选尽可能小的元素 t2依次挑选尽可能大的元素 组成的数t1-t2就是答案
如果是偶数的情况 那么就需要枚举一下每个相邻的位 t1的最高位挑选相邻位置中较大的元素 t2最高位挑选相邻位置较大的元素 然后t1依次挑选尽可能小的元素 t2挑选尽可能大的元素
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
char aa[];
int a[];
int cnt, mid;
int main()
{
//freopen("1.txt","r",stdin);
//freopen("2.txt","w",stdout);
int t;
scanf("%d", &t);
getchar();
while(t--)
{
cnt = ;
int t;
while((t = getchar()) != '\n')
{
if(t != ' ')
{
aa[cnt] = t;
cnt++;
}
} int mid = cnt/; // printf("%d\n",cnt);
if(cnt == )
{
printf("%d\n",aa[]-aa[]);
continue;
}
for(int i = ; i < cnt; i++)
{
a[i] = aa[i] - '';
}
if(cnt % == )//偶数情况
{
int t1,t2;//t1小数 t2大数
bool vis[];
memset(vis,,sizeof(vis));
int ans = 1e9; for(int i = ; i < cnt-;i++)
{
if(a[i] == ) continue;
t1 = a[i];
t2 = a[i+];
// printf("1 %d %d\n",t2, t1);
vis[i]=;vis[i+] = ;
int ti = ,pos = cnt - ;
while(ti < cnt/- && pos >= )
{
if(!vis[pos])
{
t1 = t1* + a[pos];
vis[pos] = ;
ti++;
}
pos--;
}
ti = ,pos = ;
while(ti < cnt/- && pos < cnt)
{
if(!vis[pos])
{
t2 = t2* + a[pos];
ti++;
vis[pos] = ;
}
pos++;
}
memset(vis,,sizeof(vis));
// printf("2 %d %d\n",t2, t1);
ans = min(ans,t2- t1);
} printf("%d\n",ans);
}
else//奇数情况
{
int t1 , t2;//t1为大数 t2 为小数
if(a[] != )
{
t1 = a[];
t2 = a[cnt-];
for(int i = ; i <= mid;i++)
{
if(i == ) continue;
t1 = t1* + a[i];
}
for(int i = cnt-; i > mid; i--)
{
t2 = t2* + a[i];
}
printf("%d\n",t1-t2);
}
else
{
t1 = a[];
t2 = a[cnt-];
for(int i = ; i <= mid;i++)
{
if(i == ) continue;
t1 = t1* + a[i];
}
for(int i = cnt-; i > mid; i--)
{
t2 = t2* + a[i];
}
printf("%d\n",t1-t2);
} } }
}
贪心
POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)的更多相关文章
- 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 dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 穷竭搜索: 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【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- 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(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
随机推荐
- 动态规划基础复习 By cellur925
结束帝都的qbxt dp图论精讲班后,感觉自己依然很水,接下来的一周,不妨来复习一下讲课内容:) 动态规划是lyd讲的,上次在泉城讲数据结构,然鹅体验较差,这次虽说好了些,但还是比想象中的效果不好(还 ...
- 手机端实现6位短信验证码input输入框效果(样式及代码方法)
微信移动端4位.6位.多位验证码密码输入框功能的实现代码,实现思路: 方案1: 写一个简单的input框. 评估:样式不好看,待定. 方案2: 就是用6个input框,每输入一个数字之后,切换到下一个 ...
- 乐搏讲自动化测试-Python适用公司类型(6)
相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...
- c++中快速排序
(一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...
- win10 SQL Server 配置管理器无法启动
解决方法: 用管理员方式打开命令行 进入 “C:\Program Files (x86)\Microsoft SQL Server\140\Shared”,(有的是“C:/Program Files/ ...
- IIS发布问题服务器配置
1. <validation validateIntegratedModeConfiguration="false" /> 2.Http 404.0-NotFound ...
- _bzoj1015 [JSOI2008]星球大战starwar【并查集】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 倒过来做就ok了. #include <cstdio> #include ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- 数学 Codeforces Round #308 (Div. 2) B. Vanya and Books
题目传送门 /* 水题:求总数字个数,开long long竟然莫名其妙WA了几次,也没改啥又对了:) */ #include <cstdio> #include <iostream& ...
- React Native for Android 学习
前言 Facebook 在2015.9.15发布了 React Native for Android,把 JavaScript 开发技术扩展到了移动Android平台.基于React的React Na ...