Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible. Output Format The output consists of one line containing an integer indicating the number of quantum blasts per second that Butz must fire at Swapper to prevent him from sorting the books in the worst-case scenario (best case for Swapper). If this number is not a whole number, just to prevent Swapper from ever succeeding, round up to the nearest whole number. If it is impossible for Butz to stop Swapper from restoring order to the shelf, print "Butz loses!" without quotes. Sample Input 0 4 3 4 1 3 2 Sample Output 0 2 Sample Input 1 5 3 2 4 0 3 1 Sample Output 1 1 Sample Input 2 6 5 5 0 1 2 4 3 Sample Output 2 1 The actual code def solve(books,n,s):     # TODO: Compute and print the answer here     pass def main():     n, s = list(map(int,input().strip().split(" ")))     books = list(map(int,input().strip().split(" ")))     solve(books,n,s) if __name__ == "__main__":     main()

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter17: Markov Chains
Section: Chapter Questions
Problem 12RP
icon
Related questions
Question

Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible.

Output Format

The output consists of one line containing an integer indicating the number of quantum blasts per second that Butz must fire at Swapper to prevent him from sorting the books in the worst-case scenario (best case for Swapper). If this number is not a whole number, just to prevent Swapper from ever succeeding, round up to the nearest whole number. If it is impossible for Butz to stop Swapper from restoring order to the shelf, print "Butz loses!" without quotes.

Sample Input 0

4 3
4 1 3 2

Sample Output 0

2

Sample Input 1

5 3
2 4 0 3 1

Sample Output 1

1

Sample Input 2

6 5
5 0 1 2 4 3

Sample Output 2

1

The actual code

def solve(books,n,s):
    # TODO: Compute and print the answer here
    pass

def main():
    n, s = list(map(int,input().strip().split(" ")))
    books = list(map(int,input().strip().split(" ")))
    solve(books,n,s)

if __name__ == "__main__":
    main()

