C Queen

download C Queen

of 90

Transcript of C Queen

1. The beginning of a record is identified by the begin-record-line that contains in addition to the `@' character and the record number, a number n denoting the number of options for recovering the record. A blank character precedes the number n. In case a record cannot be recovered due to any contradiction between the information contained in the record and the information provided by the company, the option number n is set equal to zero. The beginrecord-line is followed by n options for recovering the record. Each option begins with a begin-option-line that contains a number of slash character (/) pertinent to the option number, e.g., the second option will begin with a line containing two slashes (//). Each option for recovering a record contains fields in the normal order. The file ends with the end-file-line that contains in addition to the character `$' and the total number of records in the file, a number m denoting the total number of records recovered. A blank character precedes the number m. The output file structure is illustrated in sample output. Sample Input @1 gamma 436502; Beta Alpha; Nu5 321902; Epsilon, Theta; eta 123206; Sigma Kappa, Lambda; Zeta Delta; @2 Alpha, Beta; eta 123206; Sigma Kappa, Lambda; Nu5 321902; gamma 436-502; Epsilon, Theta; $2 Sample Output @1 0 @2 2 / gamma 436-502; Beta Alpha; Sigma Kappa, Lambda; Nu5 321902; eta 123206; Epsilon, Theta; // gamma 436-502; Theta Epsilon; Sigma Kappa, Lambda; Nu5 321902;

eta 123206; Alpha, Beta; $2

2. Normally for a given problem you are asked to find the solution. Here you are required to write a program to find the problem for which the solution is given. Let f = m/n be a proper fraction and F = {f1, f2,..., fk} be a set of k distinct unit fractions fi = 1/ni , i = 1, 2,..., k , where ni (not equal to 1 or n ) is a factor of n and k is a suitable integer. Recall that a proper fraction f is a number of the form / , where the numerator and the denominator are positive integers and 1 < . A unit fraction is a proper fraction with = 1 and > 1 . Since the numerator of each element of F is 1, the set F may be identified also by the set D = n1, n2,..., nk of denominators appearing in the elements of F. Consider the problem: given the set F , find the sum of elements of F and its solution: the sum f , where k is a nonnegative integer as large as possible. Given a solution f you are required to write a program to find F or equivalently find D . It should be noted that for a given solution there may exist no problem, exactly one problem or more than one distinct problem. Let p be the total number of distinct problems for a given solution. For example if f = then p = 2 , F = { , , } or { , , } and D = {3, 8, 12} or {4, 6, 8} . Again if f = then p = 0 since no F or D exists for the given f . Input The input may contain multiple test cases. For each test case there are two input lines. The first line contains the case number c and the second line gives the numerator m and the denominator n . The input terminates with an input `0' for c . The input is illustrated in sample input. Output For each test case print c , k and p in one line, where c is the test case number, k is the largest possible number of distinct unit fractions in F and p is the total number of distinct problems. In each of the next p lines, print a problem represented by k elements of D. The elements of D are printed in increasing order of magnitude. The problems are to be arranged in lexicographic order of the elements of D. Print a blank line between two successive test cases. Sample Input 1 13 18 2 13 24 0 Sample Output

100 232 3 8 12 468

3. The ABC class of 2003 consists of all outgoing students of ABC Institute of Information Technology of the year 2003. The class is engaged in developing a web site that aims to provide free services of various kinds. Considering the fact that people often lose contact with their old classmates and are eager to establish the lost contact after long years of separation a link has been kept in the web site for users to register and provide information about last school/college/university they attended and classmates they remember. Information gathered from users is included in a database that is used to process personalized queries related to information about classmates. In order to attract users, the site aims to display, while responding to personalized queries, some statistical information related to the database. In particular it is of interest for the users to know the largest number of classmates that exists in the database, who studied together in the same institution/institutions, viz., a) The same school (S), b) The same college (C), c) The same university (U), d) The same school and the same college (SC), e) The same school and the same university (SU), f) The same college and the same university (CU) and g) The same school, the same college and the same university (SCU). 4. The ABC class of 2003 seeks your help in writing a program to compute the seven statistics stated above. Assume that all registered users and classmates for whom information is given by a user are recorded in the database by name and associated school, college, university and years. However each one of them is also identified uniquely by an integer assigned arbitrarily. The integer representation is used in input/output. It should be noted that when a user A is listed as a classmate by another user B the user A may or may not list B as classmate. Further if A and B are classmates at a given level (school/college/university) then all classmates of A (or B) are classmates of B (or A) and A, B and their classmates attended the same institution at that level. Input The input may contain multiple test cases. For each test case the first line contains two integers K and N . The integer K is the case number and N( 0;y > x) and are determined so that the total amount deposited in r years is exactly equal to the specified amount T . Assume that all deposits are in whole number of Rupees. Write a program for the bank, so that given r , k and T , the program computes x and y for which the return R is maximum. For example in a 4-year scheme with r = 4 , k = 3 and T = 500 , the progressively reducing recurring deposits 248, 126, 122 and 4, ensures the maximum return R = 744 with x = 4 and y = 122 . Input The input may contain multiple test cases. For each test case there is only one input line. The line gives values of r , k and T . Assume that r is not greater than 20. A line containing a zero `0' as the first character follows the last case. Output For each test case there is only one output line. The line gives the computed values of x , y and R . Sample Input 4 3 500 5 3 10000 6 4 8000 8 5 12000 0 Sample Output 4 122 744 5 1425 12855 1 666 13332 1 363 23635

