2019杭电多校5 1005. permutation 1 (dfs,暴力)

8 篇文章 0 订阅

养成了看群决定有没比赛的习惯
因为今天群里没动静以为今天没比赛 导致这场杭电没去做 对不住队友Q^Q




permutation 1

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0

Problem Description

A sequence of length n is called a permutation if and only if it’s composed of the first n positive integers and each number appears exactly once.

Here we define the “difference sequence” of a permutation p1,p2,…,pn as p2−p1,p3−p2,…,pn−pn−1. In other words, the length of the difference sequence is n−1 and the i-th term is pi+1−pi

Now, you are given two integers N,K. Please find the permutation with length N such that the difference sequence of which is the K-th lexicographically smallest among all difference sequences of all permutations of length N.

Input
The first line contains one integer T indicating that there are T tests.

Each test consists of two integers N,K in a single line.

  • 1≤T≤40

  • 2≤N≤20

  • 1≤K≤min(104,N!)

Output
For each test, please output N integers in a single line. Those N integers represent a permutation of 1 to N, and its difference sequence is the K-th lexicographically smallest.

Sample Input

7
3 1
3 2
3 3
3 4
3 5
3 6
20 10000

Sample Output

3 1 2
3 2 1
2 1 3
2 3 1
1 2 3
1 3 2
20 1 2 3 4 5 6 7 8 9 10 11 13 19 18 14 16 15 17 12

Solution

 因为k最多只到1e4,将‘difference sequence’ 按照字典序暴力搜索就能过了
开始想着是n的阶乘,看了下范围,发现还对1e4取了个min。。

搜索时只需考虑数值的相对情况,无需考虑数值本身是多少
由于按照字典序的顺序,确认下一个数值时优先从小的开始,总能确保是按字典序
由于按照差值的字典序,搜索时要尽可能确保前面的相对差值不变的情况下,改变后面的数值,到迫不得已时才回溯改变前面的

核心是要搜索只考虑数值的差值,而不需考虑数字本身是多少

例如输入: 1 5 120
搜索历程:
Array res: 5 1 2 3 4 idest 5 1 2 3 4 difference -4 1 1 1
Array res: 5 1 2 4 3 idest 5 1 2 4 3 difference -4 1 2 -1
Array res: 5 1 3 2 4 idest 5 1 3 2 4 difference -4 2 -1 2
Array res: 5 1 3 4 2 idest 5 1 3 4 2 difference -4 2 1 -2
Array res: 5 1 4 2 3 idest 5 1 4 2 3 difference -4 3 -2 1
Array res: 5 1 4 3 2 idest 5 1 4 3 2 difference -4 3 -1 -1
Array res: 5 2 1 3 4 idest 5 2 1 3 4 difference -3 -1 2 1
Array res: 5 2 1 4 3 idest 5 2 1 4 3 difference -3 -1 3 -1
Array res: 5 2 3 1 4 idest 5 2 3 1 4 difference -3 1 -2 3
Array res: 5 2 3 4 1 idest 5 2 3 4 1 difference -3 1 1 -3
Array res: 5 2 3 4 6 idest 4 1 2 3 5 difference -3 1 1 2
Array res: 5 2 3 6 4 idest 4 1 2 5 3 difference -3 1 3 -2
Array res: 5 2 4 1 3 idest 5 2 4 1 3 difference -3 2 -3 2
Array res: 5 2 4 3 1 idest 5 2 4 3 1 difference -3 2 -1 -2

搜索里程中只需要考虑的是数值的差,因此若res数组不是 1 到 n的数, 只需要将所有数都“向左平移” 即可得到答案(平移不改变差)
例如 5 2 3 4 6, 平移得到 4 1 2 3 5(全部减一)

具体见代码


在这里插入图片描述

AC Code

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-08-05 T 19:24:53.514 +08:00
 */

import static ACMProblems.ACMIO.nextInt;
import static ACMProblems.ACMIO.out;

public class HDOJMain {
    private static int n, k, cnt;
    private static final int maxn = 25;
    static boolean used[] = new boolean[maxn];
    private static int res[] = new int[maxn];

    private static void printAnswer(int bias) {
        out.print(res[0] - bias + 1);
        for (int i = 1; i < n; ++i)
            out.print(" " + (res[i] - bias + 1));
        out.println();
    }

