HBC209989BogoSort题解

原来我爱你 算法基础篇 77 0
不断提升技能,才能在职场中立于不败之地!全网最全C++题库,助您成为编程领域的佼佼者。
Today Tonnnny the monkey learned a new algorithm called Bogo Sort. The teacher gave Tonnnny the code of Bogo sort: bool is_sorted {. So Tonnnny improved Bogo Sort. He had chosen one favourite permutationpp pwith length NN N, and he replaced the random shuffle with shuffle of pp p, so the improved algorithm, Tonnnny Sort, can solve sorting problems for length NN Narray — at least Tonnnny thinks so. int p[N] = {....}; // Tonnnny's favorite permutation of n. } Tonnnny was satsified with the new algorithm, and decided to let you give him a different array of length NN Nevery 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,pN, pN,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 mostNN Ndigits, so please output answer mod10N10^N10N instead.

Today Tonnnny the monkey learned a new algorithm called Bogo Sort. The teacher gave Tonnnny the code of Bogo sort: 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 a a  with length n n n , and the algorithm's expectation complexity is O(n⋅n!)O(n cdot n!)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 p p with length N N N , and he replaced the random shuffle with shuffle of p p p , so the improved algorithm, Tonnnny Sort, can solve sorting problems for length N N N array — at least Tonnnny thinks so. 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 N 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, pN, pN, 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 N N digits, so please output answer mod 10N10^N10N instead.

HBC209989BogoSort题解
-第1张图片-东莞河马信息技术
(图片来源网络,侵删)
全网最全C++题库,助您挑战自我,突破极限,成为编程领域的佼佼者!

标签: HBC209989BogoSort题解