CodeForces 124B Permutations
2015-06-18 16:01
330 查看
PermutationsTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeCodeForces
124B
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
Output
Input
Output
& %I64u
SubmitStatusPracticeCodeForces
124B
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/*********************************************** * Author: fisty * Created Time: 2015/6/15 21:14:23 * File Name : A.cpp *********************************************** */ #include <iostream> #include <cstring> #include <deque> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <bitset> #include <algorithm> using namespace std; #define Debug(x) cout << #x << " " << x <<endl #define Memset(x, a) memset(x, a, sizeof(x)) const int INF = 0x3f3f3f3f; typedef long long LL; typedef pair<int, int> P; #define FOR(i, a, b) for(int i = a;i < b; i++) #define MAX_N 10 int n, k; int c[MAX_N]; int z[MAX_N]; char b[MAX_N][MAX_N]; int ans; int solve(){ FOR(i, 0, n){ FOR(j, 0, k){ cin >> b[i][j]; } } for(int i = 0;i < k; i++){ c[i] = i; } ans = INF; do{ // for(int i = 0;i < k; i++){ // cout << c[i] << " "; // } // cout << endl; int _max = -INF, _min = INF; for(int i = 0;i < n; i++){ int a = 0; for(int j = 0;j < k; j++){ a += (b[i][c[j]]-'0') * z[k-j-1]; } _min = min(_min, a); _max = max(_max, a); } //Debug(_max); //Debug(_min); ans = min(ans, _max - _min); }while(next_permutation(c, c + k)); return ans; } int main() { //freopen("in.cpp", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); cin >> n >> k; z[0] = 1; for(int i = 1;i < k; i++){ z[i] = z[i-1] * 10; } cout << solve() << endl; return 0; }
相关文章推荐
- opencv学习笔记8:跟踪与运动
- 第一阶段冲刺报告(五)
- linux下日期表示法~
- 杨子江gre填空词汇分类整理
- java中的基本数据类型与封装类
- java用BufferedImage转换图片大小并保存
- Objective-C代码块语法(block)使用
- 找出100以内的质数并求和
- EMMA-代码覆盖率测试工具
- 合并排序数组 II
- js 日期格式转换
- curl网站开发指南
- git多个公钥
- java基础将一个int数组转换成一个字符串
- jFileChooser showSaveDialog 保存文件
- Android计算器开发实例
- keil 编译后 flash 和 ram 占用情况
- codeforce #129
- This End Up: Using Device Orientation
- win7系统怎么创建虚拟磁盘?