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 ...
随机推荐
- android开发(31) 动画演示 - 从页面底部向上弹出dialog,消失时逐渐向下
我想实现一个效果,从底部向上逐渐弹出.如下图所示: 1.点击 显示 按钮时,一个dialog对话框从底部慢慢向上弹出. 2.关闭dialog时, dialog缓慢的移动向底部消失.很平滑的效果. ...
- 【javascript】js 检验密码强度
最近一直在做通行证项目,里面的注册模块中输入密码需要显示密码强度(低中高).今天就把做的效果给大家分享下,代码没有网上搜索的那么复杂,能够满足一般的需求. html 代码如下: <!DOCTYP ...
- Entity Framework应用:根据实体的EntityState状态实现增删改查
在上一篇文章中,我们讲解了使用EF实现简单的增删改成,在这篇文章中我们使用实体的EntityState状态来优化数据的增删改查. 一.修改数据 上篇文章中的修改数据的方法是EF官方推荐的方式,即先查询 ...
- 在kali linux之下 下载并解压的文件名呈现乱码 解决方案
从Linux往 windows拷贝文件或者从windows往Linux拷贝文件,有时会出现中文文件名乱码的情况,出现这种问题的原因是因为,windows的文件名中文编码默认为GBK,而Linux中默认 ...
- Ubuntu之网易云音乐无法启动
官方最新版(1.1)有这个问题,似乎改成0.9版就可以了 deepin15(32位):http://s1.music.126.net/download/pc/netease-cloud-music_0 ...
- 【5】JVM-垃圾收集器
通过学习了解到现在商用的JVM中的垃圾收集采用的是分代收集算法,即针对不同年代采用不同的收集算法.在JVM中,GC主要作用于堆内存中,堆内存又被划分为新生代和老年代,由于新生代对象绝大多数是朝生夕死, ...
- 【转】【WPF】WPF MVVM 简单实例
1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 Model实现 在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细 ...
- 第三百三十一节,web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令
第三百三十一节,web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令 Scrapy框架安装 1.首先,终端执行命令升级pip: python -m pip install --u ...
- SpringMVC系列(四)使用 POJO 对象绑定请求参数值
在实际开发中如果参数太多就不能使用@RequestParam去一个一个的映射了,需要定义一个实体参数对象(POJO)来映射请求参数.Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 ...
- 详解SQLServer如何链接远程MySQL数据库
最近遇到“SQL如何链接远程MySQL”这个问题,现在问题终于解决,特把方法贴出来:(我所用的操作系统是Win7,数据库是SQL2005.) 1.在SQL SERVER服务器上安装MYSQL ODBC ...