Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python or java. The time complexity has to be as less as possible (nlogn or n at best, no n^2). Hint: Use inversion index nlogn approach 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 in Python 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() The actual code in java import java.io.BufferedReader; import java.io.InputStreamReader; public class c {     public static void main(String[] args) throws Exception {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         StringBuilder sb = new StringBuilder();                  String[] parts = br.readLine().trim().split(" ");         int n = Integer.parseInt(parts[0]);         int s = Integer.parseInt(parts[1]);         int[] books = new int[n];         parts = br.readLine().trim().split(" ");         for(int i = 0; i < n; i++) {             books[i] = Integer.parseInt(parts[i]);         }         solve(books,n,s);     }     public static void solve(int[] books, int n, int s){         // TODO: Compute and print answer here                } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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 or java. The time complexity has to be as less as possible (nlogn or n at best, no n^2).

Hint: Use inversion index nlogn approach

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 in Python

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()

The actual code in java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class c {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        String[] parts = br.readLine().trim().split(" ");
        int n = Integer.parseInt(parts[0]);
        int s = Integer.parseInt(parts[1]);
        int[] books = new int[n];
        parts = br.readLine().trim().split(" ");

        for(int i = 0; i < n; i++) {
            books[i] = Integer.parseInt(parts[i]);
        }
        solve(books,n,s);
    }

    public static void solve(int[] books, int n, int s){
        // TODO: Compute and print answer here
          
    }
}

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.
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.
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Hash Table
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education