Sie sind auf Seite 1von 16

Archives of the Kurukshetra 09 Online Programming Contest

main problemset

Editors:
u.swarnaprakash
Last updated: 2009-01-25 08:22:55

Preface
This electronic material contains a set of algorithmic problems, forming the archives of the Kurukshetra 09 Online Programming Contest (http://www.spoj.pl/KOPC09/), main problemset. The document can be accessed at the following URLs: in PostScript format: http://www.spoj.pl/KOPC09/problems/main.ps in Portable Document Format: http://www.spoj.pl/KOPC09/problems/main.pdf These resources are constantly updated to synchronise with the ever-changing hypertext version of the problems, and to include newly added problems. If you have obtained this document from another source, it is strongly recommended that you should download the current version from one of the aforementioned URLs. Enjoy problem-solving at the Kurukshetra 09 Online Programming Contest!
Disclaimer from the Editors. Despite our best efforts, it is possible that this document contains errors or that some of the content differs slightly from its original hypertext form. We take no responsibility for any such faults and their consequences. We neither authorise nor approve use of this material for any purpose other than facilitating problem solving at the Sphere Online Judge site; nor do we guarantee its fitness for any purpose whatsoever. The layout of the problems in this document is the copyright of the Editors named on the cover (as determined by the appropriate footers in the problem description). The content is the copyright of the respective Editor unless the copyright holder is otherwise stated in the resource section. The document as a whole is not protected by copyright, and fragments of it are to be regarded independently. No responsibility is taken by the Editors if use or redistribution of this document violates either their or third party copyright laws. When referring to or citing the whole or a fragment of this document, please state clearly the aforementioned URLs at which the document is to be found, as well as the resources from which the problems you are referring to originally came. Remarks concerning this document should be sent to the following e-mail address: contact@spoj.pl.

Table of Contents
1. 2. 3. 4. 5. 6. 7. Problem HIST2 (3436. Histogram) Problem KGSS (3693. Maximum Sum) Problem PROOT (3713. Primitive Root) Problem SNOOKER (3723. Snooker) Problem RAINBOW (3724. Rainbow Ride) Problem TREX (3725. Taming a T-REX) Problem KLUCKY (3727. Lucky Number)

SPOJ Problem Set (main)

3436. Histogram
Problem code: HIST2
In statistics, a histogram is a graphical display of tabulated frequencies, shown as bars. It shows what proportion of cases fall into each of several categories. It is a polygon composed of a sequence of rectangles aligned at a common base line. In this problem all rectangles have a width of unit length. But their heights are distinct. Some permutation of the heights will give the maximum perimeter. Your task is to find the maximum perimeter of the histogram and the number of permutations that give the maximum perimeter. [IMAGE] In the image Figure (a) shows a histogram with heights {1,2,3,4} (1st sample testcase) and has a perimeter of 16 units. Figure (b) shows one of the permutations {3,1,2,4} having the maximum perimeter of 20 units.

Input
Input consists of multiple test cases. Each test case describes a histogram and starts with an integer N, 2 <= N <= 15, denoting the number of rectangles it is composed of. Next line consists of N space separated positive integers representing the heights of the rectangles. All heights are distinct and less than or equal to 100. N=0 indicates the end of tests. There are atmost 50 test cases.

Output
For each test case output the maximum possible perimeter of the histogram and the number of permutations that give maximum perimeter in a single line, separated by a single space.

Example
Input: 4 1 2 3 4 3 2 6 5 0 Output: 20 8 24 2

Added by: u.swarnaprakash Date: 2008-11-29 Time limit: 4s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

SPOJ Problem Set (main)

3693. Maximum Sum


Problem code: KGSS
You are given a sequence A[1], A[2], ..., A[N] ( 0 <= A[i] <= 10^8 , 2 <= N <= 10^5 ). There are two types of operations and they are defined as follows: Update: This will be indicated in the input by a U followed by space and then two integers i and x. U i x, 1 <= i <= N, and x, 0 <= x <= 10^8. This operation sets the value of A[i] to x. Query: This will be indicated in the input by a Q followed by a single space and then two integers i and j. Q x y, 1 <= x < y <= N. You must find i and j such that x <= i, j <= y and i != j, such that the sum A[i]+A[j] is maximized. Print the sum A[i]+A[j].

Input
The first line of input consists of an integer N representing the length of the sequence. Next line consists of N space separated integers A[i]. Next line contains an integer Q, Q <= 10^5, representing the number of operations. Next Q lines contain the operations.

Output
Output the maximum sum mentioned above, in a separate line, for each Query.

Example
Input: 5 1 2 3 4 5 6 Q 2 4 Q 2 5 U 1 6 Q 1 5 U 1 7 Q 1 5 Output:

7 9 11 12

Warning: large Input/Output data, be careful with certain languages Added by: u.swarnaprakash Date: 2009-01-10 Time limit: 2s-4s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

SPOJ Problem Set (main)

3713. Primitive Root


Problem code: PROOT
In the field of Cryptography, prime numbers play an important role. We are interested in a scheme called "Diffie-Hellman" key exchange which allows two communicating parties to exchange a secret key. This method requires a prime number p and r which is a primitive root of p to be publicly known. For a prime number p, r is a primitive root if and only if its exponents r, r 2 , r 3 , ... , r p-1 are distinct (mod p). Cryptography Experts Group (CEG) is trying to develop such a system. They want to have a list of prime numbers and their primitive roots. You are going to write a program to help them. Given a prime number p and another integer r < p , you need to tell whether r is a primitive root of p.

Input
There will be multiple test cases. Each test case starts with two integers p ( p < 2 31 ) and n (1 <= n <= 100 ) separated by a space on a single line. p is the prime number we want to use and n is the number of candidates we need to check. Then n lines follow each containing a single integer to check. An empty line follows each test case and the end of test cases is indicated by p=0 and n=0 and it should not be processed. The number of test cases is atmost 60.

Output
For each test case print "YES" (quotes for clarity) if r is a primitive root of p and "NO" (again quotes for clarity) otherwise.

Example
Input: 5 2 3 4 7 2 3 4 0 0

Output: YES NO YES NO

Explanation
In the first test case 3 1 , 3 2 , 3 3 and 3 4 are respectively 3, 4, 2 and 1 (mod 5). So, 3 is a primitive root of 5. 4 1 , 4 2 , 4 3 and 4 4 are respectively 4, 1, 4 and 1 respectively. So, 4 is not a primitive root of 5. Added by: u.swarnaprakash Date: 2009-01-14 Time limit: 3s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

SPOJ Problem Set (classical)

3723. Snooker
Problem code: SNOOKER
Consider a rectangular snooker table with pockets only at the 4 corners of the rectangle as shown in the image below. Consider all integer points on the boundary of the table. At each point, except the four corners (four pockets), you are allowed to hit the ball at an angle of 45 degrees from the side of the table from which you are hitting the ball. From any point on the boundary you can hit the ball in two directions and they are considered to be two different ways. For instance in the image shown below, from the point S the ball can be hit in two ways as shown. Given the dimensions of the board your task is to find the number of ways in which the ball can be hit so that it eventually reaches one of the four holes. Consider the ball to be of negligible size, like a point. Also assume that the ball does not lose energy due to collisions or friction - it keeps moving until it drops into a hole. [IMAGE]

Input
The input has multiple test cases. Each test case consists of two space separated integers M and N, 2 <= M,N <= 10^5, representing the dimensions of the table. M=N=0 indicates the end of tests. There are atmost 300 testcases.

Output
For each test case output the number of ways as described, in a separate line.

Example
Input: 2 2 2 4 3 5 0 0 Output: 0 4 24

Added by: u.swarnaprakash Date: 2009-01-16 Time limit: 3s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

SPOJ Problem Set (main)

3724. Rainbow Ride


Problem code: RAINBOW
Mr.Raju and his extended family are on vacation in Chennai. They visit MGM and see the Rainbow ride. They want to enjoy the ride. However, there are some problems. Each person in the family likes some other people in the family. Each person insists that he or she will go on the ride only if all the people whom that person likes and all the people who like that person also go on the ride. If someone doesnt like anyone and no one likes him, he may come for the ride. You have been roped in to solve this dilemma. Given the weight of the each person in the family, and the list of people they like, and the maximum weight that the Rainbow can bear safely, calculate the maximum number of people in the family who can go on the rainbow.

Input
There will be multiple test cases in the input. For our convenience the family members are numbered from 1 to n. Each test case begins with a line containing two integers n ( 1 <= n <= 1000 ) and C ( 0 <= C <= 1000 ), where n is the number of people in the family and C the maximum capacity of the ride in kilograms. The next line contains n space separated integers with the ith integer giving the weight of the i th family member. These weights are positive and less than or equal to 200 kilograms. Then n lines follow. Each line contains a sequence of integers separated by spaces. The first integer k i gives the number of people in the family person i likes, followed by the persons i likes. An empty line separates each test case. The end of input is indicated by n=0 and C=0 and it should not be processed.There are atmost 50 test cases.

Output
For each test case output on a separate line the maximum number of persons that can take the ride under the given conditions.

Example
Input: 5 200 50 50 50 50 50 1 2 1 3 0 1 5 1 4 3 200 100 100 100 1 2 1 3

1 1 0 0 Output: 3 0

Added by: u.swarnaprakash Date: 2009-01-16 Time limit: 5s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

SPOJ Problem Set (main)

3725. Taming a T-REX


Problem code: TREX
Although evolution theory suggests that mammals started to dominate this world only after the mass extinction of the dinosaurs, there are some people who say man and dinosaurs may have co-existed for some time. Some argue that man even tamed and used some of these animals like the Flintstones. Shankar is such a believer and Sunil is a skeptic. One day Sunil asked Shankar, "If your argument is right how would you tame a T-REX and what would you use it for?". Shankar replied, "We can use it for cow transportation from one village to another and we can keep it calm by feeding it at constant intervals". Sunil argued that the T-REX would have a maximum capacity C to carry. Let us say the distance is d km. If it is long, the T-REX would eat all the cows before it reaches the other village. Shankar argued that he knew a method that takes the maximum possible number of cows M to the destination. Sunil replied, "Oh really? I will give a few conditions. The T-REX will eat 1 cow every km until it reaches the destination. It may do so at any time during a 1 km stretch. So,there can not be a situation where the TREX has no cow to eat. If you drop cows in the middle, you can do so only at distances which are a multiple of 1 km. And, finally all the cows at the destination need to be alive i.e you can not cut a cow (fractions are not allowed)". Shankar was stunned. He needs your help. Given I (the number of cows at the starting village) , d and C find M, the maximum number of cows that can be taken to the destination subject to the mentioned constraints.

Input
There will be multiple test cases in the input. The input begins with a line containing a single integer n,n <= 300, which gives the number of test cases. Then n lines follow each containing the three integers I, 1 <= I <= 10 6 , d, 1 <= d <= 10 5 , and C, 1 <= I <= 10 6 , in that order separated by spaces. d is in kilometers.

Output
For each test case print on a separate line the maximum number of cows that can be transported to the destination village under the given conditions.

Example
Input: 2 3000 1000 1000 30 10 10

Output: 533 5

Note: A few test cases have been added and this problem has been rejudged on January 25th, 2009.
Added by: u.swarnaprakash Date: 2009-01-17 Time limit: 5s Source 50000B limit: Languages: All Kurukshetra 09 Resource: OPC

SPOJ Problem Set (tutorial)

3727. Lucky Number


Problem code: KLUCKY
The Kurukshetra OPC team observed that many online programming contests have a problem titled "Lucky Number". So we decided to have one in KOPC too. We define the Lucky sequence as the infinite sequence of all integers, in ascending order, that can represented as any positive integer power of 5 (i.e 5 k where k is a positive integer) or as a sum of distinct positive integer powers of 5 (i.e 5 a1 + 5 a2 + 5 a3 + ... , where a1,a2,a3, ... are distinct positive integers). All the numbers in the lucky sequence are called lucky numbers. The first few lucky numbers are 5, 25, 30, 125, 130, 150, ... Given n your task is to find the n th lucky number.

Input
First line of input contains an integer t, t <= 200, representing the number of test-cases. Then t lines follow each containing one integer n, 1 <= n <= 8000.

Output
For each test case output the nth lucky number on a separate line. Answers will fit in a 32-bit signed integer.

Example
Input: 4 1 2 3 9 Output: 5 25 30 630

Added by: u.swarnaprakash Date: 2009-01-17 Time limit: 2s Source limit:50000B Languages: All Resource: Kurukshetra 09 OPC

Das könnte Ihnen auch gefallen