博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2718 Smallest Difference【贪心】
阅读量:5884 次
发布时间:2019-06-19

本文共 3559 字,大约阅读时间需要 11 分钟。

Smallest Difference
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10453   Accepted: 2855

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

10 1 2 4 6 7

Sample Output

28

当n为奇数的时候,很明显,让最常的从高位到最低位递增,最短的从高位到低位递减。

当n为偶数的时候,要使得两个数的最高位的差最小,同时后面的每一位的差值最大,我们可以找到差值最小的两个数,并且使得这两个数尽量在中间,这样才会使得后面的差值最大。

我看到好多全排列的题,找了两个对拍发现了对方代码都有bug,这道题的测试数据有点水。

#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#define LOACL#define space " "using namespace std;//typedef long long LL;//typedef __int64 Int;typedef pair
PAI;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double PI = acos(-1.0);const int MOD = 1e9 + 7;const int MAXN = 500 + 10;int num[MAXN];char c;struct node { int a, b, c;} data[MAXN];int cnt;bool cmp(node x, node y) { if (x.c == y.c) return (abs(x.a - cnt/2) < abs(y.a - cnt/2)); return x.c < y.c;}int main() { int T; scanf("%d", &T); getchar(); while (T--) { cnt = 0; while (c = getchar()) { if (c == '\n') break; if (c >= '0' && c <= '9') num[cnt++] = c - '0'; } sort(num, num + cnt); int a = 0, b = 0; int ans = INF; if (cnt == 2) { ans = num[1] - num[0]; } else if (cnt & 1) { for (int i = 0; i < (cnt + 1)/2; i++) { if (num[i] == 0) { a = a*10 + num[++i]; a = a*10; } else a = a*10 + num[i]; } // cout << a << endl; for (int i = cnt - 1; i >= (cnt + 1)/2; i--) { b = b*10 + num[i]; } ans = a - b; // cout << a << space << b << endl; } else { int cut = 0; for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt; j++) { if (num[i] == 0) continue; data[cut].a = j; data[cut].b = i; data[cut++].c = num[j] - num[i]; } } sort(data, data + cut, cmp); //for (int i = 0; i < cut; i++) cout << data[i].a << space << data[i].b << space << data[i].c << endl; int k = abs(data[0].a - cnt/2) == abs(data[1].a - cnt/2); k++; //cout << "k= " << k << endl; while (k--) { int ca = cnt/2 - 1; int cb = cnt/2 - 1; int ta = data[k].a, tb = data[k].b; a = num[ta]; b = num[tb]; for (int i = 0; i < cnt; i++) { if (ca == 0) {ca = i; break;} if (i != ta && i != tb) { ca--; a = a*10 + num[i]; } } for (int i = cnt - 1; i >= ca; i--) { if (i != ta && i != tb) { b = b*10 + num[i]; } } ans = min(ans, a - b); } } printf("%d\n", ans); } return 0;}

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770742.html

你可能感兴趣的文章
Zabbix汉化方法
查看>>
Java I/O系统基础知识
查看>>
Java多线程设计模式(2)生产者与消费者模式
查看>>
对象并不一定都是在堆上分配内存的
查看>>
刘宇凡:罗永浩的锤子情怀只能拿去喂狗
查看>>
php晚了8小时 PHP5中的时间相差8小时的解决办法
查看>>
JS(JavaScript)的初了解7(更新中···)
查看>>
svn文件管理器的使用
查看>>
Ansible playbook 使用
查看>>
for/foreach/linq执行效率测试
查看>>
js /jquery停止事件冒泡和阻止浏览器默认事件
查看>>
长春理工大学第十四届程序设计竞赛(重现赛)I.Fate Grand Order
查看>>
好作品地址
查看>>
[翻译]Protocol Buffer 基础: C++
查看>>
runloop与线程的关系
查看>>
[Bzoj2246]迷宫探险(概率+DP)
查看>>
详解消息队列的设计与使用
查看>>
使用Sqoop从mysql向hdfs或者hive导入数据时出现的一些错误
查看>>
控制子窗口的高度
查看>>
处理 Oracle SQL in 超过1000 的解决方案
查看>>