一、题目

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;
}

【搜索】POJ-2718 贪心+枚举的更多相关文章

  1. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  2. POJ 2718【permutation】

    POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...

  3. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  4. zoj 1033 与其说是搜索,不如说是枚举

    zoj 与其说是搜索,不如说是枚举,只不过是通过搜索来实现的罢了. 主要是要注意好闰年的判断,特别是要注意好一串数字的划分. 题意其实我也看了一个晚上,才渐渐的看懂. 题意: 给你一个字符串,其中包含 ...

  5. poj 1873 凸包+枚举

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1 ...

  6. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  7. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  8. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  9. poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

随机推荐

  1. 第一次作业:基于Linux 4.5的进程模型与调度器分析

    1.操作系统是怎么组织进程的? 1.1什么是线程,什么是进程: 刚接触时可能经常会将这两个东西搞混.简单一点的说,进程是一个大工程,线程则是这个大工程中每个小地方需要做的东西(在linux下看作&qu ...

  2. 关于Modelsim SE软件Fatal License Error的解决方法

    操作环境:Win7 32位系统 软件版本:Modelsim SE 10.1a Modelsim SE软件有时会弹出如图1所示“Fatal License Error”的提示信息,原因可能是软件破解不彻 ...

  3. SQL Server 2008 R2 Express Profiler

    I successfully installed SQL Server Profiler 2008 R2 Profiler without uninstalling SQL 2008 R2 Expre ...

  4. 20145209刘一阳《JAVA程序设计》第三周课堂测试

    第三周课堂测试 1.使用汇编语言编写指令时,用一些简单的容易记忆的符号来代替二进制指令,比机器语言更为方便,属于高级语言.(B) A .true B .false 2.下列说法正确的是(ABCD) A ...

  5. 苏州Uber优步司机奖励政策(4月23日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 2653: middle

    2653: middle 链接 分析: 二分答案+主席树. 对于中位数的经典做法,就是二分一个数,将小于的变成-1,大于等于的变成+1,那么如果sum>=0(因为+1包括等于),L=mid+1, ...

  7. 一个简单的获取RGB值方式

    操作系统内置了许多小工具,有时候这些小工具也挺有用的,省去了安装一些复杂的软件, 截图 通过键盘PrtSc获取到要取色的图片,然后用画图工具打开 查看 通过画图工具的取色工具,取到你需要的颜色,然后点 ...

  8. 基于Vue+Spring MVC+MyBatis+Shiro+Dubbo开发的分布式后台管理系统

    本文项目代码: 服务端:https://github.com/lining90567/dubbo-demo-server 前端:https://github.com/lining90567/dubbo ...

  9. python OptionParser模块使用

    OptionParser是python中用来处理命令行的模块,在我们使用python进行流程化开发中必要的工具 Optparse,它功能强大,而且易于使用,可以方便地生成标准的.符合Unix/Posi ...

  10. Unity优化方向——优化Unity游戏中的脚本(译)

    原文地址:https://unity3d.com/cn/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity ...