The 2008 BSUIR Final programming contest 24’April, 2008 Problem A: Vito’s Family Input file: A.IN Output file: A.OUT Time limit: 1 sec. The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them. Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem. Input: The input consists of several test cases. The first line contains K - the number of test cases (1 <= K <= 5). For each test case you will be given the integer number of relatives r (1 <= r <= 500) and the street numbers (also integers) s1, s2, …, si, …., sr where they live (0 <= si <= 30000 ). Note that several relatives could live in the same street number. It could also happen that the optimal street number for Vito coincides with the number of some other relative. Output: For each test case your program must write the minimal sum of distances from the optimal Vito’s house to each one of his relatives. The distance between two street numbers si and s j is di j = abs( si - s j ). Sample Input 2 2 2 4 3 2 4 6 Sample Output 2 4 The 2008 BSUIR Final programming contest 24’April, 2008 Problem B: Common sets Input file: B.IN Output file: B.OUT Time limit: 1 sec. The table below has two rows and n columns. The first row contains the numbers 1, 2, ... n in order. The second row contains numbers between 1 to n inclusive but in no particular order (and may contain duplications and omissions). 1 3 2 1 3 1 4 5 5 5 6 4 7 6 By choosing different columns you get two sets of numbers. For example, by choosing columns 1, 4 and 6, you get the sets {1, 4, 6} and {3, 5, 4} (see below). 1 3 2 1 3 1 4 5 5 5 6 4 7 6 Some sets will contain exactly the same numbers, for example if you select columns 1 and 3, then the two sets will each contain 1 and 3 (note that the order doesn't matter). You must write a program that finds the largest set that contains exactly the same numbers (the largest common subset). In this case the largest common subset is {1, 3, 5}. Input: The first line will contain n, (1 n 100). The next line contains the n numbers that go into the second row of the table. Output: The output should be one line containing the numbers of the largest common subset. The numbers should appear in the same order as they appear on the top row, i.e. in ascending order (sorted starting with the smallest number). (There is only one possible largest common subset for each input file). Examples: Input 7 3 1 1 5 5 4 6 Output 1 3 5 Input 6 3 2 6 5 1 5 Output 1 2 3 5 6 The 2008 BSUIR Final programming contest 24’April, 2008 Problem C - Palindromia Input file: C.IN Output file: C.OUT Time limit: 1 sec. A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string S, determines the number of maximal palindromes that can be obtained from S by removing some characters. The palindrome P is maximal if there are no palindromes with length greater then the length P. Input: The first line contains one string S. The (1 <= length(S) <= 100). string is formed from lowercase letters from ‘a’ to ‘z’ Output: The first line contains one integer, which is the number of maximal palindromes. Examples: Input #1 abca Output #1 2 Comment: The maximal palindromes are ‘aba’ and ‘aca’. Input #2 aaabbbcccddd Output #2 4 Input #3 abcdabcdabcd Output #3 36 The 2008 BSUIR Final programming contest 24’April, 2008 Problem D: CALISTHENICS Input file: D.IN Output file: D.OUT Time limit: 1 sec. The cows hate calisthenics (exercises). They do, however, enjoy jogging a little bit. They have surveyed their pasture and have created a list of all the possible paths in the field. Interestingly, the paths are "directed" – paths can be traversed only in one direction. Sometimes, two different paths connect a pair of points, one path going one way and one path going the other way. Your job is to help the cows find the shortest "circular route" around which they can jog. A circular route is one which starts and ends at the same location. The length of a route is the sum of the lengths of the paths one must traverse to jog around the route. Paths in their pasture always connect two points. Each point is named as a positive integer. These points are sequentially numbered, starting at 1. Distances between paths are positive integers. It is guaranteed that at least one "circular route" appears in any supplied cow pasture. Input: Line 1: integer: N, 1 ≤ N ≤ 2000, the number of connections between pairs of points in the pasture; the number of distinct points will never exceed 400; Line 2 … N+1: N lines specifying a space-separated beginning point, end point, and distance; no distances exceeds 1000 Output: A single line with the integer distance around the shortest possible circuit. Sample Input: 4 3 1 5 1 2 4 2 3 3 1 3 44 Sample Output: 12 The 2008 BSUIR Final programming contest 24’April, 2008 Problem E: Milk Team Select Input file: E.IN Output file: E.OUT Time limit: 2 sec. Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the world-famous Multistate Milking Match-up (MMM) competition. As you probably know, any team that produces at least X (1 <= X <= 1,000,000) gallons of milk is a winner. Each cow has the potential of contributing between -10,000 and 10,000 gallons of milk. (Sadly, some cows have a tendency to knock over jugs containing milk produced by other cows.) The MMM prides itself on promoting family values. FJ's cows have no doubt that they can produce X gallons of milk and win the contest, but to support the contest's spirit, they want to send a team with as many parent-child relationships as possible (while still producing at least X gallons of milk). Not surprisingly, all the cows on FJ's farm are female. Given the family tree of FJ's cows and the amount of milk that each would contribute, compute the maximum number of parent-child relationships that can exist in a winning team. Note that a set of cows with a grandmothermother-daughter combination has two parent-child relationships (grandmother-mother, mother-daughter). Input: * Line 1: Two space-separated integers, N and X. * Lines 2..N+1: Line i+1 contains two space-separated integers describing cow i. The first integer is the number of gallons of milk cow i would contribute. The second integer (range 1..N) is the index of the cow's mother. If the cow's mother is unknown, the second number is 0. The family information has no cycles: no cow is her own mother, grandmother, etc. Output: * Line 1: The maximum number of parent-child relationships possible on a winning team. Print -1 if no team can win. Sample Input: 5 8 -1 0 3 1 5 1 -3 3 2 0 Sample Output: 2 Input Details: There are 5 cows. Cow 1 can produce -1 gallons and has two daughters, cow 2 and 3, who can produce 3 and 5 gallons, respectively. Cow 3 has a daughter (cow 4) who can produce -3 gallons. Then there's cow 5, who can produce 2 gallons. Output Details: The best team consists of cows 1, 2, 3, and 5. Together they produce (-1)+3+5+2 = 9 >= 8 gallons and have 2 parent-child relationships (1--2 and 1--3). Note that a team with cows 2, 3, and 5 would be able to produce more milk (10 gallons), but would have fewer parent-child relationships (0). The 2008 BSUIR Final programming contest 24’April, 2008 Problem F: Shortcut Input file: F.IN Output file: F.OUT Time limit: 2 sec. Mirek has a favourite way from home to the university that he traverses every working day. The route consists of sections and each section is a straight segment 10 meters long. Each section is either a straight ahead extension of the previous section or it is perpendicular to the previous section. After traversing each section Mirek takes a small break to admire the beauty of the nature. During his walk he never visits the same place twice. Yesterday Mirek stayed up long in the night at the party and today he got up late from bed. He knows that he will miss the first lecture unless he changes his usual route. He plans to make one shortcut but he wants the shortcut to be as short as possible (well, we can tell you in secret that he doesn't want to be on time, he just wants to calm his conscience). The shortcut must be either a horizontal or vertical segment connecting two break points of Mirek's route. Please help Mirek find the shortest shortcut. Write a program that: • reads Mirek's route, • computes the shortest shortcut on the route, • writes the result. Input: The first line of the input contains one integer n (3 <= n <= 250 000) being the number of sections of the route. The second line of the input contains a sequence of n characters N, E, S or W with no spaces in between. Each character is a description of one section of the route. Character N, E, S or W means that Mirek walks 10 meters north, east, south or west respectively. You may assume that at least one shortcut exists for the given route. Output: The first and only line of the output contains integers I, b, e and character d separated by single spaces. Integer I is the length of the shortest shortcut (measured in 10 m segments). Integers b and e axe the numbers of break points where the shortcut begins and ends respectively (we number break points with consecutive integers from 0 for Mirek's home to n for the university). Character d is the direction of the shortcut. If more than one shortcut of the minimal length exists you should output the one that begins earliest on the route. If more than one shortcut of the minimal length begins at the same break point you should output the one that ends furthest on the route. Sample Input: Sample Output: 12 NNNENNWWWSSW 2 3 11 W The 2008 BSUIR Final programming contest 24’April, 2008 Problem G: Picture Input file: G.IN Output file: G.OUT Time limit: 2 sec. A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure l. The corresponding boundary is the whole set of line segments drawn in Figure 2. Figure 1. A set of 7 rectangles Figure 2. The boundary of the set of rectangles The vertices of all rectangles have integer coordinates. Input: The first line of the file contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. Output: The file must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles. Constraints: 0 < number of rectangles < 5000. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result may need a 32-bit signed representation. Sample Input: 7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16 This corresponds to the example of Figure 1. Sample Output: 228 This is the contents of the output file for the example above.