61. A baby tries to solve the well-known eight-queen puzzle: the problem of placing eight chess queens on an 88 chessboard so that no two queens share the same row, column, or diagonal. The baby understands the concept of row and column quite well but diagonal is not very clear to her. As a result she succeeds placing eight queens on the board so that no two queens share the same row or column but there remains the possibility that some queens share the same diagonal. Given baby's queens (a solution by the baby) and a valid eight-queen solution, it is possible to move baby's queens to positions of queens in the valid solution. Assume that in a single move, a queen can be moved one unit row-wise or column-wise into an unoccupied position.

Write a program to find the minimum number of moves required to move baby's queens to positions of queens in the valid solution. The program should be usable for a more general n -queen puzzle where n queens are placed on an nn chessboard, 4 n 16 . Assume that rows and columns of the chessboard are numbered 1, 2,..., n . Input The input consists of multiple test cases. Each case begins with a line containing the integer n . Each of the next two lines contains a sequence of n integers. Integers in the first line represent column numbers of baby's queens appearing in rows 1, 2,..., n respectively. In the same way, the second line contains column numbers of queens in the given valid solution. A space separates two consecutive integers in the sequence. A line containing a zero `0' as the first character follows the last case. Output For each test case, print the minimum number of moves required. Sample Input 4 1234 3142 4 3241 3142 5 53142 53142 5 15243 31425 0 Sample Output 6 2 0 8 62. Barracks in a garrison are structured in N rows and N columns with certain security arrangements at each row-column position. Most of the positions are well guarded; however certain positions remain weakly guarded. All positions on the boundary, either well guarded or weakly guarded, are considered vulnerable. With respect to a given vulnerable weakly guarded position, positions appearing next to it column-wise/row-wise/ diagonally, are considered vulnerable. Thus for a given vulnerable weakly guarded position there are at most eight vulnerable positions. Positions, which are

neither vulnerable nor weakly guarded, are considered strongly secured. You are required to write a program to find all strongly secured positions in the garrison. Let a zero `0' identify a weakly guarded position and a one `1' identify a wellguarded position. For illustration, in a garrison with barracks in 5 rows and 5 columns, the strongly secured positions are identified below by (1). 01110 10111 1 1 1 (1) 1 1 (1) 0 (1) 1 11111 Input The input may contain multiple test cases. For each case, the first line gives the value of N , which is not greater than 20. The next N lines identify security status of each position in the garrison. The i th line identifies the status in the i th row of barracks, i = 1, 2,..., N . Each line appears as a string of size N , containing 0's and 1's. Each 0 identifies a weakly guarded position while each 1 identifies a well-guarded position. A line containing a zero `0' as the first character follows the last test case. Output For each test case there are two output lines. The first line gives the total number S of strongly secured positions. If S is equal to 0 then there is no further output. Otherwise there is an additional line of output. It identifies all S positions, listing row number and column number of positions in row-major order. The line contains 2S integers separated by space. Sample Input 5 01110 10111 11111 11011

63. .Allocation of platforms to trains is a usual problem in a busy railway station. Trains arrive at a station, halt forspecified times at a platform and finally depart from the platform. This apparently simple process is not so simple particularly because platforms are not always available at the time of arrival of trains. Department of Railways needs a program for allocation of platforms to trains.Let there be p platforms in a railway station. Arrival times of trains are known for a period of one-hour. Times are recorded as integers; each integer represents a time in minutes counted from the beginning of the one-hour period under consideration. Arrival times vary from 0 to 60 while departure times may exceed 60 as

well. On its arrival a train waits in the yard to get the signal to go to a platform. As and when a platform is available a waiting train in the yard with the least halting-time is selected arbitrarily for allocation of a platform. The halting-time of a train is the difference between the departure time and the arrival time. Assume that no extra time is needed for a train to move from the yard to a platform. The availability of platforms and the haltingtime of trains if any in the yard, at the beginning of the one-hour period under consideration, are known.You are required to write a program to find the sum W , of waiting times in minutes for all trains including trains if any waiting in the yard at the beginning of the one-hour period under consideration. The waiting timeof a train is the time the train waits in the yard during the one-hour period under consideration. Input Input may contain multiple test cases. Each test case consists of two input lines. The first line contains a sequence of integers. The first integer in the line is the total number of platforms p .The next p integers represent times when platforms 1, 2,..., p respectively are available for the first time during the one-hour period under consideration. Next k( 0) integers in the line, if any, represent halting-times for k trains waiting in the yard from the beginning of the one-hour period under consideration. The second line contains an even number of integers. Integers in an odd and the next even numbered positions in the line, give the arrival and the departure times of trains. A line containing `0' terminates input. Output For each test case output the sum W defined above. Sample Input 1 15 30 20 35 10 30 45 65 2 15 0 20 35 10 30 45 65 0 3322 64. An organization has a well-structured human resource. The Chief Executive Officer (CEO) is the top boss of the entire organization. He is the most important personnel and has no boss to report. All other personnel in the organization have a boss to report directly. A boss in tern has direct control over a number of subordinate personnel who are ordered according to their importance. A subordinate under the direct control of a boss may or may not be a boss.All personnel including the CEO are ordered totally according to their importance in the organization. A personnel X is considered more important than another personnel Y if any of the following is true: X is the CEO, X is not the CEO but is the boss of Y X and Y have the same boss and X is more important than Y , X and Y have different bosses and boss of X is more important than boss of Y .

