http://codeforces.com/problemset/problem/124/B

Description

You are given nk-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.

Input

The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.

Output

Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule.

Sample Input

Input
6 4
5237
2753
7523
5723
5327
2537
Output
2700
Input
3 3
010
909
012
Output
3
Input
7 5
50808
36603
37198
44911
29994
42543
50156
Output
20522
题目大意:给你n个长度为m的数字串,你可以任意调换第i列和第j列的数字,得到新的n个数,求出最大值减最小值最小的情况,输出二者的插值。
数据不大,1 ≤ n, k ≤ 8,用全排列就ok。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; long long n,m,ma,mi,t,b[110000][10],s,flag[10],temp[10],ans;
char a[10][10];
void dfs(int k)//将k的全排列算出来,存在b数组中。
{
if (k>m)
{
for (int i=1;i<=m;i++)
b[s][i]=temp[i];
s++;
return;
}
for (int i=1;i<=m;i++)
{
if (!flag[i])
{
temp[k]=i;
flag[i]=true;
dfs(k+1);
flag[i]=false;
}
}
}
int main()
{ while (cin>>n>>m)
{
for (int i=1;i<=n;i++)
cin>>a[i];
s=0;
memset(flag,false,sizeof(flag));
dfs(1);
ans=999999999;
for (int p=0;p<s;p++)//求出每种全排列下的最大值和最小值,计算二者只差。
{
ma=-1;
mi=999999999;
for (int i=1;i<=n;i++)
{
long long temp=0;
for (int j=0;j<m;j++)
{
temp=temp*10+a[i][b[p][j+1]-1]-48;
}
if (ma<temp)
ma=temp;
if (mi>temp)
mi=temp;
}
if (ans>ma-mi)
ans=ma-mi;
}
cout <<ans<<endl;
}
return 0;
}

  

CodeForces 124B Permutations的更多相关文章

  1. 贪心 CodeForces 124B Permutations

    题目传送门 /* 贪心:全排列函数使用,更新最值 */ #include <cstdio> #include <algorithm> #include <cstring& ...

  2. CodeForces 187A Permutations

    反向思维,先求数组中不用处理的元素个数,再用n减去这个数,得到结果. #include <iostream> #include <cstring> #define maxn 2 ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces 285 E. Positions in Permutations

    \(>Codeforces \space 285 E. Positions in Permutations<\) 题目大意 : 定义一个长度为 \(n\) 的排列中第 \(i\) 个元素是 ...

  5. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  6. Codeforces Round #337 Alphabet Permutations

    E. Alphabet Permutations time limit per test:  1 second memory limit per test:  512 megabytes input: ...

  7. codeforces 341C Iahub and Permutations(组合数dp)

    C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

  9. codeforces 340E Iahub and Permutations(错排or容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy ...

随机推荐

  1. shell中的for、while、until

    for var in list do commands done 在每个迭代中,变量var会包含列表中的当前值,第一个迭代会使用列表中的第一个值,第二个迭代使用第二个值. 在do和done中,$var ...

  2. hex、bin、elf、axf文件区别

    AXF和ELF axf文件是ARM的调试文件,除了包含bin的内容之外,还附加了其他的调试信息,这些调试信息加在可执行的二进制数据的前面.在调试的时候,这些调试信息是不必下到RAM中去的,真正下到RA ...

  3. 有关 sizeof和strlen的总结

    先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...

  4. Android 获取当前日期算前一年、前一月、前一天Calendar

    使用Calendar的add(int field, int amount)方法 Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.se ...

  5. PooledDataSource--mybatis-3-mybatis-3.2.3

    org.apache.ibatis.executor.SimpleExecutor public <E> List<E> doQuery(MappedStatement ms, ...

  6. [置顶] Android EditText/TextView使用SpannableString显示复合文本

    在Android中EditText用于编辑文本,TextView用于显示文本,但是有时候我们需要对其中的文本进行样式等方面的设置.Android为我们提供了SpannableString类来对指定文本 ...

  7. hdu 5312 Sequence(数学推导+线性探查(两数相加版))

    Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...

  8. 话说GET与POST那点恩怨

    看过很多人写GET和POST之间的区别,为什么这么多人关注它们呢?因为它们是最常用的两种HTTP方法,之间有很多相同之处,也存在非常大的不同.首先了解一下HTTP方法: 什么是HTTP?     超文 ...

  9. C# 制作 仪表

    以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ...

  10. mycat实例(2)

    全局序列号 数据切分后,原有的关系数据库中的主键约束在分布式条件下将无法使用,因此需要引入外部机制保证数据唯一性标识,这种保证全局性的数据唯一标识的机制就是全局序列号(sequence). 1. 本地 ...