Write a C code and write comments. Ann and King Kong are best friends. King Kong enjoys
Fantastic news! We've Found the answer you've been seeking!
Question:
Write a C code and write comments.
Transcribed Image Text:
Ann and King Kong are best friends. King Kong enjoys playing games with Ann, no matter how silly the game is. Once, they were playing a very weird game, Ann arranged N cubes in a straight line on a table, each cube having a unique number in the range 1 to N marked all over it (essentially, the cubes when seen display some permutation of the numbers 1 to N). Now, King Kong bangs the table, the cubes fly in the air, get mixed up uniformly randomly and return to the table. Somehow, the cubes fall on the same straight line every time King Kong bangs the table, just that the positions of the cubes might change. The goal is to finally arrange the cubes such that the numbers on display are sorted in non- decreasing order. Let us assume each time King Kong bangs the table as a round. This game could go on forever. Ann comes up with an idea to speed up the process. Ann can fix (or unfix, if already fixed) any cube to the table, at its position. She can do this to any number of cubes, before any round. When some cubes are fixed and if King Kong bangs the table, the fixed cubes don t fly, hence it s position remains same after that round. Also, the cubes which fly can only return to a position where there is no cube (while in flight) (cubes cannot stack on each other, or go to any other part of the table, or out of the table). Ann wants to complete the game as fast as possible, hence she wants to use this feature optimally. Given a random arrangement of the N cubes, can you find the expected number of rounds required to arrange the cubes in non-decreasing order, given the availability of the feature to fix/unfix cubes? Input: • The first line of the input contains a single integer T denoting the number of test cases. • T test cases follow. Each test case consists of two lines. The first line contains a single integer N, the number of cubes. • The second line contains N space-separated integers, denoting the values on the cubes C₁, in order of the initial arrangement on the table. Output: For each test case, output one line containing a single number E, denoting the expected number of rounds required to arrange the cubes in non-decreasing order. Your answer must match up to the 6th decimal place. Also, output your answer till 6 decimal places. For example: If your answer is 2.3, output 2.300000. If your answer is 2.345678921, output 2.345678. Constraints: • 1 ≤ T ≤ 100 • 1 ≤N ≤ 1000 1 ≤ C₁ ≤ N • The C; s form a permutation of the numbers 1 to N Sample Input: 2 N N N 2 2 1 4 2 143 Sample Output: 2.000000 4.000000 Explanation: One possible optimal strategy for the second test case, firstly Ann can fix the first two cubes. The expected number of rounds to sort 4 3 in non-decreasing order is 2. Then, unfix the first two cubes and fix the last two cubes. In a similar manner, it takes 2 rounds, in expectation, to sort 2 1 in non-decreasing order. Therefore, in expectation, it takes 4 rounds, in expectation, to sort the entire set of cubes in non-decreasing order. Ann and King Kong are best friends. King Kong enjoys playing games with Ann, no matter how silly the game is. Once, they were playing a very weird game, Ann arranged N cubes in a straight line on a table, each cube having a unique number in the range 1 to N marked all over it (essentially, the cubes when seen display some permutation of the numbers 1 to N). Now, King Kong bangs the table, the cubes fly in the air, get mixed up uniformly randomly and return to the table. Somehow, the cubes fall on the same straight line every time King Kong bangs the table, just that the positions of the cubes might change. The goal is to finally arrange the cubes such that the numbers on display are sorted in non- decreasing order. Let us assume each time King Kong bangs the table as a round. This game could go on forever. Ann comes up with an idea to speed up the process. Ann can fix (or unfix, if already fixed) any cube to the table, at its position. She can do this to any number of cubes, before any round. When some cubes are fixed and if King Kong bangs the table, the fixed cubes don t fly, hence it s position remains same after that round. Also, the cubes which fly can only return to a position where there is no cube (while in flight) (cubes cannot stack on each other, or go to any other part of the table, or out of the table). Ann wants to complete the game as fast as possible, hence she wants to use this feature optimally. Given a random arrangement of the N cubes, can you find the expected number of rounds required to arrange the cubes in non-decreasing order, given the availability of the feature to fix/unfix cubes? Input: • The first line of the input contains a single integer T denoting the number of test cases. • T test cases follow. Each test case consists of two lines. The first line contains a single integer N, the number of cubes. • The second line contains N space-separated integers, denoting the values on the cubes C₁, in order of the initial arrangement on the table. Output: For each test case, output one line containing a single number E, denoting the expected number of rounds required to arrange the cubes in non-decreasing order. Your answer must match up to the 6th decimal place. Also, output your answer till 6 decimal places. For example: If your answer is 2.3, output 2.300000. If your answer is 2.345678921, output 2.345678. Constraints: • 1 ≤ T ≤ 100 • 1 ≤N ≤ 1000 1 ≤ C₁ ≤ N • The C; s form a permutation of the numbers 1 to N Sample Input: 2 N N N 2 2 1 4 2 143 Sample Output: 2.000000 4.000000 Explanation: One possible optimal strategy for the second test case, firstly Ann can fix the first two cubes. The expected number of rounds to sort 4 3 in non-decreasing order is 2. Then, unfix the first two cubes and fix the last two cubes. In a similar manner, it takes 2 rounds, in expectation, to sort 2 1 in non-decreasing order. Therefore, in expectation, it takes 4 rounds, in expectation, to sort the entire set of cubes in non-decreasing order.
Expert Answer:
Answer rating: 100% (QA)
The c code of the corresponding problem is attached below include include int mainvoid setbufstdoutN... View the full answer
Related Book For
College Mathematics for Business Economics Life Sciences and Social Sciences
ISBN: 978-0321614001
12th edition
Authors: Raymond A. Barnett, Michael R. Ziegler, Karl E. Byleen
Posted Date:
Students also viewed these algorithms questions
-
Plug Products owns 80 percent of the stock of Spark Filter Company, which it acquired at underlying book value on August 30, 20X6. At that date, the fair value of the noncontrolling interest was...
-
The following information was obtained from the records of Shae, Inc.: Merchandise inventory . . . . . . . . . . . . . . . . . . . . . . . . . $264,000 Notes payable (long-term) . . . . . . . . . . ....
-
Write a C code for Insertion Sort Insert element into sorted list Given a sorted list with an unsorted number in the rightmost cell, can you write some simple code to insert into the array so that it...
-
The following questions deal with the topics of internal controls and audit strategies. Required: Select and justify the audit strategy you would choose in each of the following situations. Comment...
-
Refer to Exercise 5.18. Assume that the sampling was conducted with replacement. That is, assume that the first ball was selected from the jar, observed, and then replaced, and that the balls were...
-
Cialdini, Reno, and Kallgren (1990) examined how people conform to norms concerning littering. The researchers wanted to determine whether a person's tendency to litter depended on the amount of...
-
Describe various forms of child abuse, how to recognize it, and reporting requirements.
-
During 2011, Stanton Corporation reported after-tax net income of $3,600,000. During the year, the number of shares of stock outstanding remained constant at 10,000 of $100 par, 9 percent preferred...
-
1 . What is the starting point in doing a projected discounted cash flows analysis?A . Start with the projected increase in assets.B . Start with the required interest rate.C . Start with the sale...
-
D Question 1 What is the total balance in the Trial Balance on the Debit side from your Worksheet? $339000 $409,000 $388,500 $393,000 Question 2 8 pts $97,000 O $104,000 O $11,000 O $106,000 8 pts...
-
The Yellow Book standards relating to financial audits apply to independent CPA firms as well as government audit departments. The following descriptions relate to an independent CPA firm that...
-
Find the requested probabilities in Problems 11-14. \(P(\bar{A})\) if \(P(A)=0.6\)
-
What is the probability of obtaining at least one tail when a coin is flipped five times?
-
In Problems 13-24, suppose a die is rolled twice and let \[\begin{array}{ll}A=\{\text { first toss is a prime }\} & B=\{\text { first toss is a } 3\} \\C=\{\text { second toss is a } 2\} & D=\{\text...
-
Find the binomial probabilities in Problems 3-10. \(n=10, X=4, p=0.80\)
-
In old gangster movies on TV, you often hear of "number runners" or the "numbers racket." This numbers game, which is still played today, involves betting \(\$ 1\) on the last three digits of the...
-
As an estimator working for a contractor to prepare bid on a project, why is it important to visit the site as part of the cost estimation process Group of answer choices Assess the competition...
-
Identify the most stable compound:
-
In Problem, graph each inequality subject to the non negative restrictions. 24x + 30y > 7,200, x 0, y 0
-
The shelf life (in years) of a laser pointer battery is a continuous random variable with probability density function (A) Find the probability that a randomly selected laser pointer battery has a...
-
In Problems 29-34, find the limiting matrix for the indicated standard form. Find the long-run probability of going from each non absorbing state to each absorbing state and the average number of...
-
Which of the following statements are hypotheses? (a) Heavier objects fall to Earth faster than lighter ones. (b) The planet Mars is inhabited by invisible beings that are able to elude any type of...
-
A couple in a sport-utility vehicle (SUV) is traveling at 90 km/h on a straight highway. The driver sees an accident in the distance and slows down to 40 km/h in
-
(a) How many significant digits are there in \(403.54 \mathrm{~kg}, 3.010 \times 10^{57} \mathrm{~m}\), \(2.43 \times 10^{-3} \mathrm{~s}, 14.00 \mu \mathrm{m}, 0.0140 \mathrm{~s}, 5300...
Study smarter with the SolutionInn App