Let the organization has n personnel. A personnel is identified by a distinct integer 1, 2,... or n , called the ID of the personnel, according to the order of importance. The most important personnel viz., the CEO has ID equal to 1, while the least important personnel has ID equal to n . A boss may communicate directly to a subordinate in 1 (one) unit of communication time (UCT) while a subordinate requires 2 (two) UCT to communicate with the boss. The organization maintains a list L of all personnel identified by their IDs. A boss appears in L only after all direct subordinates appear in order of their importance. Given L and two personnel X and Y , you are required to write a program that finds the time T in UCT for X to communicate a message to Y . Input The input consists of multiple test cases. Each test case contains data in two lines. The first line gives three integers n , X and Y defined above. The next line gives a sequence of n integers representing the list L . Fields in input are separated by space. The input terminates when a line containing `0' appears. Output For each test case output the required time T as an integer. Sample Input 724 2674531 742 2674531 0 3323

65. Dates are written using varying formats. A consulting firm deals with clients who use in their communications formats stated below for writing dates. 1.Formats for each field: a.Date (d ): one digit as in 4 or two digits as in 04. b.Month (m ): one digit as in 2, two digits as in 02, name in full as in February. c.Year (y ): low order two digits for a year in the current century as in 05 or all four digits as in 1997 or 2005. 2.Order of fields: dmy , myd , mdy or ymd . 3.Delimiters between fields: Any one of the following in a date; Colon (:), Dash (), Dot (.) or Slash(/). The firm maintains communications in chronological order with the most recent communication at the top. It is possible to have more than one communication on the same date. A date is considered to be in standard format if it appears with full name of

the month, significant digits of the date, all four digits of the year and delimiters space and comma, as in the date February 2, 2005. You are required to write a program that converts each date in a given sequence of dates in chronological order, to the date in the standard format. If more than one interpretation exists for a date then select the most recent date. In case a date violates the assumptions stated above, ignore the defective date all together and consider other dates in chronological order. Input Input consists of multiple test cases. Each test case starts with a line that gives the total number n (0 n 20) of dates in the given sequence. The next n lines give n dates in chronological order, the first line containing the most recent date. Input terminates when n is equal to 0. Output For each test case, print dates in standard format on n lines in the order of dates in input. For each defective date, output `?' against the date. Add a blank line to separate outputs of two test cases. Sample Input 3 02-04-05 06/06/06 02.04.2005 2 02:04:23 02:04/23 2 02/February/03 02.April.01 0 Sample Output April 2, 2005 ? April 2, 2005 April 2, 2023 ? February 2, 2003 April 1, 2002

