zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟

    B. Permutations
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Input

    The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next nlines 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 test(s)
    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
    Note

    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位的数字的序列,现在可以对每一个数字进行相同的操作,比如,你把第1个数字的第1,2位互换了,那么剩余的n-1个数字你也必须把第1,2位互换。

    现在对这n个数字一系列操作后,会产生许多不同的序列,输出min{序列的max-序列的min}

    注意到n,k的范围,就想暴力了。

    对每一个数字,对这个数字进行操作后,会有k!种可能。

    所以最后会有k!种序列,对每一个序列求出其max-min

    再取所有中的min就是答案了。

    步骤:

    1、dfs求出1~k生成的排列,有k!种。

    2.对每一种排列,把n个数字的每一个数字按照排列的顺序重新安排,

    就产生了新的序列,把这个序列转换为int型的,然后求出其max-min

    每一种排列求完以后,对应的ans也出来了。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 const int maxn=10;
     8 const int inf=0x3f3f3f3f;
     9 
    10 char init[maxn][maxn];
    11 char s[maxn][maxn];
    12 int num[maxn];
    13 int a[maxn];
    14 int ans;
    15 int n,k;
    16 
    17 int change(int i)
    18 {
    19     int ret=0;
    20     for(int j=0;j<k;j++)
    21     {
    22         int tmp=s[i][j]-'0';
    23         //printf("%d
    ",tmp);
    24         ret=ret*10+tmp;
    25     }
    26     //printf("%d
    ",ret);
    27     return ret;
    28 }
    29 
    30 void solve()
    31 {
    32     for(int i=0;i<n;i++)
    33     {
    34         for(int j=0;j<k;j++)
    35             s[i][j]=init[i][a[j]];
    36     }
    37 
    38     for(int i=0;i<n;i++)
    39     {
    40         num[i]=change(i);
    41     }
    42 
    43     sort(num,num+n);
    44     ans=min(ans,num[n-1]-num[0]);
    45 }
    46 
    47 void dfs(int u)
    48 {
    49     if(u==k)
    50     {
    51         solve();
    52         return ;
    53     }
    54     for(int i=0;i<k;i++)
    55     {
    56         int ok=1;
    57         for(int j=0;j<u;j++)
    58         {
    59             if(a[j]==i)
    60                 ok=0;
    61         }
    62         if(ok)
    63         {
    64             a[u]=i;
    65             dfs(u+1);
    66         }
    67     }
    68     return ;
    69 }
    70 
    71 int main()
    72 {
    73     scanf("%d%d",&n,&k);
    74     for(int i=0;i<n;i++)
    75     {
    76         scanf("%s",init[i]);
    77     }
    78     ans=inf;
    79 
    80     dfs(0);
    81 
    82     printf("%d
    ",ans);
    83 
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    C# DateTime
    C# const static readonly
    HTML div+CSS
    javaScript 正则表达式验证
    前期绑定和后期绑定
    如何应对大数据量高并发访问问题
    数据库优化之:SQL语句优化之二
    银河系漫游指南 图标
    [转] 花3分钟时间来关闭你电脑上没用的服务
    Devdays 2006 China 微软开发者日
  • 原文地址:https://www.cnblogs.com/-maybe/p/4579458.html
Copyright © 2011-2022 走看看