    /**
     * Search in lexicographic order of the number difference,
     * While dfs-ing, try to keep the difference of the number that has been determined before unchanged,
     * and modify the numbers that have not been determined yet.
     * Since what we want is lexicographical order of the ‘difference sequence’,
     * we only need to consider the relativeity of the numerical difference, rather than the value itself.
     *
     * @param currPos current position
     * @param currMin the minimum number of current paths
     * @param currMax The maximum number of current paths
     * @return Returns whether the answer has been found
     */
    private static boolean dfs(int currPos, int currMin, int currMax) {
        if (currPos == n) {
            ++cnt;
            if (cnt == k) {
                printAnswer(currMin);
                return true;
            }
            return false;
        }
        for (int currNum = currMax - n + 1; currNum < currMin + n; ++currNum) {
            if (!used[currNum]) {
                used[currNum] = true;
                res[currPos] = currNum;
                if (dfs(currPos + 1, Math.min(currMin, currNum), Math.max(currMax, currNum))) {
                    used[currNum] = false;
                    return true;
                }
                used[currNum] = false;
            }
        }
        return false;
    }

    private static void solve() {
        cnt = 0;
        res[0] = n;
        used[n] = true;
        dfs(1, n, n);
        used[n] = false;
    }

    public static void main(String[] args) throws Exception {
        int t;
        for (t = nextInt(); t-- != 0; ) {
            n = nextInt();
            k = nextInt();
            solve();
        }
        out.flush();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: np.random.permutation 是 numpy 中的一个函数,它可以将一个数组中的元素随机打乱,返回一个打乱后的新数组。 使用方法如下: ```python import numpy as np # 对一个列表进行打乱 arr = [1, 2, 3, 4, 5] np.random.permutation(arr) # 对一个 numpy 数组进行打乱 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr) ``` 你也可以指定打乱的范围: ```python import numpy as np # 只打乱数组中的前 3 个元素 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr)[:3] ``` ### 回答2: np.random.permutation是NumPy库中的一个函数,用于对数组进行随机排列。它可以将数组中的元素打乱顺序,生成一个新的打乱后的数组。这个函数的使用非常灵活,可以接受多种不同类型的输入。 np.random.permutation函数可以接受一个整数作为参数,表示生成打乱顺序的数组的长度。比如,如果传入参数10,则会生成一个包含0到9的整数,且每个数字都会随机出现一次的数组。 此外,np.random.permutation函数还可以接受一个数组作为参数,表示对该数组进行随机排列。这个函数会返回一个新的打乱顺序后的数组,不会改变原始数组。这种用法可以用于在一些机器学习任务中,将数据集随机打乱,以便更好地进行训练。 需要注意的是,np.random.permutation函数对于数组的维度有特定的处理方式。如果传入的是一个n维数组,它会以第一个维度为准,对数组进行打乱顺序,而不会改变其他维度的相对位置。 总的来说,np.random.permutation函数是NumPy库中用于对数组进行随机排列的函数,可以生成一个新的打乱顺序的数组,或者对一个给定的数组进行随机排列。它的使用非常灵活,可以满足不同类型的需求。 ### 回答3: `np.random.permutation`是NumPy库中的一个函数,用于对给定的数组或整数序列进行随机排列。 当`np.random.permutation`的参数是一个整数n时,它会返回一个长度为n的随机排列的整数序列。例如,若`np.random.permutation(5)`返回[3, 0, 4, 1, 2]。 当`np.random.permutation`的参数是一个数组a时,它会返回一个随机排列的a的副本。该函数不会改变原始数组的顺序。例如,若`a = np.array([1, 2, 3, 4, 5])`,则`np.random.permutation(a)`可以返回[4, 1, 2, 5, 3]。 可以使用`np.random.permutation`函数来打乱数组的顺序,以便进行随机化处理或者洗牌操作。这对于在机器学习和数据分析中进行训练集与测试集划分、数据扩增等操作非常有用。 需要注意的是,`np.random.permutation`函数是基于随机数生成器的,通过设置`np.random.seed`函数可以获得重复的随机排列结果。此外,在高维数组中,`np.random.permutation`默认只会对第一个维度进行随机排序,若想对其他维度进行随机排序,可以使用`np.take`函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值