66. Examination halls in a college contain seats in m rows and n columns. There are k (n k < 10) groups of students. Students of each group appear in the same paper; however students of different groups appear in different papers. Groups as well as seats are identified by integers 1, 2,..., k . A student of a group seats on a seat having the same identification number as that of the group. For allocations of seats in a hall following rules are followed: a) Each row contains at most one student from a group.

b) Each column contains an equal number of students of each group. c) No two students of the same group seat in neighbouring positions, i.e., next to each other in the same row, the same column or the same diagonal. Controller of examination sends a team of staff to identify seats in halls following the rules stated above.However after identifying certain number of seats they find it difficult to identify other seats satisfying the rules. So Controller wants a program that can complete a partial allocation of seats in an examination hall. Input Input may contain of a number of test cases. Each test case starts with a line that gives parameters m , n and k as integers. The next m lines give partial allocations of seats made by the staff; the i -th line gives the partial allocation in the i -th row, for i = 1, 2,..., m . A string of length n represents a partial allocation in a row. The j -th character in the string is an integer 1, 2,... or k if the j -th seat in the row is already allocated; otherwise the j -th character is a `#', for j = 1, 2,..., n . A line containing `0' terminates input. Output For each test case, output a complete allocation in m lines replacing each `#' in partial allocation by an appropriate integer 1, 2,... or k and separating two integers in a line by space. If a complete allocation is not feasible then output a line containing `'0. Separate outputs of two test cases by a blank line. Sample Input 545 1### #4## ##2# ###5 ##1# 333 67. Here is a problem on Geometry. A square non-defective alloy steel plate is required to be cut off from a rectangular plate of the same material containing defects at a set of n given points. The defective points (xi, yi) , i = 1, 2,..., n , are identified with reference to a 2-dimensional Cartesian coordinate system. Let two perpendicular sides of the given rectangular plate form the axes of coordinates and the line segment joining the points (0, 0) and (a, b) form a diagonal of the rectangular plate. You are required to write a program that finds the area of a largest square plate that can be cut off from the given rectangular plate so that none of the defective points is inside the square plate. For simplicity consider only the squares whose sides are parallel to the coordinate axes.

Input The input consists of a number of test cases. Each test case contains two lines of data. The first line gives an integer n and two real numbers a , b . These are the parameters defining the total number of defective points and the rectangle itself. The next line gives 2n real numbers specifying x and y coordinates of the n given points. A number appearing in an odd numbered position in the line defines the x coordinate while the next number defines the y coordinate of a point. You may assume that the magnitude of a coordinate is less than 100. A line containing `0' terminates input. Output For each test case output the required area as a real number. Sample Input 4 5.0 6.0 1.0 2.0 2.0 3.0 1.0 3.0 4.0 4.0 2 3.0 4.0 1.0 2.0 2.0 3.0 0 Sample Output 9.0 4.0 68. A sports meet was organized at a ground close to a wide shallow stream. Animals cross the stream at ease, stepping on stones forming a path in a line from one bank A to the other bank B of the stream. Apes are known for their intelligence as well as for their ability to imitate men. Two friendly groups of apes, one white and the other black, live together happily on two banks of the stream. They observed keenly from their home trees, sport events that went on, on the nearby ground. After the meet apes were seen crossing the stream in an organized manner. In fact they were playing, imitating Hop Step & Jump in their own intelligent way. Let there be a certain number of stones forming the path. At a given instant apes position themselves arbitrarily on stones in the path, keeping some stones empty. No two apes are on the same stone. White apes move toward bank A, while black apes move toward bank B until all of them reach the bank in the direction of motion. At no time an ape changes the direction of motion. Let P , Q and # represent respectively a stone with an ape of one group, a stone with an ape of the other group and an empty stone. Thus a string of P , Q and # represents an occupancy of apes on neighbouring stones at a given instant. The movement of an ape is restricted to the following valid moves: Step: An ape on P moves to the next empty stone, changing occupancy from P# to #P in the direction of motion of the ape.