Swapper is a notorious, well, swapper. Whenever he sees objects in a line, he like to swap
them. He can, however, only swap two objects that are adjacent. He does this randomly,
but every now and then, through astronomical odds, after performing enough swaps, the
objects find themselves in a particularly desirable order.
Take for example playing cards. If Swapper sees four playing cards with values {4, 1, 3, 2}
, he can do the following:
1. Swap 1 and 3, resulting in {4, 3, 1, 2};
2. Swap 1 and 2, resulting in {4, 3, 2, 1}
Through astronomical odds, Swapper has accidentally arranged the playing cards in
descending order.
Butz is an agent of chaos, who happens to be a monkey as well, and he hates it when
objects are organized. He has a particularly intense disdain for objects arranged in
ascending or descending order. He believes sorted lists to be an abomination to the
natural order of the universe, which is chaos. All things gravitate towards a natural state of
entropy.
Butz likes going around various universes, simply to preserve the natural order of things,
which is a lack thereof, and he goes by many names: Nyarlathotep, Randall Flagg, or
Richard Faraday. The list goes on. Unfortunately, this time around, he landed in a small
Spanish village, where Swapper resides.
Currently, Swapper has stumbled upon a library with shelves and shelves of books. Most
of them are sorted, but a new assortment of N books has just come in and is not
arranged at all. Swapper's compulsion kicks in he would like to begin his crazy trend of
randomly swapping pairs of books. Butz is none too pleased at this new circumstance and
would like to account for the worst case scenario, which is that, by astronomical odds,
Swapper succeeds in sorting the books, either in ascending or descending order.
Butz can fire a quantum blast at Swapper to slow him down. Swapper can take S
quantum blasts before being blasted into the Todash space, the realm between realities.
However, Swapper can swap exactly one pair of books each second.
Transcribed Image Text:Swapper is a notorious, well, swapper. Whenever he sees objects in a line, he like to swap them. He can, however, only swap two objects that are adjacent. He does this randomly, but every now and then, through astronomical odds, after performing enough swaps, the objects find themselves in a particularly desirable order. Take for example playing cards. If Swapper sees four playing cards with values {4, 1, 3, 2} , he can do the following: 1. Swap 1 and 3, resulting in {4, 3, 1, 2}; 2. Swap 1 and 2, resulting in {4, 3, 2, 1} Through astronomical odds, Swapper has accidentally arranged the playing cards in descending order. Butz is an agent of chaos, who happens to be a monkey as well, and he hates it when objects are organized. He has a particularly intense disdain for objects arranged in ascending or descending order. He believes sorted lists to be an abomination to the natural order of the universe, which is chaos. All things gravitate towards a natural state of entropy. Butz likes going around various universes, simply to preserve the natural order of things, which is a lack thereof, and he goes by many names: Nyarlathotep, Randall Flagg, or Richard Faraday. The list goes on. Unfortunately, this time around, he landed in a small Spanish village, where Swapper resides. Currently, Swapper has stumbled upon a library with shelves and shelves of books. Most of them are sorted, but a new assortment of N books has just come in and is not arranged at all. Swapper's compulsion kicks in he would like to begin his crazy trend of randomly swapping pairs of books. Butz is none too pleased at this new circumstance and would like to account for the worst case scenario, which is that, by astronomical odds, Swapper succeeds in sorting the books, either in ascending or descending order. Butz can fire a quantum blast at Swapper to slow him down. Swapper can take S quantum blasts before being blasted into the Todash space, the realm between realities. However, Swapper can swap exactly one pair of books each second.
Butz isn't taking any chances. Assuming the universe bestows the ultimate curse, which is
that Swapper swaps randomly and through astronomical odds sorts the books either in
ascending or descending order (either is equally displeasing to Butz) as fast as possible,
how many quantum blasts per second must Butz fire at Swapper to prevent the atrocity of
a sorted set of books from ever manifesting on any level of reality?
Input Format
Input consists of one test case, containing N and S indicating the number of books on
the shelf and the number of quantum blasts Swapper can take before being banished into
Todash space.
The second line contains N distinct space-separated integers B₁, representing the ID
codes of the books. If Swapper sorts the books in either ascending or descending ID codes
B₁, Butz will be royally cross with him and will probably destroy several realities to relax.
NOTE: If you were an innocent bystander, you may, of course, play this as a classic trolley
problem of a.) let Swapper be banished to Todash, or b.) let countless realities perish, but
in this example, you are Butz, so no need to worry about the ramifications. :D
Constraints
1≤N, S≤ 2.105
0 ≤ B; ≤ 107
Output Format
Output consists of one line containing an integer indicating the number of quantum blasts
per second that Butz must fire at Swapper to prevent him from sorting the books in the
worst case scenario (best case for Swapper). If this number is not a whole number, just to
prevent Swapper from ever succeeding, round up to the nearest whole number. If it is
impossible for Butz to stop Swapper from restoring order to the shelf, print "Butz loses!"
without quotes.
Transcribed Image Text:Butz isn't taking any chances. Assuming the universe bestows the ultimate curse, which is that Swapper swaps randomly and through astronomical odds sorts the books either in ascending or descending order (either is equally displeasing to Butz) as fast as possible, how many quantum blasts per second must Butz fire at Swapper to prevent the atrocity of a sorted set of books from ever manifesting on any level of reality? Input Format Input consists of one test case, containing N and S indicating the number of books on the shelf and the number of quantum blasts Swapper can take before being banished into Todash space. The second line contains N distinct space-separated integers B₁, representing the ID codes of the books. If Swapper sorts the books in either ascending or descending ID codes B₁, Butz will be royally cross with him and will probably destroy several realities to relax. NOTE: If you were an innocent bystander, you may, of course, play this as a classic trolley problem of a.) let Swapper be banished to Todash, or b.) let countless realities perish, but in this example, you are Butz, so no need to worry about the ramifications. :D Constraints 1≤N, S≤ 2.105 0 ≤ B; ≤ 107 Output Format Output consists of one line containing an integer indicating the number of quantum blasts per second that Butz must fire at Swapper to prevent him from sorting the books in the worst case scenario (best case for Swapper). If this number is not a whole number, just to prevent Swapper from ever succeeding, round up to the nearest whole number. If it is impossible for Butz to stop Swapper from restoring order to the shelf, print "Butz loses!" without quotes.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Public key encryption
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole