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 ...
随机推荐
- Knockout开发中文API系列4–绑定关键字
目的 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 示例 <div data-bind="visible: shouldShowMessage"> You ...
- JAXP操作xml
DOM对象详解1.基本的DOM对象 DOM的基本对象有5个:Document,Node,NodeList,Element和Attr.下面就这些对象的功能和实现的方法作一个大致的介绍. Document ...
- C# 不借助第三个变量实现两整数交换
c#中实现两个int变量的数值互换,在借助第三个变量做临时载体的情况下,非常简单. ; ; int c ; c = a; a = b; b = c; 如果不借助第三方变量,有几种实现的方法: 1.异或 ...
- ansible wc -l 对结果值取大小的操作
[root@localhost ansible]# cat zss.yml - hosts: zss tasks: - name: step1 ping: - name: step2 install ...
- Maven打包生成源码包和Javadoc包
https://blog.csdn.net/top_code/article/details/53586551 当我们开发了一个公共模块,将它deploy到Maven仓库时,最好同时提供源码包和Jav ...
- Overview & PPT 类型 & PPT 制作流程
PPT 策略 PPT 不能当讲稿 初次制作PPT的人常常把PPT当成大号讲稿,把演示时所有要说的话都变成文字做到PPT里,这 样的结果是观众很难记住PPT里到底讲了些什么需要我们帮助他们将文字尽可能转 ...
- 关于很怂地退回SDK,ndk,gradle版本这件事。。。(降版本fix项目异常)
前言:说明一下,这篇文章对别人应该是没什么用的,单作为自己的记录吧,方便以后查询 电脑重装后没有再使用之前的studio2.3,而是直接下载了最新版的v3.1.2,同时升级了所有SDK(28),NDK ...
- idea+maven无法自动加载jar包
没有配置maven的环境变量所致 执行mvn -version进行检测
- git中报unable to auto-detect email address 错误的解决拌办法
昨天刚配置好的git,今天刚要commit一些修改,就遇到了这个问题** Please tell me who you are. Run git config --global user.email ...
- win10 UWP Markdown 含源码
Windows下没有比較好的Markdown编辑器 我就自己写一个 csdn的Markdown非常好,就是我须要截图保存有麻烦 须要把我的截图保存在本地,然后上传 这个过程比較麻烦 csdn的图没法外 ...