Jump: An ape on P jumps over an ape on Q and occupy the empty stone situated next to Q , changing occupancy from PQ# to #QP in the direction of motion of the ape. Hop Step & Jump: An ape on P hops over an ape on Q to an empty stone and steps on to the next empty stone. Finally the ape jumps over another ape on a Q to occupy the next empty stone. In other words the movement of the ape on P changes the occupancy from PQ##Q# to #Q##QP in the direction of motion of the ape. Reach: An ape on P reaches the destination finally using a Step, a Jump or a Hop Stop & Jump move, considering the destination bank as a stone. Apes cooperate with one another irrespective of the group they belong to. They organize their movements with a clear objective: maximize the total number of Hop Stop & Jump so that all of them reach their respective destinations. You are required to write a program that determines, for a given initial occupancy, the maximum number of Hop Stop & Jump that can be used by apes to reach their respective destinations. Input The input may contain a number of test cases. Each test case consists of a single input line that gives the initial occupancy of apes on stones. The line contains a string of `0', `1' and `#' that begins with `A' (or `B') and ends with `B' (or `A'). A zero (0) represents a stone with a white ape while a one (1) represents a stone with a black ape. A line containing a single `#' terminates input. Output For each test case, output the maximum number of Hop Stop & Jump that can be used by all apes jointly to reach their respective destinations. There could be a possibility of a deadlock when some apes cannot reach their destinations using the valid moves. In such a case output the value `-1'. Sample Input A111####000B B111####000A A#01#1100B B#01#1100A # Sample Output 4 0 -1 1

69. Alias is an assumed or additional name that constitutes a distinctive designation of a person. Consider a set of n persons and assume that each person has k distinct aliases. A person is identified using any one of its k aliases. The nk (= nk) distinct aliases are identified using integers 1, 2,...nk . Integers 1, 2,..., n represent the first aliases of all n persons in an arbitrary order. In general, integers (j - 1)n + 1,(j - 1)n + 2,...,(j - 1)n + n represent the jth alias of all n persons in an arbitrary order, for j = 1, 2,..., k . Persons in the set are totally ordered with respect to a quality characteristic Q associated with each person. Let Q (r) be the value of Q for a person identified by one of its alias r . Given a sufficient number, say m , of inequalities of the type: Q(r) > Q(s) , you are required to write a program to sort all persons in descending order and recognize all aliases of each person in the set. As a simple illustration consider distinct total marks scored by three students in an examination. Each student is identified by any one of three distinct aliases in the Name: {first-name middle-name last-name}. Let integers 1, 2, 3 represent the first names, 4, 5, 6 represent the middle names and 7, 8, 9 represent the last names in an arbitrary order. Let Q(r) be the total marks of student r, r being an alias. Given the following inequalities: Q(6) > Q(1), Q(9) > Q(4), Q(5) > Q(8), Q(2) > Q(9), Q(7) > Q(3), Q(9) > Q(3) , one can conclude that the names of students appearing in descending order of total marks are {2 6 7}, {1 5 9} and {3 4 8}. Input The input may contain multiple test cases. For each test case the first input line gives the parameters n , k and m . The second line contains m inequalities represented by 2m integers. An integer r occurring in an odd numbered position in the line and the integer s occurring in the next even numbered position, represent the inequality Q(r) > Q(s) . Assume that nk is less than 100 and each integer in the second input line is of two digits, including a non-significant 0 when required. The input terminates with a line containing 0 as input. Output For each test case print n lines giving k aliases of each person in a line; a line contains aliases in increasing order. Arrange persons in descending order of the quality characteristic Q . As in input, each integer in output is of two digits, including a non-significant 0 when required. A blank line appears after the last output line of a test case. Sample Input 336 06 01 09 04 05 08 02 09 07 03 09 03 3764 242 03 08 02 05 0

Sample Output 02 06 07 01 05 09 03 04 08 02 03 06 07 01 04 05 08

