POJ 2718 Smallest Difference (dfs)

Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5005 Accepted: 1372

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

本题可以用两种姿势 

一个是用next_permutation枚举全排列(375ms AC)

然后对半切就好了 - - 因为最小当然要对半切了 要处理好两个数字可能有前导0的情况

含0的时候  对于大于2个数字的情况 有前导0的排列必然不被允许

那么就有一个特殊情况 就是在2个数字的时候 且含0 直接单独拿出来就好了


另一种姿势就是 dfs暴力枚举(800ms AC)

dfs(k,b,s) k为深度 b为半个大数 s为另一半小数 对于偶数个数字对半分

对于奇数的情况 下标比如是0 1 2  那偶数下标0 2应该在大数s这一边  奇数下标1应该在 小数这一边

在网上看到了剪枝的姿势 - - 就是补0 但是一直没能自己证明就没剪枝


AC代码如下:

//
//  POJ 2718 Smallest Difference
//
//  Created by TaoSama on 2015-02-20
//  Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, l, r, a[15];
bool used[15];
void dfs(int k, int b, int s) {
	if(k == n) {
		if(b < s) swap(b, s);
		if(b - s < l - r) l = b, r = s;
		return;
	}
	if(k & 1) {
		for(int i = 0; i < n; ++i) {
			if(!used[i]) {
				if(a[i] == 0 && k == 1) continue;
				used[i] = true;
				dfs(k + 1, b, s * 10 + a[i]);
				used[i] = false;
			}
		}
	} else {
		for(int i = 0; i < n; ++i) {
			if(!used[i]) {
				if(a[i] == 0 && k == 0) continue;
				used[i] = true;
				dfs(k + 1, b * 10 + a[i], s);
				used[i] = false;
			}
		}
	}
}

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	int t; scanf("%d ", &t);
	while(t--) {
		char c; n = 0;
		memset(used, false, sizeof used);
		while(scanf("%c", &c) == 1 && c != '\n') {
			if(c == ' ') continue;
			a[n++] = c - '0';
		}
		int ans = INF, x, y;
		if(n == 2) ans = a[1] - a[0];
		else {
			l = INF, r = 0;
			dfs(0, 0, 0);
			ans = l - r;
		}
		printf("%d\n", ans);
	}

	return 0;
}

//next_permutation枚举全排列求解
/*
int n, a[15];
int cal(int st, int ed) {
	int ret = 0;
	for(int i = st; i < ed; ++i) {
		if(a[st] == 0) break;
		ret = ret * 10 + a[i];
	}
	return ret == 0 ? INF : ret;
}
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	int t; scanf("%d ", &t);
	while(t--) {
		char c; n = 0;
		while(scanf("%c", &c) == 1 && c != '\n') {
			if(c == ' ') continue;
			a[n++] = c - '0';
		}
		int ans = INF, x, y;
		if(n == 2) ans = a[1] - a[0];
		else do {
				x = cal(0, n >> 1);
				y = cal(n >> 1, n);
				if(x != INF && y != INF) ans = min(ans, abs(x - y));
			} while(next_permutation(a, a + n));
		printf("%d\n", ans);
	}
	return 0;
}
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值