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. libc++abi.dylib: terminate_handler unexpectedly threw an exception错误小结

    说法一: 我们在运行xcode工程时,有时候会遇到”libc++abi.dylib: terminate_handler unexpectedly threw an exception”错误,app莫 ...

  2. oracle starup报错

    今天在linux里面安装Oracle的时候出现一个报错,忽略报错安装之后,数据库启动的时候报错: 从网上找到的解决方法: 创建实例后,进入sqlplus启动报错: sqlplus / as sysdb ...

  3. LeetCode_Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. USB 各型插座插头引脚分布

    最近画USB的电路,老是忘记它的引脚排列,每次都要去翻手册,很麻烦,索性整理了一下,以后用着也方便,这些图都来自USB标准上. 注:以下均为插座或插头的前视图,即将插座或插头面向自己. USB A型插 ...

  5. fragment中嵌套viewpager,vierpager中有多个fragment,不显示 .

    fragment中嵌套viewpager,vierpager中有多个fragment,不显示 ... 现在好多应用流行一种布局.底部几个工具栏选项,上面也有类似tab的选项. 底部用RadioGrou ...

  6. Shell下通过echo+telnet在远端执行命令

    创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令   执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...

  7. poj 2229 Sumsets(dp 或 数学)

    Description Farmer John commanded his cows to search . Here are the possible sets of numbers that su ...

  8. python3-day2(基本回顾)

    1.作用域 1>外层变量可以被内层变更使用 2>内层变更不可以被外层使用 global nonlocal 2.对于Python,一切事物都是对象,对象基于类创建 3.练习 有如下值集合 [ ...

  9. Oracle11g x64使用Oracle SQL Developer报错:Unable to find a Java Virtual Machine

    原因oracle 11g中安装的Oracle SQL Developer是32位的,而我们现在给他指定的java.exe却是64位的,所以会出现这种错误.解决方法1)从网上下载Oracle SQL D ...

  10. css-盒模型

    <!DOCTYPE html>CSS3-盒模型 盒模型属性: width 宽度 height 高度 margin 外边距 border 边框 padding 内边距.test{width: ...