Codeforces Beta Round #92 (Div. 2 Only) B. Permutations
You are given n k-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.
The first line contains integers n and k —
the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines
containk-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging
of digits.
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.
6 4
5237
2753
7523
5723
5327
2537
2700
3 3
010
909
012
3
7 5
50808
36603
37198
44911
29994
42543
50156
20522
In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits).
In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
题意:给你n个字符串,每个字符串的长度都为k,然后对每个字符串中的字母顺序进行调换,当一个字符串调换顺序时,其他字符串都以相同的规律调换,问你调换过程中最大的数和最小的数的差的最小值是多少。
思路:只要把所有情况搜出来就行了,刚开始还想找规律。。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
char s[100][100],str[100];
int vis[100],k,cha,b[100],n;
#define inf 99999999
void dfs(int wei)
{
int i,j,minx=inf,maxx=-inf,num;
if(wei==k+1){
num=0;
for(i=1;i<=n;i++){
num=0;
for(j=1;j<=k;j++){
num=num*10+s[i][b[j]]-'0';
}
maxx=max(maxx,num);
minx=min(minx,num);
}
cha=min(cha,maxx-minx);//return;
}
else{
for(i=1;i<=k;i++){
if(!vis[i]){
vis[i]=1;
b[wei]=i;
dfs(wei+1);
vis[i]=0;
}
}
}
}
int main()
{
int m,i,j,t,num;
char c;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
cha=inf;
if(n==1){
printf("0\n");continue;
}
memset(vis,0,sizeof(vis));
dfs(1);
printf("%d\n",cha);
}
return 0;
}
Codeforces Beta Round #92 (Div. 2 Only) B. Permutations的更多相关文章
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- python学习笔记 | 顺序表的常规操作
''' @author: 人人都爱小雀斑 @time: 2020/3/11 8:46 @desc: 顺序表的相关操作 ''' class SequenceList: def __init__(self ...
- 【分布式锁的演化】终章!手撸ZK分布式锁!
前言 这应该是分布式锁演化的最后一个章节了,相信很多小伙伴们看完这个章节之后在应对高并发的情况下,如何保证线程安全心里肯定也会有谱了.在实际的项目中也可以参考一下老猫的github上的例子,当然代码没 ...
- Azure Key Valut 简介
Azure Key Vault(密钥库)是用于安全地存储和访问Secret的云服务,Secret是需要严格控制访问权限的内容,例如API密钥,密码,证书或加密密钥.Key Vault Service支 ...
- 怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中
怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中
- SAP系统跨平台字符编码转换
SAP系统在进行了夸平台的迁移,可能会遇到操作系统层文件编码不同,导致SAP系统无法识别或者乱码的问题.例如SAP系统从AIX平台迁移到linux平台,SAP应用服务器的编码会发生变化,从4102变化 ...
- PAT练习num2-挖掘机技术哪家强
为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 1 的正整数 N,即参赛人数.随后 N 行, ...
- C# 正则表达式 -- 复习
符号解释: \ 特殊的字符,转义 ^ 匹配输入的字符串的开始位置 $ 匹配输入的字符串的结束位置 * 匹配0次或多次,等价于{0,} + 匹配1次或多次,等价于{1,} ? 匹配0次或1次,等价于{0 ...
- java虚拟机——轻松搞懂jvm
一.JVM体系结构概述 JVM位置 JVM体系结构 1.1 类加载器 ClassLoader 类加载器(ClassLoader)负责加载class文件,class文件在文件开头有特定的文件标示,并 ...
- 网易新闻App架构重构实践:DDD正走向流行
网易新闻App架构重构实践:DDD正走向流行 https://mp.weixin.qq.com/s/FdwrT_xn3CQqpWoRVBttvQ 小智 InfoQ 2020-05-14 作者 | 小智 ...
- 写给小白的 Nginx 文章
原文地址:Nginx concepts I wish I knew years ago 原文作者:Aemie Jariwala(已授权) 译者 & 校正:HelloGitHub-小鱼干 &am ...