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...
-
Describe the two ways whereby capital market securities pass from the issuer to the public.
-
In Exercises 1-2, determine whether each value of x is a solution of the inequality. 1. x2 - 3 < 0 a. x = 3 b. x = 0 c. x = 3/2 d. x = -5 2. Inequality x2 - 2x - 8 0 Values a. x = -2 b. x = 0 c. x =...
-
13. For the lookback call: a. What is the value of a lookback call as St approaches zero? Verify that the formula gives you the same answer. b. Verify that at maturity the value of the call is ST ST...
-
Here are earnings per share for two companies by quarter from the first quarter of 2009 through the second quarter of 2012. Forecast earnings per share for the rest of 2012 and 2013. Use exponential...
-
What is the investment in working capital from year 2 0 2 1 to 2 0 2 2 ? a ) $ 5 3 2 . 2 K b ) $ 2 5 5 K c ) $ 4 2 5 K d ) $ 5 7 5 K
-
The city manager of a large midwestern city was negotiating with the three unions that represented the police, firefighters, and building inspectors over the salaries for these groups of employees....
-
Include a discussion describing your experiment and commenting on experimental finding when rolling a marble down a ramp[ at different angles
-
The English statistician Karl Pearson (1857-1936) introduced a formula for the skewness of a distribution. P = 3 ( x median ) s Pearson's index of skewness Most distributions have an index of...
-
You are to specify an orifice meter for measuring the flow rate of a $35^{\circ} \mathrm{API}$ distillate $(\mathrm{SG}=0.85$ ) flowing in a $2 \mathrm{in}$. sch 160 pipe at $70^{\circ} \mathrm{F}$....
-
Let $\theta$ and $\phi$ be the polar coordinates. Introduce the complex numbers $z$ and $\bar{z}$, where $$\begin{equation*} z=e^{i \phi} \tan (\theta / 2) \equiv \xi+i \eta \tag{5.393}...
-
Suppose the profit \(P\) (in dollars) of a certain item is given by \(P=1.25 x-850\), where \(x\) is the number of items sold. a. Graph this profit relationship. b. Interpret the value of \(P\) when...
-
(a) Draw a simplified ray diagram showing the three principal rays for an object located outside the focal length of a diverging lens. (b) Is the image real or virtual? (c) Is it upright or inverted?...
-
A city government has decided to build and operate a convention center. Identify what factors should be considered in deciding whether to account for the convention center as an Enterprise Fund or as...
-
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...
-
40. The City of Pfeiffer starts the year of 2008 with the General Fund and an enterprise fund. The General Fund has two activities: education and parks/recreation. For convenience, assume that the...
-
41. Use the information in problem 40 to prepare the 2008 fund-based financial statements for the gov ernmental funds and the proprietary funds. A statement of cash flows is not required. Assume that...
-
37. The County of Maxnell decides to create a sanitation department and offer its services to the pub lic for a fee. As a result, county officials plan to account for this activity within the...
![Mobile App Logo](https://dsd5zvtm8ll6.cloudfront.net/includes/images/mobile/finalLogo.png)
Study smarter with the SolutionInn App