Sie sind auf Seite 1von 5

# PROBLEM-SET ( 03 SEP 2011 )

Problem A : K Distinct Min Max Given N and K, find the smallest and the largest positive integers, each with exactly N digits and having exactly K distinct digits ( 0 - 9 ). Note that the answer should have exactly K distinct digits, for eg: 1124214 has only 3 distinct digits 1, 2 and 4. Leading zeros are not allowed, for eg: 0112 is not allowed. Input: Only one line, containing N K ( 1 <= N <= 18 and 1 <= K <= 10 ). Input is given such that the answer will always exist. Output: Print the smallest and largest integers, separated by exactly one space (Followed by a '\n' for new line, no need to display) Time Limit: 1s Sample: Input: 22 Output: 10 98\n Input: 33 Output: 102 987\n ---------------------------------------------------------------------------------------------------------------------------Problem B : SubMatrix Sums Remember that you answered a lot of queries about sum of elements in a subarray last month ? This month we will do the 2D version of it. You are given a matrix A of size MxN having M rows and N columns. You can define a sub-matrix by mentioning just its top-left and bottomright corner cells. A sub-matrix B ((x1,y1)-(x2,y2)) of A is a matrix that has all the elements that belong to the rows x1, x1+1, x1+2, ..., x2 and columns y1, y1+1, y1+2, ..., y2. We use 0-based indices so 0 <= x1 <= x2 < M and 0 <= y1 <= y2 < N. Given a sub-matrix, can you find the sum of all elements in it ? uhhh! Do I need to repeat it again ? ok.. you will be asked many such queries, Q of them. You just need to find the sum of answers to all the queries. Input: First line contains M N ( 1 <= M, N <= 200000, and the product 1 <= M x N <= 1000000 ) Next M lines contains N elements each, describing the matrix A ( 0 <= A[i][j] < 10 ) Next line contains Q, number of queries to follow ( 1 <= Q <= 100000 ) Each of the next Q lines contain x1 y1 x2 y2, as described in the problem statement.

Output: For each query, find the sum of elements in the sub-matrix and just print the sum of answers to all the queries (Followed by a '\n' which is just for a newline, no need to display) Time limit: 1s Sample Cases: Input: 34 1212 1023 3125 2 0112 0023 Output: 28\n Explanation: Sum((0,1)-(1,2)) = 2 + 1 + 0 + 2 = 5 Sum((0,0)-(2,3)) = sum of whole matrix = 23 Sum of answers to all queries = 5 + 23 = 28 ---------------------------------------------------------------------------------------------------------------------------Problem C : Falling footballs Good Morning Folks, We know, you all like Games?? Letz Play A Game!! Free Falling Footballs. In this game, a number of footballs are falling on a flat football field, one by one. The ball falls on a specified axis without any deviation on the path. If the ball, say A, lands on the field directly, it remains where it is. If it lands on top of another ball B, one of the following things happens: -If ball B is surrounded on both sides by balls (denoted by 'O'), ball A remains where it is. A OBO -If there is no ball directly to the right of ball B, ball A slides down to the position directly to the right of ball B. A OB -> OBA A B -> BA -If there is a ball directly to the right of ball B, but no ball directly to the left of ball B, ball A slides

## down to the position directly to the left of ball B. A BO -> ABO

Each time ball A slides down to a different position, it will continue to slide until it's in a place where it will not slide any further. Input : Comma separated numbers. Maximum 50 numbers. Each number between 0 and 20, inclusive. i.e 1<=football-falls<=50, 0<=axis<=20. e.g 0,1,2,1 means first ball falls on axis 0, second ball falls on axis 1, third ball falls on axis 2, fourth ball falls on axis 1, in that order. Output : .O. OOO Sample Case:Input: 1,1,3,2 Output: .O. OOO\n Explanation : X X -> OX -> OOX -> OOO (X denotes location where the last football will settle)

Note: Output contains 'Capital o' and 'not zero 0' Sample Case:Input: 20,5,6,20,5,6 Output: OOOO............OO\n Sample Case:Input: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 Output: ....O..... ...OOO.... ..OOOOO... .OOOOOOO.. OOOOOOOOOO\n ----------------------------------------------------------------------------------------------------------------------------

Problem D : Maximum Sub-Array Sum Given an array A, you now know how to find a Sub-Array sum i.e., A[i]+ A[i+1] + ... + A[j]. A subarray is defined by its starting index i and its ending index j ( 0 <= i <= j < N ). Can you find the maximum sum of a sub-array. Input: First line contains N, number of elements in the array ( 1 <= N <= 100000 ). Second line contains N integers in range [-10^4,10^4], separated by spaces. Output: Print the maximum sum possible for a sub-array of A. (Followed by a '\n' which is just for a newline, no need to display) Time Limit: 1s Sample Cases: Input: 4 1 -2 1 2 Output: 3\n Explanation: Sum(0..0) = 1, Sum(0..1) = -1, Sum(0..2) = 0, Sum(0..3) = 2, . . . , Sum(2..3) = 3, . . . , Sum(3,3) =2 Among all possible Sub-Array sums, Sum(2..3) = 3 has the maximum value. ---------------------------------------------------------------------------------------------------------------------------Problem E : Maximum square sub matrix Given a square matrix A of size N x N. Find the maximum sum of a sub-matrix which is also a square matrix of size M X M where 1<=M<=N. Input Constraints : 1 <= N <=500 & -100 < A[i][j] < 100. Time limit: 1s Sample Cases: Input: 3 123 456 789 Output: 45\n

Explanation: The Sub-Matrix (0,0)-(2,2) has the maximum sum. ---------------------------------------------------------------------------------------------------------------------------Problem F : Maximum Sum of Sub-Matrix Given a matrix of size M x N, find the maximum sum of a sub-matrix. Input: First line contains M N ( 1 <= M, N <= 500 ) Next M lines contains N elements each, describing the matrix A ( -100 <= A[i][j] <= 100 ) Output: The Maximum Sum (Followed by a '\n' which is just for a newline, no need to display) Time Limit: 2s Sample Cases: Input: 33 -11 32 46 73 -39 -56 94 -25 20 Output: 167\n Explanation: The SubMatrix (1,0)-(2,0) has the maximum sum. Input: 35 25 -42 84 54 91 -43 91 -53 92 -8 4 -7 41 67 -69 Output: 341\n Explanation: The Sub-Matrix (0,1)-(2,4) has the maximum sum.

-- The End --