BogoSort(2023牛客五一集训派对day5,E题)

Today Tonnnny the monkey learned a new algorithm called Bogo Sort. The teacher gave Tonnnny the code of Bogo sort:

1

2

3

4

5

6

7

8

9

10

11

12

13

bool is_sorted(int a[], int n) {

    for (int i = 1; i < n; i++) {

        if (a[i] < a[i - 1]) {

            return false;

        }

    }

    return true;

}

void bogo_sort(int a[], int n) {

    while (!is_sorted(a, n)) {

        shuffle(a, a + n);

    }

}

The teacher said the shuffle function is to uniformly randomly permute the array  a  with length  n , and the algorithm's expectation complexity is O(n⋅n!).

However, Tonnnny is a determined boy — he doesn't like randomness at all! So Tonnnny improved Bogo Sort. He had chosen one favourite permutation  p with length  N , and he replaced the random shuffle with shuffle of  p , so the improved algorithm, Tonnnny Sort, can solve sorting problems for length N array — at least Tonnnny thinks so.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

int p[N] = {....}; // Tonnnny's favorite permutation of n

void shuffle(int a[], int n) {

    int b[n];

    for (int i = 0; i < n; i++) {

        b[i] = a[i]

    }

    for (int i = 0; i < n; i++) {

        a[i] = b[p[i]];

    }

}

void tonnnny_sort(int a[], int n) {

    assert (n == N); // Tonnnny appointed!

    while (!is_sorted(a, n)) {

        shuffle(a, a + n);

    }

}

Tonnnny was satsified with the new algorithm, and decided to let you give him a different array of length N every day to sort it with Tonnnny Sort.

You are the best friend of Tonnnny. Even though you had found the algorithm is somehow wrong, you want to make Tonnnny happy as long as possible. You're given N, p, and you need to calculate the maximum number of days that Tonnnny will be happy, since after that you can't give Tonnnny an array that can be sorted with Tonnnny Sort and didn't appeared before.

The answer may be very large. Tonnnny only like numbers with at most N digits, so please output answer mod10N instead.

AC代码:

import java.util.*;
import java.io.*;
import java.math.BigInteger;
 
public class Main {
    public static void main(String[] args) {
        InputReader ip = new InputReader(System.in);
        PrintWriter op = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = ip.nextInt();
        int[] p = new int[n + 1];
        int[] pf = new int[n + 1];
        for (int i = 1; i <= n; ++i) {
            p[i] = ip.nextInt();
            pf[p[i]] = i;
        }
        List<Integer> nums = new ArrayList<>();
        boolean[] flags = new boolean[n + 1];
        for (int i = 1; i <= n; ++i) {
            if (flags[i]) continue;
            int cnt = 1;
            int cur = p[i];
            while (true) {
                flags[cur] = true;
                if (pf[cur] == p[i])
                    break;
                cur = pf[cur];
                ++cnt;
            }
            nums.add(cnt);
        }
//         op.println(nums);
        BigInteger ans = new BigInteger(nums.get(0).toString());
        BigInteger mod = BigInteger.TEN.pow(n);
        for (int i = 1; i < nums.size(); ++i) {
            int num = nums.get(i);
            BigInteger bigNum = new BigInteger(String.valueOf(num));
            BigInteger gcd = ans.gcd(bigNum);
            ans = ans.multiply(bigNum).divide(gcd);
        }
        op.println(ans.mod(mod));
        op.flush();
    }
}
 
class InputReader {
    private final BufferedReader buf;
    private StringTokenizer tkl;
 
    public InputReader(InputStream is) {
        buf = new BufferedReader(new InputStreamReader(is));
    }
 
    public boolean hasNext() {
        try {
            while (tkl == null || !tkl.hasMoreElements())
                tkl = new StringTokenizer(buf.readLine());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
 
    public String next() {
        return hasNext() ? tkl.nextToken() : null;
    }
 
    public int nextInt() {
        return Integer.parseInt(next());
    }
 
    public double nextDouble() {
        return Double.parseDouble(next());
    }
}

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值