Spring 2015Intermediate Tier Problem Packet

SPRING 2015 PROBLEM PACKET INTERMEDIATE TIER SPONSORED BY PRESENTED BY: RULES AND REGULATIONS •
•
•
•
•
•
•
•
•
•
•
•
•
•
All participants must be Penn State students and must provide an active PSU email address. Teams will be broken up by skill level-­‐-­‐intermediate and advanced. Students that have taken 300-­‐level and up CMPSC classes will be put in the advanced tier. Those who have taken 200-­‐level and below will be put in the intermediate tier. All non-­‐CSE students will be placed in the intermediate tier. Teams may consist of 1-­‐3 people of any skill level. A team's skill level will determined by the highest skill level of a member in the team. Intermediate teams have the option to compete in the advanced tier, but advanced teams may not compete down. Teams will have four hours to complete as many problems as possible. The competition will consist of 10 problems ranging in difficulty from easy to hard for both skill levels. Teams are limited to one computer that they must bring themselves. Teams are permitted to bring books and use Internet sources during the competition. Solutions to problems must be written in C, C++, or Java. Source code for solutions will be uploaded to the judges who will compile and test teams' solutions. Compiled solutions will be limited to an execution time that will vary per problem. If a solution takes too long to finish executing, it is incorrect. Only standard libraries may be used. The C99 and C++98 standards are used for C and C++ respectively with GCC. Java solutions are compiled with Java 7. All input must be through standard input, and all output must be through standard output. Otherwise, a solution is incorrect. HINTS •
•
•
•
•
•
All submissions are subject to a time limit; take too long, and your process is killed. If you use C or C++, you must use `int main()` rather than `void main()` and NEVER use `system("pause")`. If you need `stdlib.h` or `math.h`/`cmath` you MUST explicitly `#include` these even though Visual Studio does not enforce this. Do not prompt for input. Anything your program prints is considered output and will be judged. Read each problem carefully and pay attention to the given input bounds. The submission system needs the Java JDK installed on your computer to run. It's a big download, so do that now if you haven't already. # 1 | ENTER THE MATRIX PROBLEM Matrices are a key part of many computational problems such as cryptography, graphic visualization and manipulation, and machine learning. Dense Matrix Matrix multiplication in particular is used in many performance benchmarks, some of which measure and rate high performance supercomputers. Let’s say you are writing a new benchmarking program which multiplies many matrices of various sizes together. More specifically, you want to know how many elementary operations (addition and multiplication namely) are involved when you multiply these various matrices. For example, let A be a matrix with dimensions 5x10, B be a matrix with dimensions 10x30, and C be a matrix with dimensions 30x30 and we want to know how many elementary operations can come from A*B*C. Recall, however, that this type of multiplication is associative and this can greatly affect the number of elementary operations. For the example above, (A*B)*C would result in 6000 elementary operations, whereas A*(B*C) would result in 10500 elementary operations. INPUT The input consists of two parts. The first part is a list of matrices. This list will begin with an integer n between 1 and 26 indicating the amount of matrix entries that will follow. Each matrix will start with an uppercase letter, denoting its name, along with its dimensions (rows by columns) as integers. Each matrix entry will be on its own line. The second part of the input is a list of operations. All operations will be coupled by parenthesis and letters placed together (without spaces) represents multiplication. For example, say we have matrices A, B, and C, then (AB) = A*B and ((AB)C) = (A*B)*C. OUTPUT For every matrix equation, you must output the number of elementary operations given the appropriate parenthetical notation. If an equation is not legal, output “error” instead. Each output should be on its own line. SAMPLES Input 6 A 5 10 B 10 30 C 30 30 D 10 50 E 5 5 F 50 50 D ((AB)C) (A(BC)) (E(AB)) ((E(AD))F) (E((AD)F)) (E(BC)) Output 0 6000 10500 2250 16250 16250 error # 2 | SYNTAX TUTOR PROBLEM In the future, the laziness efficiency of the American people has resulted in reduced language with a straightforward syntax. This same laziness efficiency has created a demand for a syntax tutor to teach their young how to grammar -­‐-­‐ and to program this tutor is your task. A simple drag/drop interface has already been programmed, so all you need to do is check the input and provide a “Correct!” if the syntax is correct or “Try Again…”, otherwise. There are four simple rules for New ‘Mer’can’s syntax: 1.
2.
3.
4.
The only characters in the language are A to L, inclusive and x, y, z, & Every character from A to L is a syntactically correct sentence If N is a correct sentence, then xN is as well If N and M are correct sentences, so are yNM, zNM, and &NM All other sentences are syntactically incorrect. INPUT Inputs shall range from 1 to 256 characters. They are terminated by new line characters. The collection of sentences is terminated with a 0. OUTPUT The output shall be Correct! for each syntactically correct sentence, and Try Again... for each syntactically incorrect sentence. Outputs shall be separated by new line characters, and terminated with an eof. SAMPLES Input AT yAB xyAB xyyABC 0 Output Try Again… Correct! Correct! Correct! # 3 | TESLA MOTORS NEEDS YOU PROBLEM Tesla Motors is an American car company founded by Elon Musk (among others) with the objective of making electric cars and other fuel efficient vehicles commonplace. They first made news when they created the first electric sports car, the Tesla Roadster. They are currently starting to ramp up production for their Model 3 which will be produced in much higher volumes than any of their previous vehicles at an attractive price point of $35,000. You, and your team have been brought on as consultants to try to maximize the number of orders that we can fill while still in our current reduced operational state. We can simplify our task by projecting a system where we roll out one car from each factory every hour. A chart of customer orders is made at the start of each month to fill out the production schedule. The first day of each month will correspond to day 0, the second day 1, so on and so forth. Your task is to find the optimal solution and make the Tesla Model 3 what the Ford Model T was for the previous generation. INPUT You will receive a single positive integer followed by a blank line which will indicate the number of cases. If there are two consecutive inputs they will also have a new line in between. The first line has the number of orders (n) (<=80000), followed by n lines each containing two integers. One is the amount of materials and resources available, we’ll assume aluminum is the only material for simplicity’s sake (<=1000) and the time by which it is received (in hours, less than 2*10^6). OUTPUT Return the number of orders that can be filled for each case. Separate the output of two cases with a blank line in between. SAMPLES Input Output 4 1 6 7 15 8 20 6 8 4 9 3 21 5 22 HINTS: Our economists recommend sequencing orders in non-­‐decreasing order of time it needs to be filled. There is also a simple solution for two orders such that if the amount of aluminum needed for the first order is greater than the second and the delivery time is less for the first order as opposed to the second; if the first order is accepted, the second must also be accepted for optimal solution. # 4 | TATA TRANSCRIPTION PROBLEM You are an RNA polymerase (ok not actually, but let’s pretend), and your goal is to transcribe some RNA from a DNA template. However, you can’t just go copying any segment of DNA! That’d be silly. So we need to look for a region of DNA called the promoter region. In this case, we’re looking for a specific promoter region called a TATA box. The name is actually very intuitive, but let’s start with a little review of DNA before we get into that. DNA is made up of four bases: A, C, G, T. These bases can appear in any order and in any combination. For simplicity, we’ll say a TATA box is just a sequence of A’s and T’s alternating repeatedly, e.g. ATATATA or TATATAT. This TATA box can appear in any part of the DNA sequence and it can have any length. The premise is simple: given a set of n DNA sequences, return the positions of the sequences which contain a TATA box with length greater than or equal to m. A TATA box may start and end with either a T or an A, i.e. ATAT, TATA, TAT, ATA are all acceptable TATA boxes. The length of the TATA box spans from the first T/A and continues as long as the characters are alternating adjacently. INPUT Input will start with two integers, separated by a space: m which represents the minimum length of an acceptable TATA box and n which is the number of sequences you are given. This line will be followed by n sequences. These sequences may or may not contain a TATA box and could contain more than one. Each sequence will be on a new line, and each sequence may have any length. OUTPUT You need to output the positions of the sequences containing a TATA box with length at least m. The position numbers should be print on one line separated by a space. Position numbers start at zero. SAMPLES Input 5 4 GCAAGCGTATAA TATAGCGCCCGCTATATCGGAA GCGCGCTTTTTTTTCCCTGCAAAAAAAAAAAAAAAAA GAGAGAGACACACACACATATATATATATAT Output 1 3 # 5 | SNEAKY SNEAKY PROBLEM Everyone knows someone who has a fake ID. Bars across the country make it their mission to catch people using fake IDs. Sometimes people make dumb mistakes when making fake IDs, like reusing the same license number on different cards. Your job is to write a program to help bars find out who is using a fake ID. INPUT Your input will be a list of 2000 names and ID numbers separated by commas. OUTPUT The output will be the names and IDs of the people who have the same ID numbers ordered alphabetically by first name. The names and ID numbers will be separated by commas. SAMPLES Input Marsha Johnson, 123 Carrie Washington, 456 James Hatton, 123 Output James Hatton, 123 Marsha Johnson, 123 # 6 | SPIDERMAN SAVES THE DAY PROBLEM After a long night of crime fighting, Spiderman drops his haul off at the local jail. All of the criminals are spider webbed up into three bundles. The local police only have three jail cells in which to put the three bundles. They need to sort the criminals according to their crimes into the correct jail cell. They want to move criminals with as few moves as possible. They also must move each criminal through jail cells. They can’t walk them up and down the halls because the criminals are more likely to escape. There are three types of criminals: burglars, murderers, and thieves. You must sort all of the criminals into correct jail cells. INPUT The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of burglars, murderers, and thieves (respectively) in bundle number 1, the second three represent the number of burglars, murderers, and thieves (respectively) in bundle number 2, and the last three integers represent the number of burglars, murderers, and thieves (respectively) in bundle number 3. For example, the line 10 15 20 30 12 8 15 8 31 indicates that there are 20 burglars in bundle 1, 12 murderers in bundle 2, and 15 thieves in bundle 3. Integers on a line will be separated by one or more spaces. OUTPUT For each line of input there will be one line of output indicating what criminals go in what cell to minimize the number of movements. You should also print the minimum number of movements. The output should consist of a string of the three upper case characters 'B', 'M', 'T' (representing the colors burglar, murderer, and thieves) representing the criminal associated with each cell. The first character of the string represents the color associated with the first cell, the second character of the string represents the color associated with the second cell, and the third character represents the color associated with the third cell. The integer indicating the minimum number of movements should follow the string. If more than one order of burglars, murderers, and thieves cells yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed. SAMPLES Input 1 2 3 4 5 6 7 8 9 5 10 5 20 10 5 10 20 10 Output MBT 30 TMB 50 # 7 | SUPERMAN SWAPS PROBLEM Every time Clark Kent needs to change into Superman, he has to find the closest elevator to change. Clark is a very efficient guy. It takes him a certain number of floors to change into his Superman outfit. He wants to optimize how long it takes him to change between floors. He needs to sort how many floors it takes for him to change into ascending order. Each swap has a cost. To swap two floors, the cost is the sum of those floors. INPUT The input contains several test cases. Each case consists of two lines. The first line contains a single integer n (n > 1), representing the number of items to be sorted. The second line contains n different integers (each positive and less than 1000), which are the numbers to be sorted. The input is terminated by a zero on a line by itself. OUTPUT For each test case, the output is a single line containing the test case number and the minimal cost of sorting the numbers in the test case. Place a blank line after the output of each test case SAMPLES Input 3 3 2 1 4 8 1 2 4 5 1 8 9 7 6 6 8 4 5 3 2 7 0 Output Case 1: 4 Case 2: 17 Case 3: 41 Case 4: 34 # 8 | MINESWEEPIER PROBLEM Minesweeper is a game played on an MxN grid of squares, with some of the squares containing mines. The goal of the player is to find all of the mines. Squares that don’t contain mines are called “safe”. To aid the player, the game shows a number in each safe square that shows how many mines there are adjacent to that square. Each square has at most eight adjacent squares. But Richard, the Master of Minesweeper, has designed a more difficult version of the game. In this version, safe squares only show how many mines there are in a subset S of the adjacent squares. S contains different numbers based on what squares the safe squares report on, using the following chart, if the safe space is P: 0 1 2
3 P 4
5 6 7
(Position Chart) So 0 is above and to the left of P, 1 is above P, 4 is the right of P, etc. In the minefield: mines are represented by the character ‘*’ (asterisk) safe spaces are represented by the character ‘-­‐‘ (hyphen) The following 4x4 minefield with S = {0, 1, 2, 3, 4, 5, 6, 7}, the minefield on the right when the numbers are reported: *---
*100
----
2210
-*--
1*10
----
1110
INPUT The input will consist of an arbitrary number of fields. In each field: The first line contains one integer, K, representing the number of elements in the set S. The second line of contains K number of integers, each corresponding to a position element to be placed in S, using the Position Chart given above. The third line will contain two integers: M, and N, which indicate the dimensions of the minefield The following M lines will contain N characters, either ‘*’ or ‘-­‐‘, indicating the mine squares and safe squares, respectively. Terminate input when K is -­‐1. Limits: 1 ≤ M ≤ 15 1 ≤ N ≤ 15 1 ≤ K ≤ 8 or K = -­‐1 OUTPUT For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next M lines should contain the minefield, with all of the ‘-­‐‘ characters replaced by the number of mines contained in any of the adjacent squares with respect to the set of positions in S. Print a newline after each field. SAMPLES Input 3 3 5 6 4 4 -­‐-­‐-­‐* -­‐-­‐-­‐-­‐ -­‐*-­‐* -­‐-­‐-­‐* 6 0 2 4 5 6 7 7 8 -­‐-­‐-­‐**-­‐-­‐* -­‐-­‐**-­‐-­‐-­‐* -­‐-­‐*-­‐-­‐-­‐-­‐* -­‐-­‐*-­‐-­‐-­‐-­‐-­‐ -­‐***-­‐*-­‐* -­‐**-­‐-­‐-­‐-­‐-­‐ -­‐-­‐-­‐**-­‐*-­‐ -­‐1 Output Field #1: 000* 0111 0*1* 000* Field #2: 013**02* 02**113* 03*2102* 14*32131 2***1*1* 2**34231 112**1*0 # 9 | MORSE NO MORE PROBLEM Occasionally you need to send and receive secret messages to your friends, and up until now Morse code worked fine. Morse code is the famous method of transmitting text with dots and dashes. Unique combinations of dots and dashes corresponded to specific letters and numbers. However, you and your friends wanted to step it up so you devised a system that allowed for characters as well. Instead of .’s and –‘s, this new system uses bits of 1’s and 0’s. Every combination of 5 bits (00111, 11000 , 10101, etc.) represents a different character. This on its own allows for 32 different characters. However, your new system also has two different modes: primary and secondary. Both the primary and the secondary modes correspond to a distinct set of 32 characters. That means each 5-­‐bit sequence corresponds to two different characters, the only distinction being that one is “primary” and the other is “secondary.” You know which mode to decode in based on which mode has been most recently toggled. The bit sequence 11011 is reserved for toggling primary mode while sequence 11111 toggles secondary mode. Secret messages begin in primary mode by default. Therefore, any 5-­‐bit sequences transmitted after detecting a sequence of 11111 should be decoded as secondary characters – up until the pattern 11011 is detected and toggles back to primary mode. INPUT The input has two parts. The first part of the input consists of two lines. The first line contains the 32 primary characters while the second line contains the 32 secondary characters. These characters are listed in the order of the 5-­‐bit sequence that encodes them (i.e. the nth character in a line corresponds to n in binary). There are spaces representing toggling at 11011 and 11111. The second part contains the secret message(s), encoded with your system. Each message is contained on one line with up to 80 bits per message. Each line will strictly contain 0’s and 1’s with no characters or spaces separating them. Remember: each secret message begins in primary mode by default. OUTPUT The output should contain one line of text for each message. Each message should contain the appropriate decoded characters. SAMPLES Input ZYXWVUT SRQPONMLKJIHGFEDCBA *=< 0123456 789>@%,.+!/-­‐~()&:;” #?$ 11000011000110110100010011101000110010001111 111000 00001011000010100111010000110001111001001011 010111001110011010011101100011111000011001011110110
111111000110001 1 Output CONGRATS: YOU SOLVED THE CODE!! # 10 | SPECIAL SPICES PROBLEM A busy restaurant in New York requires its kitchen to be fast and efficient. They have one rack dedicated to store reserve spices. These special spices are used by most of the cooks in the kitchen and are most popular therefore they can be checked out for short periods of time. When the cook wants a reserve spice, the spice is put on the left side of the rack. All the spices that were on rack must be moved to the right to make room for the new spice that was added. When a reserve spice is checked out then returned, it is put on the left end of the rack as if it were being placed on reserve. When a spice is placed on reserve or when a previously checked out reserve spice is returned and there is not enough room on the reserve rack, then starting at the right end, as many spices are removed from the reserve rack as needed in order to make room for the spice to be put on the reserve rack.
INPUT The first line of the input should contain an integer, which is the total available space on the reserve rack measured in millimeters( 1 inch = 25 mm). Its value will be at least 250 and at most 1500. Your program will start with an empty reserve rack. The following four commands will be used. The ADD command is followed by a spice (starting in column 10), consisting of at most 29 printable characters. The spice is followed (starting in column 40) by the thickness of the spice (the amount of rack space it will require), a positive integer, not greater than 150. The effect of the ADD command will be to place a new spice on reserve, as described above. Once a particular spice has been ADDed, it will not be ADDed again as long as that spice is on the reserve spice rack or has been checked out. However, if a spice has been forced off the reserve spice rack (by another ADD command or by a RETURN command) then that spice may appear in a subsequent ADD command (with the same thickness as before). The CHECKOUT command is followed by a spice (starting in column 10) which is currently on the reserve rack. The effect of the CHECKOUT command is to remove the specified spice from the reserve rack (until it is placed back on the reserve rack by a subsequent RETURN command. A CHECKOUT may create a gap between two spices. Such a gap will count as part of the free space. The RETURN command is followed by a spice (starting in column 10) which is currently checked out. The effect of the RETURN command is to put the specified spice to the left end of the reserve rack. The effect of the PRINT command will be to display in the output file: the spice and thickness of each spice shaker currently on the reserve rack, one spice per line, in the (left-­‐to-­‐right) order in which the spices are currently on the reserve rack, the currently available space on the reserve rack, and one blank line. OUTPUT In column 1 the commands such as print, return, add, and checkout will be displayed. Those will be followed by the name of the spice and then size of the spice shaker will be right-­‐justified. SAMPLES Input
Output
250 PRINT AVAILABLE SHELF SPACE: 250 ADD Cinnamon 38 Nutmeg 101 ADD Oregano Cardamom 44 63 Oregano 63 ADD Cardamom Cinnamon 38 44 AVAILABLE SHELF SPACE: 4 ADD Nutmeg 101 Cloves 79 PRINT Nutmeg 101 ADD Cloves Salt 44 79 AVAILABLE SHELF SPACE: 26 PRINT CHECKOUT Salt Fenugreek 15 ADD Chives Chives 55 55 Cloves 79 ADD Fenugreek Nutmeg 101 15 AVAILABLE SHELF SPACE: 0 PRINT End of program