一、题目

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. Firebird3基本使用

    解决C#无法访问的情况:1. 使用FirebirdSql.Data.FirebirdClient 5版本以上.2.修改Firebird.conf配置文件WireCrypt为Enabled#WireCr ...

  2. 2017-2018-1 20155318 《信息安全系统设计基础》第九周课下实践——实现mypwd

    2017-2018-1 20155318 <信息安全系统设计基础>第九周课下实践--实现mypwd 相关知识 man -k 查找含有关键字的内容 与管道命令结合使用:man -k k1 | ...

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

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

  4. vue复习(一)

    一.认识Vue 定义:一个构建数据驱动的Web界面的渐进式框架 优点: 1.可以完全通过客户端浏览器渲染页面,服务器端只提供数据 2.方面构建单页面应用程序(SPA) 二.引入Vue <!DOC ...

  5. GDC快讯,腾讯CMatrix布局云游戏B端领域

    2019年3月20日-22日, GDC游戏开发者大会(Game Developers Conference,以下简称GDC)于旧金山召开.每年的GDC大会上,来自世界各地,数以万计的游戏开发者们都会分 ...

  6. C#英文数字混合验证

    日常可见的验证码,当然不会这么简单,不过算是基本验证码中比较经典的,可以做一点参考,欢迎有更好方法的大哥们指正 using System; using System.Collections.Gener ...

  7. STM8S——Flash program memory and data EEPROM

    1.简介 STM8S内部的FLASH程序存储器和数据EEPROM是由一组通用寄存器来控制的:所以我们可以通过这些通用寄存器来编程或擦除存储器的内容.设置写保护.或者配置特定的低功耗模式.我们也可以自己 ...

  8. Spring学习----- Spring配置文件xml文档的schema约束

    1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  9. 电商打折套路分析 —— Python数据分析练习

    电商打折套路分析 ——2016天猫双十一美妆数据分析 数据简介 此次分析的数据来自于城市数据团对2016年双11天猫数据的采集和整理,原始数据为.xlsx格式 包括update_time/id/tit ...

  10. Altium中坐标的导出及利用坐标快速布局

    器件的坐标其实在我们处理布局的时候,非常有用,例如A板布局导入B板. 1.在A板PCB中执行菜单命令“File-Assembly-Generates Pick and Place File”对器件的坐 ...