Problem B. Manage your Energy
Small input 12 points | |
Large input 23 points |
Problem
You've got a very busy calendar today, full of important stuff to do. You worked hard to prepare and make sure all the activities don't overlap. Now it's morning, and you're worried that despite all of your enthusiasm, you won't have the energy to do all of this with full engagement.
You will have to manage your energy carefully. You start the day full of energy - E joules of energy, to be precise. You know you can't go below zero joules, or you will drop from exhaustion. You can spend any non-negative, integer number of joules on each activity (you can spend zero, if you feel lazy), and after each activity you will regain R joules of energy. No matter how lazy you are, however, you cannot have more than E joules of energy at any time; any extra energy you would regain past that point is wasted.
Now, some things (like solving Code Jam problems) are more important than others. For the ith activity, you have a value vi that expresses how important this activity is to you. The gain you get from each activity is the value of the activity, multiplied by the amount of energy you spent on the activity (in joules). You want to manage your energy so that your total gain will be as large as possible.
Note that you cannot reorder the activities in your calendar. You just have to manage your energy as well as you can with the calendar you have.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case is described by two lines. The first contains three integers: E, the maximum (and initial) amount of energy, R, the amount you regain after each activity, and N, the number of activities planned for the day. The second line contains N integers vi, describing the values of the activities you have planned for today.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the maximum gain you can achieve by managing your energy that day.
Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ E ≤ 5.
1 ≤ R ≤ 5.
1 ≤ N ≤ 10.
1 ≤ vi ≤ 10.
Large dataset
1 ≤ E ≤ 107.
1 ≤ R ≤ 107.
1 ≤ N ≤ 104.
1 ≤ vi ≤ 107.
Sample
Input |
Output |
3
|
Case #1: 12
|
In the first case, we can spend all 5 joules of our energy on the first activity (for a gain of 10), regain 2 and spend them on the second activity. In the second case, we spend 2 joules on the first activity, regain them, and spend 5 on the second. In the third case, our regain rate is equal to the maximum energy, meaning we always recover all energy after each activity - so we can spend full 3 joules on each activity.
Small input 1 10 points | |
Small input 2 31 points |
Problem
Maryam and Peiling have recently been practicing a new number trick, and they need your help to get it right. The trick goes as follows: Maryam starts by picking N independent random integer numbers, each between 2 and M, inclusive, appearing with equal probability, and writes them down on N cards, one number per card. Note that some numbers might be equal. Then, she repeats the following K times: take a random subset of cards (each card is taken with probability 0.5), and write down the product of the numbers on those cards. Having done all that, she shows all K products to Peiling, and Peiling's goal is to guess what the original N numbers were, knowing just N, M, and the products.
An example game with N=3, M=4, K=4 might go like this: first, Maryam picks 3 random numbers between 2 and 4, inclusive - let's say she randomly chose A1=3, A2=3 and A3=4. Then, she calculates four products of random subsets of those three numbers. For example, let's say those products are A1*A2=9, A3=4, A1*A2*A3=36, and 1=1 (the last product has no numbers in it, so it's equal to 1). Peiling receives numbers 9,4,36,1 from her, and she's also told that N=3 and M=4. In this case, just seeing the number 36 is enough to find what the original numbers were, since the only way to represent that as a product of up to 3 numbers, each up to 4, is 3*3*4. So Peiling says that the original numbers were 3, 3 and 4, and the audience is impressed.
In some other cases, guessing the original numbers is not as simple. For example, it might happen that all products are equal to 1. In that case there is no way to know anything about the hidden numbers, so Peiling cannot always be right. However, Peiling knows that Maryam follows the procedure exactly as described above: she selects the first N numbers as independent uniform integers between 2 and M, and then selects K independent random subsets, picking each number into each subset independently with probability 0.5. Help Peiling use that knowledge to make better guesses!
Solving this problem
This problem is a bit unusual for Code Jam. You will be given R independent sets of K numbers each, and should print an answer for each set — this part is as usual. However, you don't need to get all of your answers right! Your solution will be considered correct if answers for at least X sets are correct, with the value of X given in the Limits for the given input, below. However, you must follow the output format, even for sets in which your answer doesn't turn out to be correct. The only thing that can be wrong on any sets, yet still allow you to be judged correct, is the digits you output; but there should still be exactly N digits printed for each case, and each digit must be between 2 and M.
This problem involves randomness, and thus it might happen that even the best possible solution doesn't make X correct guesses (remember the situation when all products are equal to 1?) for a certain input. Because of that, this problem doesn't have a Large input, but instead has two Small inputs. That means you can try again if you think you got unlucky. You may only attempt to solve the second Small input once you have solved the first one. Otherwise, both Small inputs work in the same way as Small inputs for any other problem: you may try multiple times, and there is a 4-minute penalty for incorrect submissions if you later solve that input, even if the only reason you got it wrong was chance.
Good luck!
Input
The first line of the input gives the number of test cases, T, which is always equal to 1. The second line of the input file contains four space-separated integers R, N, M and K, in that order. The next R lines describe one set of K products each. Each of those lines contains K space-separated integers — the products that Maryam passes to Peiling. It is guaranteed that all sets in the input are generated independently randomly according to the procedure from the problem statement.
Output
On the first line, output "Case #1:". On each of the next R lines output N digits — your guess for Maryam's hidden numbers for the corresponding set of products. You can print the numbers for each set in any order, but there must be exactly N digits, each between 2 and M, inclusive (note that M<10, so none of the numbers will be more than one digit). Do not put spaces between the digits.
Limits
First Small dataset
T = 1.
R = 100.
N = 3.
M = 5.
K = 7.
You need to get at least X=50 sets right.
Second Small dataset
T = 1.
R = 8000.
N = 12.
M = 8.
K = 12.
You need to get at least X=1120 sets right.
Sample
Input |
Output |
1
|
Case #1:
|
Note
The sample input doesn't follow the limitations for either input. In the sample input, you need to get at least X=1 sets right.
In the sample input, Maryam picked the numbers 3, 3, 4 the first time, and the numbers 2, 4, 4 the second time. In the sample output, Peiling guessed correctly the first time, but not the second time.
Round 1A was our biggest non-Qualification round ever, with 6414 contestants who downloaded at least one input. Contestants faced three challenging problems: Bullseye could be tricky and cause integer overflow without some careful attention (or the use of Python, or similar languages), and 62% of attempts on the Large input failed as a consequence. Manage Your Energy gave contestants a similarly hard time, with a 60% failure rate: many people found their algorithms didn't run quickly enough to fill a calendar with 10,000 events.
The toughest problem of all was the non-traditional Good Luck. The problem required some knowledge of probability, and gave contestants the opportunity to retry the second input set. It didn't help most of them, though: 95% of the people who attempted the problem got it wrong.
At the end of the day, though, an impressive 92% of our contestants solved something, and 23 people got everything right.
We hope everybody enjoyed the round! Congratulations to the Top 1000, who have now made it to Round 2; and to everyone else, we'll see you in 1B and 1C!
Cast
Problem A. Bullseye Written by Khaled Hafez. Prepared by Karim Nosseir and Hackson Leung.
Problem B. Manage Your Energy Written by Onufry Wojtaszczyk. Prepared by Zhen Wang and Onufry Wojtaszczyk.
Problem C. Good Luck Written by Petr Mitrichev. Prepared by Tomek Czajka and Petr Mitrichev.
Contest analysis presented by Bartholomew Furrow, Hackson Leung, Onufry Wojtaszczyk and Tomek Czajka. Solutions and other problem preparation by Igor Naverniouk, Bartholomew Furrow, Hao Pan, Jan Kuipers and Victor Passichenko.Category | Asked | Question | Answered | Answer | ||||
---|---|---|---|---|---|---|---|---|
Manage your Energy Announcement | 23:37 | In "Manage your Energy" in the input, R > E sometimes, even though the problem statement says it shouldn't. | 27:29 | The "limit" saying R <= E has now been replaced with a different limit for R; it is *not* required to be <= E. |