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. 利用Azure高级存储搭建高性能Linux服务器(2)

    我们首先来测试随机写的IOPS,可以看到随机写的IOPS可以达到,顺序写的IOPS可以达到: $ sudo fio -filename=/data/testfile -direct=1 -iodept ...

  2. WORD-如何解除WORD文档的锁定

    Word文档保护破解 般来说WORD文档有两种密码打开密码和文档保护密码下面介绍几种破解文档保护密码方法 方法1:插入文件法 启动WORD新建空白文档执行插入→文件打开插入文件对框定位需要解除保护文档 ...

  3. Honkly分享链接集总篇

    VC6.0 Filetool Honkly版         http://pan.baidu.com/s/1bnentr5 密码:15eq,解压密码:honkly VC6.0 Filetool 官方 ...

  4. Keil C51里关于堆栈指针的处理

    Keil C是非常优秀的C51编译器,可能是最好的C51编译器,提供各种优化模式,对变量的优化和地址安排做得非常好.这是用C语言写代码的好处之一,如果用汇编写,得费一大番功夫给各个变量安排内存物理地址 ...

  5. SEO的URL如何优化才是最佳

    原文地址:http://www.chinaz.com/web/2007/0413/6841.shtml 很多人都知道URL对SEO的重要之处,但是很多站点却忽略了站点的路径优化.今天本人在这里写几点关 ...

  6. pyqt 正则表达式例子学习

    def rex01(self): username=QtCore.QRegExp('[a-zA-Z0-9_]{2,10}') self.names.setValidator(QtGui.QRegExp ...

  7. Linux查看系统信息

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  8. poj 1088 滑雪问题

    滑雪问题 import java.util.Scanner; public class Main{ static int a[][],r,c; public static void main(Stri ...

  9. 【Oracle】逻辑结构(TableSpace→Segment→Extent→Block)

     一.逻辑体系结构图 二.逻辑结构图组成介绍 从上表能够看出,一个数据库是由多个表空间(tablespace)组成,一个表空间又由多个段(segment)组成,一个段又由多个区(extent)组成 ...

  10. According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    1.错误描写叙述 2014-7-13 17:27:21 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one ...