Smallest Difference(暴力全排列)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10387 | Accepted: 2836 |
Description
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
Output
Sample Input
1
0 1 2 4 6 7
Sample Output
28
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int ans;
int vis[];
int p[] = {,,,,,,,,,};
int get(int* a){
int i = ;
while(){
char c = getchar();
if(c == ' ')continue;
if(c == '\n')return i;
a[i ++] = c - '';
vis[c - ''] = ;
} } int cal(int *a, int n){
int x = ;
for(int i = ; i < n; i++){
// printf("%d ", a[i]);
x = x * + a[i];
}//printf("\n计算的结果是:\n", x);
return x;
} void distribute(int* a, int an, int* l, int ln, int* r, int rn, int i){ if(i == an){
// puts("l的元素是:");
// for(int j = 0; j < ln; j++){
// printf("%d ", l[j]);
// }puts("");
//
// puts("r的元素是:");
// for(int j = 0; j < rn; j++){
// printf("%d ", r[j]);
// }puts(""); if(ln == || rn == ){
return;
}
if(abs(rn - ln) > ){
return;
}
do{
do{
if(ln > && l[] == ){
continue;
}
if(rn > && r[] == ){
continue;
}
int x = cal(l, ln);
int y = cal(r, rn);
// printf("x = %d\ny = %d\n", x, y);
if(abs(x - y) <= ans){
ans = abs(x - y);
}
}while(next_permutation(r, r + rn));
}while(next_permutation(l, l + ln)); return;
} l[ln] = a[i];
distribute(a, an, l, ln + , r, rn, i + );
r[rn] = a[i];
distribute(a, an, l, ln, r, rn + , i + ); } int main(){
int T;
scanf("%d", &T);
getchar();
int a[], l[], r[];
while(T--){
memset(vis, , sizeof(vis));
int n = get(a);
ans = 0x3f3f3f3f;
// if(n == 10){
// puts("247");
// continue;
// }else if(n == 9){
// int m[10] = {2469,10469,469,369,359,358,359,369,469,1469};
// for(int i = 0; i < 10; i++){
// if(!vis[i]){
// printf("%d\n", m[i]);
// }
// }
// continue;
// }
distribute(a, n, l, , r, , );
printf("%d\n", ans);
}
return ;
}
Smallest Difference(暴力全排列)的更多相关文章
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ2718Smallest Difference(暴力全排列)
传送门 题目大意:升序输入十进制数 没有重复 分成两个非空集合 每个集合组成一个数(不能有前导零) 求两个数差的最小值. 题解:全排列...我数组从1开始怎么一直WA...还有这个输入值得学习. 代码 ...
- 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(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
- Smallest Difference(POJ 2718)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6740 Accepted: 18 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...
随机推荐
- MATLAB实现多元线性回归预测
一.简单的多元线性回归: data.txt ,230.1,37.8,69.2,22.1 ,44.5,39.3,45.1,10.4 ,17.2,45.9,69.3,9.3 ,151.5,41.3,58. ...
- 通过ambari安装hadoop集群(一)
之前一直用的开源社区的版本,最近突然想到找一个好的商业版本用着,然后一直使用,然后就定上了hdp和cdh,都装上去了,但是cdh使用难度太大了,和开源版本的差距比较大,api不会使用,需要学它的cdk ...
- 【Python】 linecache模块读取文件
[linecache] 过往在读取文件的时候,我们通常使用的是这种模式: with open('file.txt','r') as f: line = f.readline() while line: ...
- Redis 2.8.18 安装报错 error: jemalloc/jemalloc.h: No such file or directory解决方法
http://www.phperz.com/article/14/1219/42002.html ——————————————————————————————————————————————————
- jQuery中ajax和post处理json的不同
近日在做门户的用户评论时,好长时间没有用jquery了正好用一下,没想到偷工用了post方法去处理ajax回调的json数据,死活取不到,后台就是有json返回了.不料这么小小一个问题挂了我好几个小时 ...
- HBase的compact分析
HBase是基于LSM树存储模型的分布式NoSQL数据库.LSM树对比普遍的B+树来说,能够获得较高随机写性能的同时,也能保持可靠的随机读性能(可参考这里).在进行读请求的时候,LSM树要把多个子树( ...
- getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()区别详解
getDimension()是基于当前DisplayMetrics进行转换,获取指定资源id对应的尺寸.文档里并没说这里返回的就是像素,要注意这个函数的返回值是float,像素肯定是int. getD ...
- java 正则表达式 验证字符串 只包含汉字英文数字
String content = “testContent”; String regex="^[a-zA-Z0-9\u4E00-\u9FA5]+$"; Pattern patter ...
- SpringMVC系列(六)处理模型数据
Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据 Map 及 Model: ...
- 如何找回Ucenter创始人密码,账号无需修改
UCenter 创始人的密码非常重要,忘记或丢失后,就不能进入 UCenter 进行用户和数据的管理,也会对站点造成安全隐患.由于 UCenter 的密码是采用两次 md5 加一个随机数的形式加密的, ...