70. Begin a sequence of distinct natural numbers Ni , i = 0, 1, 2,... , with the number B (= N0) ; generate numbers Ni , i = 1, 2,... , recursively and end the sequence with the last generated number E . The characteristic of numbers and the process for generation are stated below: Each number in the sequence contains an even number of decimal digits and is of the form f1d1f2d2fk...dk where d1, d2,..., dk , are k distinct digits in increasing order and each fj is a non-zero digit. For i = 0, 1, 2,... , if Ni = f1d1f2d2...fkdk then Ni+1 = F1D1F2D2...FKDK , where K k ; D1, D2,..., DK ,are distinct digits that occur in Ni and appear in increasing order in Ni+1 ; and FJ is the frequency of DJ in Ni , for J = 1, 2,..., K . For example if Ni = 102335 then Ni+1 = 1011122315 . Write a program to find for a given E , the longest sequence of numbers that ends with E and begins with the smallest B . Again consider an example; if E =1011122315 then the required sequence of numbers is 303355 103325 1011122315. Input The input may contain multiple test cases. Each test case contains only one input, viz., E . The input terminates when a line containing 0 appears as a test case. Output For each test case, print the longest sequence of numbers that ends with E and begins with the smallest B . Use space to separate two consecutive numbers in the sequence. Sample Input 1011122315 22 112213 0 Sample Output 303355 103325 1011122315 22 13 1113 3113 2123 112213

71. Crossbits are like Crosswords; instead of entering words you enter binary bits 01 in a Crossbit under certain given conditions, assuming that a solution exists. An empty Crossbit

of size N is an empty grid of size NN . Given a natural number N , consider entering N2 binary bits in an empty Crossbit, satisfying the following conditions: Each square in the grid contains either a 0-bit or a 1-bit with no 1-bit in two major diagonals.The total number of 1-bit in each row / column is exactly equal to K , K being a given natural number less than N . A 0-bit has at least another adjacent 0-bit either in the same row or in the same column. The Crossbit represents the N2 -bit binary number B formed by placing bits in the 1st , the 2nd , ... theNth row from left to right You are required to write a program that enters bits in an empty Crossbit so that the Crossbit represents the least binary number B for given N and K . As an illustration consider the case with N = 4 and K = 1 . The Crossbit shown below represents the least binary number B = 0010100000010100 of 16 bits satisfying the specified conditions. Input The input may contain multiple test cases. For each test case parameters N and K of the Crossbit are given in one line. Assume that N does not exceed 10. The input terminates with a line containing 0 as input. Output For each test case, print the Crossbit in N rows; each row contains N bits with a space between two neighbouring bits. Keep a blank line after the last output line of each test case. Sample Input 41 62 0 Sample Output 0010 1000 0001 0100 3766 - Crossbits

72. Disputes between two warring parties over possession of properties are often settled amicably with the intervention of a third party. You are required to write a program on behalf of a consultancy firm that settles disputes amicably over possession of properties between two warring parties.The dispute between two warring parties A and B, is over a set of n profit making industrial units (PMIU) thatcurrently make profits P1, P2,..., Pn . Assume that n is an even number less than 20. Each profit is a distinct integer representing rounded profit in crores of rupees. A profit identifies a PMIU and determines its valuation. For a subset of PMIU, the product of all profits from PMIU in the subset determines the total valuation of the subset. The two warring parties have agreed to accept a solution that divides the set of n PMIU into two disjoint subsets satisfying the following conditions: 1. The total number of PMIU in each subset is n/2 .

2. The total valuation of each subset is the same. 3. The subset with higher total sum of profits is allocated to A. Write a program that determines the subset of PMIU to be allocated to A, assuming that there exists a unique solution to the problem. As a simple example consider 6 PMIU with profits 2, 4, 5, 12, 15 and 18. The subset of PMIU allocated to A is {2, 12, 15}. Input The input may contain multiple test cases. For each test case there is a single input line. The line gives a set of distinct integers representing profits of PMIU. The input terminates with a line containing 0 as input. Output For each test case there is only one output line. The line prints the subset of PMIU allocated to A in ascending order of profits. Sample Input 8 12 10 15 2 4 5 12 15 18 0 Sample Output 8 15 2 12 15

