【搜索】POJ-2718 贪心+枚举
一、题目
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 贪心+枚举的更多相关文章
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- POJ 2718【permutation】
POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- zoj 1033 与其说是搜索,不如说是枚举
zoj 与其说是搜索,不如说是枚举,只不过是通过搜索来实现的罢了. 主要是要注意好闰年的判断,特别是要注意好一串数字的划分. 题意其实我也看了一个晚上,才渐渐的看懂. 题意: 给你一个字符串,其中包含 ...
- poj 1873 凸包+枚举
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6198 Accepted: 1 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
Description Bessie noted that although humans have many universities they can attend, cows have none ...
随机推荐
- 人生苦短,我用python
星空不问赶路人,时光不负有心人,你可以脱变. 1.计算机的初步认识 2.解释器的安装 python2.7(2020年官方不在维护) python3.6 (官方推荐) 1.下载安装包 https://w ...
- go语言的结构体指针
Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比 ...
- 链接SQLServer数据库 导出csv文件
依赖::::<dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>ms ...
- python字典键值对转化为相应的变量名和变量值
将python字典键值对转化为相应的变量名和变量值可以使用以下方法: globals().update({"name":"value"}) locals().u ...
- jQuery学习-事件绑定
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- P3704 [SDOI2017]数字表格
P3704 [SDOI2017]数字表格 链接 分析: $\ \ \ \prod\limits_{i = 1}^{n} \prod\limits_{j = 1}^{m} f[gcd(i, j)]$ $ ...
- AGC 007 D - Shik and Game
D - Shik and Game 链接 题意: 数轴上有一个人,从0出发到E,速度为1.数轴上还有n只熊,每只熊会在经过后的T时刻后产生一个金币.给定E,T以及n个熊的坐标pi,求收集完所有金币并到 ...
- 解决web翻转动画闪屏
首先确保backface-visibility: hidden.这样做可以解决大部分闪屏的情况. 然后需要特别注意的是谷歌的浏览器,不管是桌面端还是移动端,在翻转的过程中在该元素上绘制其他元素也会导致 ...
- Assert.notNull(sessionUser);
rg.springframework.util.Assert Assert翻译为中文为"断言".就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常.
- Linux入门基础(四):Linux网络基本配置
网络基础 ip编址 ip编址是一个双层编址方案(网络部分和主机部分),一个ip地址标识一个主机(或一个网卡接口) 现在应用最广泛的是IPv4编址,已经开始逐渐向IPv6编址切换 IPv4地址32位长, ...