【搜索】POJ-2718 贪心+枚举
2017-08-03 18:16
323 查看
一、题目
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.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
二、思路&心得
贪心:根据题目特点,选择不同情况下的最优解枚举:枚举多种局部最优解,然后求出符合题意的最值
三、代码
#include<cstdio> #include<algorithm> #define MAX 99999 using namespace std; int nums[11]; char ch; int solve() { int len = 0; while (1) { scanf("%d%c", &nums[len ++], &ch); if (ch == '\n') break; } if (len == 2) { return abs(nums[0] - nums[1]); } int x = 0, y = 0; int mid; if (len % 2 != 0) { mid = len / 2; if (!nums[0]) swap(nums[0], nums[1]); for (int i = 0; i < mid + 1; i ++) { x = x * 10 + nums[i]; } for (int i = len - 1; i > mid; i --) { y = y * 10 + nums[i]; } return x - y; } else { int index, cnt = 0, ans = MAX; int min_XY = 11; for (int i = 1; i < len; i ++) { if (min_XY >= nums[i] - nums[i - 1] && nums[i] && nums[i - 1]) { min_XY = nums[i] - nums[i - 1]; x = nums[i], y = nums[i - 1]; for (cnt = 0, index = 0; index < len, cnt < (len - 2) / 2; index ++) { if (index != i && index != i - 1) { x = x * 10 + nums[index]; cnt ++; } } mid = index; for (cnt = 0, index = len - 1; index >= mid, cnt < (len - 2) / 2; index --) { if (index != i && index != i - 1) { y = y * 10 + nums[index]; cnt ++; } } if (ans > (x - y)) ans = x - y; } } return ans; } } int main() { int t; scanf("%d", &t); while (t --) { printf("%d\n", solve()); } return 0; }
相关文章推荐
- 枚举搜索+贪心:POJ2718--Smallest Difference(解一)
- poj 2718 dfs搜索或者枚举全排列
- poj 3674 枚举+贪心与搜索
- POJ2718(枚举 + 贪心)
- poj Gone Fishing 枚举加贪心 当初做的很纠结啊!!终于A了,与大家分享一下经验
- poj 1042 Gone Fishing 贪心+枚举
- poj 1222 【枚举】【遍历搜索】
- Y2K Accounting Bug POJ - 2586 (贪心+枚举)
- 【POJ】2718 - Smallest Difference(枚举全排列)
- Smallest Difference(poj 2718)二进制枚举+全排列
- poj 1042 Gone Fishing 枚举+贪心
- POJ 2718——穷竭搜索,全排列生成
- Y2K Accounting Bug POJ - 2586 (贪心+枚举)
- POJ 1042 Gone Fishing(贪心+枚举)
- POJ-1042-Gone Fishing-枚举+贪心
- POJ 2965-The Pilots Brothers' refrigerator(贪心+枚举)
- 《挑战程序设计竞赛》2.1 穷竭搜索 POJ2718 POJ3187 POJ3050 AOJ0525
- Y2K Accounting Bug POJ - 2586 (贪心+枚举)
- POJ 1505:二分枚举+贪心
- POJ 1696 Space Ant --枚举,模拟,贪心,几何