73. Elimination of contestants from a live IQ contest on a TV channel is decided in phases. Initially at phase 0, N contestants, where N = 2n , n < 10 , are selected through a special online IQ contest in which a total of M (M > N) contestants participate. The contestants are identified by distinct registration numbers 1, 2,..., M . The selected contestants are ranked distinctly from 1 to N according to their performance in the online contest. They are qualified to participate in the live contest. In the pth phase, p = 1, 2,..., n, Kp contestants participate in the live contest, where Kp = 2n-p+1 . On the basis of response to questions presented during the show, Kp/2 of Kp contestants are ranked distinctly from 1 to Kp/2 . These Kp/2 contestants qualify to participate in the next phase. At the nth phase there are only two contestants and the one selected at this phase is the winner of the contest.You are required to write a program that identifies the winner of the contest, given the following information: INFO_1: Registration numbers of N contestants who are selected through the online IQ contest, in order of the rank in the online IQ contest, and INFO_2: A total of N - 1 qualified contestants in different phases; K2 in phase 1, K3 in phase 2, ... ,and Kn+1 in phase n . Qualified contestants of different phases appear in order of phases, i.e., phase 1,phase 2, ... , phase n . Further, qualified contestants in a phase, say phase p , appear in the order of therank in the phase, i.e., the rank in phase p .

A qualified contestant of a phase, say phase p , is identified by his/her rank in the previous phase, i.e., in phase p - 1 . Input Input may contain multiple test cases. For each case there are two input lines. The first line gives N integers representing INFO_1 while the second line gives N - 1 integers representing INFO_2. In each input line integers are separated by space. The input terminates with a line containing 0 as input. Output For each test case there is only one output line. The line prints the registration number of the winner of the contest. Sample Input 23 18 6 20 422 29 57 4 33 5 12 16 18 7153211 0 3768

74. Friends are as friends should be. Friends believe each other, help each other and above all share joys, sorrows and problems. Friendship is mutual.A college is on vacation. Classmates of a class are in their respective homes. It is a common knowledge to all classmates, who is a friend of whom. Friends among classmates are in direct contact through SMS. A classmate may send SMS to certain specified classmates through friends in minimum time. Assume that it takes exactly one unit of time to send an SMS from a sender to a receiver. When a classmate initiates transmitting an SMS at time 0 to a friend, the friend receives the SMS at time 1. At time 1 both the initiator and the friend may send SMS to two different receivers simultaneously. Consider all senders at time t and all receivers at time (t + 1), t = 1, 2 , etc. Senders are classmates who have either received SMS at time t or sent the last SMS at time (t - 1) . Receivers are classmates who have not received SMS as yet and are friends of one or more senders. Who sends SMS to whom and when? The initiator decides in advance senders, receivers and times so that the SMS reaches specified classmates in minimum time. This information is transmitted to each receiver along with the SMS. Write a program that finds the minimum time required for an SMS from a given initiator to reach three specified classmates through friends. Assume that the total number of classmates in a class does not exceed 50 and the roll number identifies a classmate. Input The input may contain multiple test cases. For each test case there are two input lines. The first line identifies friends among classmates. It gives an even number of integers representing roll numbers. Each pair of integers in the line, one appearing in an odd numbered position and the other appearing in the next even numbered position,

represents roll numbers of a pair of friends among classmates.The second line gives four integers. The first integer is the roll number of the initiator while the other integers are the roll numbers of three specified classmates. Integers in an input line are separated by space. The input terminates with a line containing 0 as input. Output For each test case, print output in a single line. The line gives the minimum time required for the SMS from the initiator to reach specified classmates through friends. Sample Input 3224254515 4123 3224254515 2345 134672413676325185 1678 3769

75. You are invited to be a part of the team that is developing yet another DBMS

(Data Base Management System). You will be responsible for the Lock Manager. Locks control concurrent access to data items by multiple transactions. Your DBMS is simple and uses only Shared (S) and Exclusive (X) mode locks. Each lock request contains a lock mode (S or X), a transaction identifier and a data item identifier. Multiple locks can be granted to the same data item as long as none of them conflict. Two locks for the same data item conflict if: they belong to different transactions, and at least one of them is exclusive (X) mode lock. At the earliest stages of development you are asked to write very simple lock manager that processes lock requests. The lock is granted if it does not conflict with previously granted locks for this data item. Your task is simple: locks, once granted, are never released or changed in any way. If lock request is denied due to conflict with some previously granted lock, then transaction making this request is blocked and all further requests from this transaction are ignored. Input The first line of the input consists of an integer indicating the number of test cases in the input. Then there is a blank line and the test cases separated by a

