您的位置:首页 > Web前端

POJ-2718 smallest difference (DFS 随机数 剪枝)

2014-12-15 12:44 197 查看
Smallest Difference

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4745 Accepted: 1314
Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer
is 0, the integer may not start with the digit 0. 

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

The first line of input contains the number of cases 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

For each test case, write on a single line the 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

题意:
升序输入一组数,只包含0~9,不重复。将这些数分为两拨,组成两个整数,求所有情况中两者smallest difference(最小差值)
思路:
用深搜将输入的这些数生成全排列(也可以直接使用next_permutation),不断比较求出最小值,思路很简单,但是以下几点需要注意:
1.注意对输入的处理;
2.包含出现零的情况,但是不能以零开头组成多位数。如solve(0,1)=1,solve(0,1,2)=8;
3.剪枝,当x1,x2首位出现零时剪枝,当pos>=m/2时,将x2后位补零,若abs(x1-x2)已经大于现有最小值,剪枝;#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
const int INF=100000000;
int n,m,dif;
int nums[10]={1,10,100,1000,10000,100000,1000000};
int num[20];//保存输入
int used[20];//标记
int perm[20];//保存全排列
void permutation(int pos);
int main()
{
cin>>n;getchar();
while(n--)//输入要注意
{
m=0;dif=INF;
char a;
while((a=getchar())!='\n')
{
if(a==' ')
continue;
num[m]=a-'0';
m++;
}
memset(used,0,sizeof(used));
memset(perm,1,sizeof(perm));
permutation(0);
cout<<dif<<endl;
}
return 0;
}
void permutation(int pos)
{
int x1,x2;
if(pos>m/2)//当位数大于m/2时考虑是否剪枝
{
x1=x2=0;
for(int i=0;i<m/2;i++)
{
x1*=10;
x1+=perm[i];
}
x2=perm[m/2]*nums[m-m/2-1];//后位补零
if(abs(x1-x2)>dif)//满足条件,剪枝
return;
}
if(pos==m)
{
x1=x2=0;
for(int i=0;i<m/2;i++)
{
x1*=10;
x1+=perm[i];
}
for(int j=m/2;j<m;j++)
{
x2*=10;
x2+=perm[j];
}
x1=x1>x2?x1-x2:x2-x1;
if((perm[0]==0||perm[m/2]==0)&&m>=3)//x1,x2首位出现零,剪枝
return ;
dif=min(dif,x1);
return ;
}
for(int i=0;i<m;i++)//生成全排列
{
if(used[num[i]]==0)
{
perm[pos]=num[i];
used[num[i]]=true;
permutation(pos+1);
used[num[i]]=false;
}
}
return ;
}


下面是一位学长写的代码,没有剪枝竟然水过了。。。他花了半个小时,我却前后纠结了两三个小时,并且后来发现G++能过,C++不行,这让我几乎重新开始思考人生。。但是剪枝还是快了将近一倍,你懂得。。。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#define REP(i,x,v) for(int i=x;i<=v;i++)
#define REPD(i,x,v) for(int i=x;i>=v;i--)
#define FOR(i,v) for(int i=0;i<v;i++)
#define CLR(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define cz(x) scanf("%d",&(x))
#define cz2(x,y) scanf("%d%d", &(x), &(y))
#define cz3(x,y,z) scanf("%d%d%d", &(x), &(y), &(z))
#define MAXN 100000+50
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
int TT;
vi arr;
bool vis[10];
void slove();
LL ans;
int main()
{
cz(TT);
FOR(i, TT){
CLR(vis, false);
arr.clear();
ans = 9876543210;
int tmp;
char ch;
while(cz(tmp)){
vis[tmp] = true;
ch = getchar();
arr.push_back(tmp);
if(ch == '\n'){
// slove();
break;
}
}
int cnt = arr.size();
if(cnt == 2 && vis[0]){
cout << arr[1] << endl;
continue;
}
do{
LL num1 = 0, num2 = 0;
if(arr[0] == 0 || arr[cnt/2] == 0){
continue;
}
else{
FOR(i, cnt/2){
num1 = num1*(LL)10 + arr[i];
}
for(int i = cnt/2; i < cnt; ++i){
num2 = num2*(LL)10 + arr[i];
}
}
if(num1 < num2){
LL tmp;
tmp = num2;
num2 = num1;
num1 = tmp;
}
// cout << num1 << " " << num2 << endl;
// cout <<ans <<endl;
ans = min((LL)(num1-num2), ans);
}while(next_permutation(arr.begin(), arr.end()));
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DFS 剪枝 全排列