blank line.Each test case consists of a number of lock requests, each request on a different line. Requests have the following format:MODE TRID ITEM

Where MODE is a single capital letter S or X denoting requested lock mode. TRID and ITEM are transaction identifier and data item identifier correspondingly. Both TRID and ITEM are integers, both are greater than zero, and both consist of at most 9 decimal digits. There are at least one and at most 10000 requests in each test case. The last request is followed by a line consisting of a single character '#'. Output Your program shall sequentially process all requests from each test case. For each request you should write one line that contains the response to the request. The following responses are allowed: GRANTED - the lock request does not conflict with any previously granted locks and is granted. DENIED - the lock request conflicts with some previously granted lock and is denied, thus blocking the requesting transaction. IGNORED - the transaction was blocked on some request before this one. Responses shall appear in all capital letters exactly as shown above. Print a blank line between data sets. Sample input1 S S X S S X S S S X X 1 1 2 2 10 1 6 123456789 3 3 2 2 5 6 3 1 3 2 987654321 123456789 1 4

S S S X S #

6 3 2 4 2

6 5 4 5 51

Output for the sample inputGRANTED GRANTED DENIED GRANTED GRANTED GRANTED GRANTED GRANTED DENIED DENIED GRANTED GRANTED IGNORED DENIED GRANTED IGNORED

76. Mr. Smith received a loan for Q dollars. He plans to pay off this loan in K years

at an interest rate of P percent per year. That means that, after each year, Mr. Smith's debt grows by P*Q'/100 dollars (Q' being the debt at the beginning of that year) and his annual payment is deducted from his debt. For the first year, Mr. Smith wants to pay minimal amount that will allow him to pay off the loan within exactly K years. For each subsequent year, he is willing to pay either the same amount as the previous year or one cent less than the previous year's payment. He wants the loan to be completely paid off without overpayment of even a single cent by the end of the Kth year. The bank performs all transactions with a precision of one cent, and calculates interest due at the end of each year. Whenever interest is calculated, the result is immediately rounded to the nearest cent, with 0.5 cents rounded up. Input The first line of the input contains an integer indicating the number of test cases in the input. Then there will be a blank line, and the test cases separated by a blank line. Each test case consists of a single line with three

numbers Q,P and K, separated by spaces. Q is a real number (10 Q 1000000) given with no more than two digits to the right of the decimal point. This value represents the amount of the loan in dollars. One one-hundredth of a dollar is a cent. P and K are integers (0 P 100, 1 K 100). Output Write to the output file a schedule of payments for Mr. Smith. You should write the amount of each payment and number of years that payment should be made, thus effectively grouping equal payments. Each group of equal payments must be written on separate line, with no blank lines between them. The output format for each group of payments is:$X for Y year(s)

where X is payment amount in dollars printed with exactly two digits after decimal point. Y is a number of years for which this payment should be made. The dollar value given on each line must be one cent less than the dollar value printed above it. If there are multiple correct payment schedules, you can output any one of them, but the first payment should be the minimal possible one. If no solution can be found for the given input, then the output file shall contain only the word "Impossible". Print a blank line between test cases. Sample Input3 100 100 100 20 0 10 939850.83 85 35

Sample OutputImpossible $2.00 for 10 year(s)

$798873.22 $798873.21 $798873.20 $798873.19 $798873.18 $798873.17 $798873.16 $798873.15 $798873.14 $798873.13 $798873.12 $798873.11 $798873.10 $798873.09 $798873.08 $798873.07 $798873.06 $798873.05

for for for for for for for for for for for for for for for for for for

1 1 1 1 1 4 1 2 1 3 1 4 2 7 2 1 1 1

year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s) year(s)

77. Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, 21, 15. There are eight possible expressions:17 + 5 + -21 + 15 = 16 17 + 5 + -21 - 15 = -14 17 + 5 - -21 + 15 = 58 17 + 5 - -21 - 15 = 28 17 - 5 + -21 + 15 = 6 17 - 5 + -21 - 15 = -24 17 - 5 - -21 + 15 = 48 17 - 5 - -21 - 15 = 18

We call the sequence of integers divisible byK if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.You are to write a program that will determine divisibility of sequence of integers.

Input The first line of the input file contains a integer M indicating the number of cases to be analyzed. Then M couples of lines follow. For each one of this couples, the first line contains two integers, N and K(1